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: catch unhandled promise rejection in WebTransport muxer #2566

Merged
merged 1 commit into from
May 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 45 additions & 31 deletions packages/transport-webtransport/src/muxer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,38 +23,42 @@

const activeStreams: Stream[] = []

void Promise.resolve().then(async () => {
//! TODO unclear how to add backpressure here?
while (true) {
const { done, value: wtStream } = await reader.read()
Promise.resolve()
.then(async () => {
//! TODO unclear how to add backpressure here?
while (true) {
const { done, value: wtStream } = await reader.read()

if (done) {
break
}
if (done) {
break
}

if (activeStreams.length >= config.maxInboundStreams) {
log(`too many inbound streams open - ${activeStreams.length}/${config.maxInboundStreams}, closing new incoming stream`)
// We've reached our limit, close this stream.
wtStream.writable.close().catch((err: Error) => {
log.error(`failed to close inbound stream that crossed our maxInboundStream limit: ${err.message}`)
})
wtStream.readable.cancel().catch((err: Error) => {
log.error(`failed to close inbound stream that crossed our maxInboundStream limit: ${err.message}`)
})
} else {
const stream = await webtransportBiDiStreamToStream(
wtStream,
String(streamIDCounter++),
'inbound',
activeStreams,
init?.onStreamEnd,
logger
)
activeStreams.push(stream)
init?.onIncomingStream?.(stream)
if (activeStreams.length >= config.maxInboundStreams) {
log(`too many inbound streams open - ${activeStreams.length}/${config.maxInboundStreams}, closing new incoming stream`)
// We've reached our limit, close this stream.
wtStream.writable.close().catch((err: Error) => {
log.error(`failed to close inbound stream that crossed our maxInboundStream limit: ${err.message}`)
})
wtStream.readable.cancel().catch((err: Error) => {
log.error(`failed to close inbound stream that crossed our maxInboundStream limit: ${err.message}`)
})

Check warning on line 44 in packages/transport-webtransport/src/muxer.ts

View check run for this annotation

Codecov / codecov/patch

packages/transport-webtransport/src/muxer.ts#L37-L44

Added lines #L37 - L44 were not covered by tests
} else {
const stream = await webtransportBiDiStreamToStream(
wtStream,
String(streamIDCounter++),
'inbound',
activeStreams,
init?.onStreamEnd,
logger
)
activeStreams.push(stream)
init?.onIncomingStream?.(stream)
}
}
}
})
})
.catch(err => {
log.error('could not create a new stream', err)

Check warning on line 60 in packages/transport-webtransport/src/muxer.ts

View check run for this annotation

Codecov / codecov/patch

packages/transport-webtransport/src/muxer.ts#L60

Added line #L60 was not covered by tests
})

const muxer: StreamMuxer = {
protocol: 'webtransport',
Expand All @@ -74,15 +78,25 @@
*/
close: async () => {
log('closing webtransport muxer gracefully')
wt.close()

try {
wt.close()
} catch (err: any) {
muxer.abort(err)
}

Check warning on line 86 in packages/transport-webtransport/src/muxer.ts

View check run for this annotation

Codecov / codecov/patch

packages/transport-webtransport/src/muxer.ts#L85-L86

Added lines #L85 - L86 were not covered by tests
},

/**
* Abort all tracked streams and stop the muxer
*/
abort: (err: Error) => {
log('closing webtransport muxer with err:', err)
wt.close()

try {
wt.close()
} catch (err: any) {
log.error('webtransport session threw error during close', err)
}

Check warning on line 99 in packages/transport-webtransport/src/muxer.ts

View check run for this annotation

Codecov / codecov/patch

packages/transport-webtransport/src/muxer.ts#L94-L99

Added lines #L94 - L99 were not covered by tests
},

// This stream muxer is webtransport native. Therefore it doesn't plug in with any other duplex.
Expand Down
Loading