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(@libp2p/webtransport): be more thorough about closing sessions #1969

Merged
merged 1 commit into from
Aug 15, 2023

Conversation

achingbrain
Copy link
Member

Refactors session closing to happen in one function and call that function when the session has closed or failed to init.

Doesn't quite solve the "Too many pending WebTransport Sessions" problem but does slow it down a little bit.

Refs: #1896

Refactors session closing to happen in one function and call that
function when the session has closed or failed to init.

Doesn't quite solve the "Too many pending WebTransport Sessions"
problem but does slow it down a little bit.

Refs: #1896
@SgtPooki SgtPooki self-requested a review August 15, 2023 16:07
Copy link
Member

@SgtPooki SgtPooki left a comment

Choose a reason for hiding this comment

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

definitely an improvement

}))
})

cleanUpWTSession = (closeInfo?: WebTransportCloseInfo) => {
Copy link
Member

Choose a reason for hiding this comment

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

why define this function inside dial instead of making it a method on the webtransportTransport class?

Copy link
Member Author

Choose a reason for hiding this comment

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

It needed access to a lot of scoped variables, and it's passed to the stream muxer so it got a bit fiddly.

Comment on lines +146 to +164
maConn = {
close: async (options?: AbortOptions) => {
log('Closing webtransport')
cleanUpWTSession()
},
abort: (err: Error) => {
log('aborting webtransport due to passed err', err)
cleanUpWTSession({
closeCode: 0,
reason: err.message
})
},
remoteAddr: ma,
timeline: {
open: Date.now()
},
// This connection is never used directly since webtransport supports native streams.
...inertDuplex()
}
Copy link
Member

Choose a reason for hiding this comment

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

if we make this a property on the class we can split out this dial function a bit

Comment on lines +114 to +128
// this promise resolves/throws when the session is closed or failed to init
wt.closed
.then(async () => {
await maConn?.close()
})
.catch((err: Error) => {
log.error('error on remote wt session close', err)
maConn?.abort(err)
})
.finally(() => {
// if we never got as far as creating the maConn, just clean up the session
if (maConn == null) {
cleanUpWTSession()
}
})
Copy link
Member

Choose a reason for hiding this comment

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

I believe this should cover the case where await wt.ready throws, but from my testing, I'm not confident the .closed catch always handles errors thrown from wt.ready.

Copy link
Member Author

Choose a reason for hiding this comment

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

I think this is an oddity of the clean up procedure - .ready and .closed are treated separately.

@achingbrain achingbrain merged commit 90e793e into master Aug 15, 2023
17 checks passed
@achingbrain achingbrain deleted the fix/close-webtransport-sessions branch August 15, 2023 16:59
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.

2 participants