fix(transport-webtransport): abort connection when session dies#3489
Open
paschal533 wants to merge 2 commits intolibp2p:mainfrom
Open
fix(transport-webtransport): abort connection when session dies#3489paschal533 wants to merge 2 commits intolibp2p:mainfrom
paschal533 wants to merge 2 commits intolibp2p:mainfrom
Conversation
…on dies When a WebTransport session becomes invalid (e.g. after laptop sleep/resume), createBidirectionalStream() throws a DOMException. Previously this error propagated unhandled and the connection was never removed from the connection manager. Three fixes: - muxer.ts onCreateStream(): catch the DOMException from createBidirectionalStream(), then call this.abort(err) and this.maConn.abort(err) before rethrowing. This triggers the full cleanup chain: sendReset() -> cleanUpWTSession() -> wt.close(), followed by a StreamAbortEvent on maConn which propagates through Connection -> upgrader -> connection manager removal. - muxer.ts incoming stream reader: call abort on both the muxer and maConn when the incomingBidirectionalStreams reader fails (same session-death scenario on the inbound path). - index.ts wt.closed handler: split .catch().finally() into .then()/.catch() so that when wt.closed rejects, cleanUpWTSession is called first (sets closed=true to prevent double wt.close()), then maConn.abort(err) fires the close event chain. Closes libp2p#1982
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When a WebTransport session becomes invalid after a network interruption or laptop sleep/resume,
connection.newStream()callscreateBidirectionalStream()on the deadWebTransportobject which throws:Three independent gaps caused the stale connection to remain in the connection manager indefinitely:
muxer.tsonCreateStream()— the DOMException fromcreateBidirectionalStream()propagated unhandled; neither the muxer nor the connection were cleaned up.muxer.tsincoming stream reader — errors from theincomingBidirectionalStreamsreader were only logged; no cleanup triggered.index.tswt.closedrejection —cleanUpWTSession()only setmaConn.timeline.close = Date.now(). This never dispatches acloseevent onmaConn, so the connection manager was never notified even whenwt.closedeventually fired.Changes
src/muxer.tsonCreateStream()— wrapcreateBidirectionalStream()in try/catch. On error, callthis.abort(err)(all open streams) andthis.maConn.abort(err)before rethrowing.maConn.abort()triggers:sendReset()→cleanUpWTSession()→wt.close(), then dispatches aStreamAbortEvent('close' event) onmaConn. This propagates throughConnection→ upgrader →connection:closeevent bus → connection manager removes the entry.Incoming stream reader — add
this.abort(err)andthis.maConn.abort(err)to the.catch()handler (same cleanup for the inbound path).src/index.tsSplit
.catch().finally()into.then()/.catch(). In the.catch(), callcleanUpWTSession('remote_close')first (setsclosed = trueto prevent a doublewt.close()call viasendReset()), thenmaConn?.abort(err)to fire the full close event chain.test/muxer.spec.ts(new)Two unit tests using sinon-ts stubs:
onCreateStreamcatches DOMException and abortsmaConnmaConnCloses #1982
Test plan
cd packages/transport-webtransport && npx aegir test -t node— both new muxer tests passnpx aegir build— no TypeScript errorsnewStream()rejects cleanly and the connection is removed from the connection manager after resume