@@ -204,6 +204,21 @@ FSReqPromise<AliasedBufferT>::~FSReqPromise() {
204204 CHECK_IMPLIES (!finished_, !env ()->can_call_into_js ());
205205}
206206
207+ inline bool FSOperationChannelHasSubscribers (FSOperationChannels& channels,
208+ FSOperationChannel channel) {
209+ diagnostics_channel::Channel* ch =
210+ channels[static_cast <size_t >(channel)].get ();
211+ return ch != nullptr && ch->HasSubscribers ();
212+ }
213+
214+ inline bool AnyFSOperationChannelHasSubscribers (FSOperationChannels& channels) {
215+ for (size_t i = 0 ; i < kNumFSOperationChannels ; i++) {
216+ diagnostics_channel::Channel* ch = channels[i].get ();
217+ if (ch != nullptr && ch->HasSubscribers ()) return true ;
218+ }
219+ return false ;
220+ }
221+
207222template <typename AliasedBufferT>
208223FSReqPromise<AliasedBufferT>::FSReqPromise(BindingData* binding_data,
209224 v8::Local<v8::Object> obj,
@@ -233,8 +248,8 @@ void FSReqPromise<AliasedBufferT>::Reject(v8::Local<v8::Value> reject) {
233248template <typename AliasedBufferT>
234249void FSReqPromise<AliasedBufferT>::Resolve(v8::Local<v8::Value> value) {
235250 finished_ = true ;
236- PublishFSOpCompletionEvent (this , FSOperationChannel:: kAsyncEnd , " result " ,
237- value);
251+ PublishFSOpCompletionEvent (
252+ this , FSOperationChannel:: kAsyncEnd , " result " , value);
238253 v8::HandleScope scope (env ()->isolate ());
239254 InternalCallbackScope callback_scope (this );
240255 v8::Local<v8::Value> val;
@@ -338,12 +353,18 @@ FSReqBase* AsyncDestCall(Environment* env, FSReqBase* req_wrap,
338353 // See SyncCallAndThrowIf: instrumentation is unsafe with a pending
339354 // exception.
340355 if (binding != nullptr && !env->isolate ()->HasPendingException ()) {
341- channels = & GetFSOperationChannels (binding, env, syscall);
356+ channels = GetFSOperationChannels (binding, env, syscall);
342357 req_wrap->set_op_channels (channels);
343- if (FSOperationChannelHasSubscribers (*channels,
344- FSOperationChannel::kStart )) {
345- PublishFSOperationEvent (env, *channels, FSOperationChannel::kStart ,
346- api, nullptr , req_wrap->data (), -1 , nullptr ,
358+ if (channels != nullptr && FSOperationChannelHasSubscribers (
359+ *channels, FSOperationChannel::kStart )) {
360+ PublishFSOperationEvent (env,
361+ *channels,
362+ FSOperationChannel::kStart ,
363+ api,
364+ nullptr ,
365+ req_wrap->data (),
366+ -1 ,
367+ nullptr ,
347368 v8::Local<v8::Value>());
348369 }
349370 }
@@ -363,8 +384,14 @@ FSReqBase* AsyncDestCall(Environment* env, FSReqBase* req_wrap,
363384 // The path is captured for the completion events; it requires a copy
364385 // since the uv request is cleaned up before they fire.
365386 req_wrap->set_op_path (path == nullptr ? std::string () : path);
366- PublishFSOperationEvent (env, *channels, FSOperationChannel::kEnd , api,
367- path, req_wrap->data (), fd, nullptr ,
387+ PublishFSOperationEvent (env,
388+ *channels,
389+ FSOperationChannel::kEnd ,
390+ api,
391+ path,
392+ req_wrap->data (),
393+ fd,
394+ nullptr ,
368395 v8::Local<v8::Value>());
369396 }
370397 return req_wrap;
@@ -426,12 +453,18 @@ int SyncCallAndThrowIf(Predicate should_throw,
426453 // of which is safe with a pending exception (a multi-step operation keeps
427454 // going after a failed step to clean up, e.g. write + close).
428455 if (binding != nullptr && !env->isolate ()->HasPendingException ()) {
429- channels = &GetFSOperationChannels (binding, env, req_wrap->syscall_p );
430- if (FSOperationChannelHasSubscribers (*channels,
431- FSOperationChannel::kStart )) {
432- PublishFSOperationEvent (env, *channels, FSOperationChannel::kStart ,
433- " sync" , req_wrap->path_p , req_wrap->dest_p , -1 ,
434- nullptr , v8::Local<v8::Value>());
456+ channels = GetFSOperationChannels (binding, env, req_wrap->syscall_p );
457+ if (channels != nullptr && FSOperationChannelHasSubscribers (
458+ *channels, FSOperationChannel::kStart )) {
459+ PublishFSOperationEvent (env,
460+ *channels,
461+ FSOperationChannel::kStart ,
462+ " sync" ,
463+ req_wrap->path_p ,
464+ req_wrap->dest_p ,
465+ -1 ,
466+ nullptr ,
467+ v8::Local<v8::Value>());
435468 }
436469 }
437470 int result = fn (nullptr , &(req_wrap->req ), args..., nullptr );
@@ -449,9 +482,15 @@ int SyncCallAndThrowIf(Predicate should_throw,
449482 nullptr ,
450483 req_wrap->path_p ,
451484 req_wrap->dest_p );
452- PublishFSOperationEvent (env, *channels, FSOperationChannel::kError ,
453- " sync" , req_wrap->path_p , req_wrap->dest_p ,
454- fd, " error" , error);
485+ PublishFSOperationEvent (env,
486+ *channels,
487+ FSOperationChannel::kError ,
488+ " sync" ,
489+ req_wrap->path_p ,
490+ req_wrap->dest_p ,
491+ fd,
492+ " error" ,
493+ error);
455494 }
456495 } else if (FSOperationChannelHasSubscribers (*channels,
457496 FSOperationChannel::kEnd )) {
0 commit comments