Skip to content

Commit 3649ec9

Browse files
quic: destroy stream on invalid promise body
When `setBody()` receives a promise that resolves to an unsupported body type, `configureOutbound()` throws inside the fulfillment handler. The rejection handler only covered the original promise, so the error rejected a discarded promise and crashed the process as an unhandled rejection. Chain the rejection handler after the fulfillment handler so both cases destroy the stream with the error. For an unsupported resolved value, `stream.closed` now rejects with the same `ERR_INVALID_ARG_TYPE` that `setBody()` throws synchronously. Signed-off-by: Christian Aurich Zanettini Martins <christian.aurichzm@gmail.com>
1 parent 24b9ca9 commit 3649ec9

2 files changed

Lines changed: 44 additions & 2 deletions

File tree

‎lib/internal/quic/quic.js‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1376,8 +1376,11 @@ function configureOutbound(handle, stream, body) {
13761376
// so the resolved value will never itself be a promise.
13771377
if (isPromise(body)) {
13781378
PromisePrototypeThen(
1379-
body,
1380-
(resolved) => configureOutbound(handle, stream, resolved),
1379+
PromisePrototypeThen(
1380+
body,
1381+
(resolved) => configureOutbound(handle, stream, resolved),
1382+
),
1383+
undefined,
13811384
(err) => {
13821385
if (!stream.destroyed) {
13831386
stream.destroy(err);
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Flags: --experimental-quic --no-warnings
2+
3+
// Test: body: Promise resolving to an invalid body destroys the stream.
4+
// When the body is a Promise that resolves to an unsupported type, the
5+
// stream should be destroyed with the same ERR_INVALID_ARG_TYPE that
6+
// setBody() throws synchronously for that type.
7+
8+
import { hasQuic, skip, mustCall } from '../common/index.mjs';
9+
import assert from 'node:assert';
10+
11+
if (!hasQuic) {
12+
skip('QUIC is not enabled');
13+
}
14+
15+
const { listen, connect } = await import('../common/quic.mjs');
16+
17+
const serverEndpoint = await listen(mustCall(async (serverSession) => {
18+
await serverSession.closed;
19+
}), { transportParams: { maxIdleTimeout: 5 } });
20+
21+
const clientSession = await connect(serverEndpoint.address, {
22+
transportParams: { maxIdleTimeout: 5 },
23+
});
24+
await clientSession.opened;
25+
26+
const stream = await clientSession.createBidirectionalStream();
27+
28+
assert.throws(() => stream.setBody(42), { code: 'ERR_INVALID_ARG_TYPE' });
29+
30+
const stream2 = await clientSession.createBidirectionalStream();
31+
32+
const closedPromise = assert.rejects(stream2.closed, {
33+
code: 'ERR_INVALID_ARG_TYPE',
34+
});
35+
36+
stream2.setBody(Promise.resolve(42));
37+
38+
await Promise.all([closedPromise, clientSession.closed]);
39+
await serverEndpoint.close();

0 commit comments

Comments
 (0)