Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: WebRTC transport unhandled promise rejection during connect #2299

Merged
merged 6 commits into from
Dec 5, 2023

Conversation

achingbrain
Copy link
Member

@achingbrain achingbrain commented Dec 5, 2023

Simplifies the connection logic to just use the abort signal to abort the dial instead of the abort signal and multiple deferred promises.

Fixes a crash in node where the abort signal passed into a WebRTC private to private dial can cause an unhandled promise rejection due to a race condition between the abort firing and awaiting the connectedPromise promise:

file:///Users/alex/Documents/Workspaces/ipfs/helia-http-gateway/node_modules/@libp2p/webrtc/dist/src/private-to-private/initiate-connection.js:42
            connectedPromise.reject(new CodeError('SDP handshake aborted', 'ERR_SDP_HANDSHAKE_ABORTED'));
                                    ^

CodeError: SDP handshake aborted
    at EventTarget.sdpAbortedListener (file:///Users/alex/Documents/Workspaces/ipfs/helia-http-gateway/node_modules/@libp2p/webrtc/dist/src/private-to-private/initiate-connection.js:42:37)
    at [nodejs.internal.kHybridDispatch] (node:internal/event_target:807:20)
    at EventTarget.dispatchEvent (node:internal/event_target:742:26)
    at abortSignal (node:internal/abort_controller:369:10)
    at AbortController.abort (node:internal/abort_controller:391:5)
    at EventTarget.onAbort (file:///Users/alex/Documents/Workspaces/ipfs/helia-http-gateway/node_modules/any-signal/dist/src/index.js:8:20)
    at [nodejs.internal.kHybridDispatch] (node:internal/event_target:807:20)
    at EventTarget.dispatchEvent (node:internal/event_target:742:26)
    at abortSignal (node:internal/abort_controller:369:10)
    at AbortController.abort (node:internal/abort_controller:391:5) {
  code: 'ERR_SDP_HANDSHAKE_ABORTED',
  props: {}
}

Node.js v20.8.0

Change checklist

  • I have performed a self-review of my own code
  • I have made corresponding changes to the documentation if necessary (this includes comments as well)
  • I have added tests that prove my fix is effective or that my feature works

Fixes a crash in node where the abort signal passed into a WebRTC
private to private dial would cause an unhandled promise rejection:

```
file:///Users/alex/Documents/Workspaces/ipfs/helia-http-gateway/node_modules/@libp2p/webrtc/dist/src/private-to-private/initiate-connection.js:42
            connectedPromise.reject(new CodeError('SDP handshake aborted', 'ERR_SDP_HANDSHAKE_ABORTED'));
                                    ^

CodeError: SDP handshake aborted
    at EventTarget.sdpAbortedListener (file:///Users/alex/Documents/Workspaces/ipfs/helia-http-gateway/node_modules/@libp2p/webrtc/dist/src/private-to-private/initiate-connection.js:42:37)
    at [nodejs.internal.kHybridDispatch] (node:internal/event_target:807:20)
    at EventTarget.dispatchEvent (node:internal/event_target:742:26)
    at abortSignal (node:internal/abort_controller:369:10)
    at AbortController.abort (node:internal/abort_controller:391:5)
    at EventTarget.onAbort (file:///Users/alex/Documents/Workspaces/ipfs/helia-http-gateway/node_modules/any-signal/dist/src/index.js:8:20)
    at [nodejs.internal.kHybridDispatch] (node:internal/event_target:807:20)
    at EventTarget.dispatchEvent (node:internal/event_target:742:26)
    at abortSignal (node:internal/abort_controller:369:10)
    at AbortController.abort (node:internal/abort_controller:391:5) {
  code: 'ERR_SDP_HANDSHAKE_ABORTED',
  props: {}
}

Node.js v20.8.0
```

Simplifies the connection logic to just use the abort signal to
abort the dial instead of the abort signal and multiple deferred
promises.
@achingbrain achingbrain requested a review from a team as a code owner December 5, 2023 12:20
Copy link
Member

@maschad maschad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The refactor makes sense , but are you able to simulate this race condition?

@achingbrain
Copy link
Member Author

I've added a test that fails against main and also causes a PromiseRejectionHandledWarning to be logged:

(node:24480) PromiseRejectionHandledWarning: Promise rejection was handled asynchronously (rejection id: 33)
(Use `node --trace-warnings ...` to show where the warning was created)

@achingbrain achingbrain merged commit 64a915a into main Dec 5, 2023
22 checks passed
@achingbrain achingbrain deleted the fix/webrtc-unhandled-promise-rejection branch December 5, 2023 18:00
achingbrain added a commit that referenced this pull request Dec 6, 2023
We catch WebRTC errors, log them and throw normalised errors to ensure they are the same across WebRTC stacks (FF, Chrome, node).

#2299 made one of these operations async which would cause an UHPR if thrown.
This was referenced Jan 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants