libp2p / js-libp2p

The JavaScript Implementation of libp2p networking stack.
https://libp2p.github.io/js-libp2p/
Other
2.33k stars 446 forks source link

UnhandledPromiseRejectionWarning related to StreamHandler #679

Closed oed closed 3 years ago

oed commented 4 years ago

Type: Bug

Severity: Medium

Description:

We're seeing an unhandled promise rejection in the libp2p circuit code. The error looks like this:

(node:21) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'type' of undefined
at ClassIsWrapper._onProtocol (/3box-pinning-server/node_modules/libp2p/src/circuit/index.js:45:21)
(node:21) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 5)

Looking into the libp2p code it seems to be because request variable is undefined: https://github.com/libp2p/js-libp2p/blob/0.27.x/src/circuit/index.js#L45 Observing the StreamHandler it appears that if msg.value is undefined the method will not reach a return statement, which is likely what is causing the error: https://github.com/libp2p/js-libp2p/blob/0.27.x/src/circuit/circuit/stream-handler.js#L31-L42

Looks like this code is the same in 0.28.x.

Steps to reproduce the error:

Unfortunately I don't yet know how to reproduce the issue. I've seen it a few times in our production environment.

Not really sure how sever this might be (we are currently investigating some service degradation, but this might be unrelated).

jacobheun commented 4 years ago

Circuit needs some improved error handling, I'm seeing a few other areas we need to be catching errors.

Not sure on the underlying issues yet. Since msg.value is undefined the read stream should have ended, so there might be a stream being opened without any request being send.

geolffreym commented 4 years ago

same here.. @jacobheun any updates about this issue? Thanks!!

(node:90237) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'type' of undefined
    at ClassIsWrapper._onProtocol (.../node_modules/libp2p/src/circuit/index.js:46:21)
    at runMicrotasks (<anonymous>)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)

i dont really know if that issue cause event.js error because throw me throw er; // Unhandled 'error' event after Rejection:

events.js:288
      throw er; // Unhandled 'error' event
      ^

Error: connect EHOSTUNREACH 2a01:4f8:172:6c8::2:4002 - Local (:::63082)
    at internalConnect (net.js:917:16)
    at defaultTriggerAsyncIdScope (internal/async_hooks.js:311:12)
    at net.js:1007:9
    at processTicksAndRejections (internal/process/task_queues.js:79:11)
Emitted 'error' event on Socket instance at:
    at emitErrorNT (internal/streams/destroy.js:92:8)
    at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
    at processTicksAndRejections (internal/process/task_queues.js:84:21) {
  errno: 'EHOSTUNREACH',
  code: 'EHOSTUNREACH',
  syscall: 'connect',
  address: '2a01:4f8:172:6c8::2',
  port: 4002
}

CC @vasco-santos

vasco-santos commented 3 years ago

This second issue should be fixed by https://github.com/libp2p/js-libp2p-websockets/pull/118

vasco-santos commented 3 years ago

With the upcoming libp2p@0.31 this will be fixed as with the types work the optional request value is validated before crashing on type of undefined. Relevant change on: https://github.com/libp2p/js-libp2p/blob/v0.31.0-rc.2/src/circuit/transport.js#L61

vasco-santos commented 3 years ago

This should be fixed with the new released we just fixed 🎉 If it is not the case, please reopen the issue