Running node index.js, the output is empty. Using $ DEBUG=*err* node index.js the ouput does not contain the "YO" error.
example output
$ DEBUG=*err* node index.js
QmfHQdZ737fhQ8exhrcnWmGjqV4dEg2nc9eUb3ftoGg3sM discovered: Qmekcj3NdtxRqoZ82TNjVUcHDMTEKFiQh91CQuoxuhjm6a
(node:8740) ExperimentalWarning: Readable[Symbol.asyncIterator] is an experimental feature. This feature could change at any time
libp2p:upgrader:error Failed to upgrade outbound connection { Error: The operation was aborted
at nextAbortHandler (/home/mh-cbon/projects/libp2p/node_modules/abortable-iterator/index.js:37:20)
at AbortSignal.abortHandler (/home/mh-cbon/projects/libp2p/node_modules/abortable-iterator/index.js:16:29)
at AbortSignal.dispatchEvent (/home/mh-cbon/projects/libp2p/node_modules/event-target-shim/dist/event-target-shim.js:818:35)
at abortSignal (/home/mh-cbon/projects/libp2p/node_modules/abort-controller/dist/abort-controller.js:52:12)
at AbortController.abort (/home/mh-cbon/projects/libp2p/node_modules/abort-controller/dist/abort-controller.js:91:9)
at AbortSignal.onAbort (/home/mh-cbon/projects/libp2p/node_modules/any-signal/index.js:13:16)
at AbortSignal.dispatchEvent (/home/mh-cbon/projects/libp2p/node_modules/event-target-shim/dist/event-target-shim.js:818:35)
at abortSignal (/home/mh-cbon/projects/libp2p/node_modules/abort-controller/dist/abort-controller.js:52:12)
at AbortController.abort (/home/mh-cbon/projects/libp2p/node_modules/abort-controller/dist/abort-controller.js:91:9)
at dialAbortControllers.map.c (/home/mh-cbon/projects/libp2p/node_modules/libp2p/src/dialer/dial-request.js:74:39) type: 'aborted', code: 'ERR_MUXER_UNAVAILABLE' } +0ms
libp2p:upgrader:error Failed to upgrade outbound connection { Error: The operation was aborted
at nextAbortHandler (/home/mh-cbon/projects/libp2p/node_modules/abortable-iterator/index.js:37:20)
at AbortSignal.abortHandler (/home/mh-cbon/projects/libp2p/node_modules/abortable-iterator/index.js:16:29)
at AbortSignal.dispatchEvent (/home/mh-cbon/projects/libp2p/node_modules/event-target-shim/dist/event-target-shim.js:818:35)
at abortSignal (/home/mh-cbon/projects/libp2p/node_modules/abort-controller/dist/abort-controller.js:52:12)
at AbortController.abort (/home/mh-cbon/projects/libp2p/node_modules/abort-controller/dist/abort-controller.js:91:9)
at AbortSignal.onAbort (/home/mh-cbon/projects/libp2p/node_modules/any-signal/index.js:13:16)
at AbortSignal.dispatchEvent (/home/mh-cbon/projects/libp2p/node_modules/event-target-shim/dist/event-target-shim.js:818:35)
at abortSignal (/home/mh-cbon/projects/libp2p/node_modules/abort-controller/dist/abort-controller.js:52:12)
at AbortController.abort (/home/mh-cbon/projects/libp2p/node_modules/abort-controller/dist/abort-controller.js:91:9)
at dialAbortControllers.map.c (/home/mh-cbon/projects/libp2p/node_modules/libp2p/src/dialer/dial-request.js:74:39) type: 'aborted', code: 'ERR_MUXER_UNAVAILABLE' } +4ms
libp2p:upgrader:error could not create new stream { Error: read ECONNRESET
at TCP.onread (net.js:656:25) errno: 'ECONNRESET', code: 'ECONNRESET', syscall: 'read' } +13ms
libp2p:identify:error Error: read ECONNRESET
libp2p:identify:error at TCP.onread (net.js:656:25) +0ms
libp2p:upgrader:error could not create new stream { Error: read ECONNRESET
at TCP.onread (net.js:656:25) errno: 'ECONNRESET', code: 'ECONNRESET', syscall: 'read' } +1ms
libp2p:identify:error Error: read ECONNRESET
libp2p:identify:error at TCP.onread (net.js:656:25) +0ms
You can try adding node1.on("error", console.error);, the output does not contain the error.
Solution
The problem is located in libp2p.emit method. When it emits over super, it does not catch errors, they are lost.
Using below patch
when emit fails with an exception, the error is re sent over this.emit to possibly be redirected to the logger (there is risk of circular call...) or over super.
I can run $ DEBUG=*err* node index.js and get:
$ DEBUG=*err* node index.js
QmWUqo3Yd4hzZnfH2M7psEFK1Du8DwCWKk1N1V9JxXBkP6 discovered: QmWa2Df6cPANNrATs1E5juNd66cqeVpeE1eqsXLueLZKrt
libp2p:error YO +0ms
If i add an event listener to node.error, i get
$ DEBUG=*err* node index.js
QmVhdYZaiDpMnHMhB6ZXTsmCPH5MUjpqxNF1cFVMR9vrAq discovered: QmfV2F1Gpbj9TxXTFv5TsR9YPrsKUgZtiQR4e6Zqny5kV9
YO
(node:9121) ExperimentalWarning: Readable[Symbol.asyncIterator] is an experimental feature. This feature could change at any time
libp2p:upgrader:error Failed to upgrade outbound connection { Error: The operation was aborted
at nextAbortHandler (/home/mh-cbon/projects/libp2p/node_modules/abortable-iterator/index.js:37:20)
at AbortSignal.abortHandler (/home/mh-cbon/projects/libp2p/node_modules/abortable-iterator/index.js:16:29)
at AbortSignal.dispatchEve...
But also $ node index.js and get:
$ node index.js
QmRCHQ6eVGCTfC6a2cXyACDAcBQWYVvCh3preCB9RtzCd6 discovered: QmbZgTiWQixYwRMN2xAfUNaZAbnVDkG6xbpZNZ83bFmzG6
YO
(node:9203) ExperimentalWarning: Readable[Symbol.asyncIterator] is an experimental feature. This feature could change at any time
^C
Finally, i would like to get your attention to the special event EventEmitter.errorMonitor as described in https://nodejs.org/api/events.html#events_error_events. I believe the constructor should install such listener to forward all errors to the logger. In such case i guess the libp2p.emit method wont need the logic to redirect errors on the logger and that the catch block should not re emit over this, but directly over super.
Version:
Platform: node / fedora
Subsystem: libp2p (core)
Type: Bug
Severity: Medium
Description: Errors generated by event handlers attached to a node created with
libp2p.create
are not printed on stderr.Steps to reproduce the error:
Using this script
Running
node index.js
, the output is empty. Using$ DEBUG=*err* node index.js
the ouput does not contain the "YO" error.example output
You can try adding
node1.on("error", console.error);
, the output does not contain the error.Solution
The problem is located in
libp2p.emit
method. When it emits over super, it does not catch errors, they are lost. Using below patchwhen emit fails with an exception, the error is re sent over this.emit to possibly be redirected to the logger (there is risk of circular call...) or over super.
I can run
$ DEBUG=*err* node index.js
and get:If i add an event listener to node.error, i get
But also
$ node index.js
and get:Finally, i would like to get your attention to the special event EventEmitter.errorMonitor as described in https://nodejs.org/api/events.html#events_error_events. I believe the constructor should install such listener to forward all errors to the logger. In such case i guess the
libp2p.emit
method wont need the logic to redirect errors on the logger and that the catch block should not re emit over this, but directly over super.