Files
node/test/parallel/test-quic-shared-endpoint-stream-close.mjs
Ethan Arrowood c3189d436e test: replace discard loops with dump()
Replaces the `for await (const _ of source) {}` discard idiom with
dump(), which says what it does and does not need an eslint-disable
for the unused loop variable. The eslint-disable directives and
comments that become redundant are removed with it.

Only files that already declare --experimental-stream-iter are
converted, so no test gains an experimental flag it did not already
opt into. Files still using the old idiom are left as they are;
converting them would change what those tests run under and belongs
in a separate discussion.

Loops that break early are left alone: those cancel the source rather
than reading it to completion, which is not what dump() does.

Assisted-by: Claude Opus 5
Signed-off-by: Ethan Arrowood <ethan@arrowood.dev>
PR-URL: https://github.com/nodejs/node/pull/65598
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Tim Perry <pimterry@gmail.com>
2026-10-07 20:01:31 +00:00

96 lines
3.0 KiB
JavaScript

// Flags: --experimental-quic --experimental-stream-iter --no-warnings
// Regression test: when a client QuicEndpoint has a session terminated
// by a stateless reset, subsequent sessions on the same endpoint must
// be able to complete their stream close handshake.
// Without the fix, the server-side stream.closed for session 2 never
// resolves and the test hangs.
import { hasQuic, skip, mustCall } from '../common/index.mjs';
import assert from 'node:assert';
import { dump } from 'node:stream/iter';
if (!hasQuic) {
skip('QUIC is not enabled');
}
const { listen, connect } = await import('../common/quic.mjs');
const { bytes } = await import('stream/iter');
const { QuicEndpoint } = await import('node:quic');
const encoder = new TextEncoder();
let sessionCount = 0;
const serverDone1 = Promise.withResolvers();
const serverDone2 = Promise.withResolvers();
const serverEndpoint = await listen(mustCall((serverSession) => {
sessionCount++;
const which = sessionCount;
serverSession.onstream = mustCall(async (stream) => {
const data = await bytes(stream);
assert.ok(data.byteLength > 0);
stream.writer.endSync();
// For session 2 this hangs when the bug is present — the server
// never receives the client's ACK for its FIN.
await stream.closed;
if (which === 1) serverSession.destroy();
(which === 1 ? serverDone1 : serverDone2).resolve();
});
}, 2), {
onerror(err) { /* marks promises as handled */ },
});
// Both sessions share one endpoint — same source UDP address.
const clientEndpoint = new QuicEndpoint();
// Session 1: complete a full round-trip, then the server destroys
// without sending CONNECTION_CLOSE. The client sends a packet to the
// now-unknown DCID, which causes the server to send a stateless reset.
// The client receives the stateless reset and closes session 1.
const client1 = await connect(serverEndpoint.address, {
endpoint: clientEndpoint,
verifyPeer: 'manual',
onerror: mustCall((err) => { assert.ok(err); }),
});
await client1.opened;
const s1 = await client1.createBidirectionalStream({
body: encoder.encode('session1'),
});
await dump(s1);
await s1.closed;
await serverDone1.promise;
// Trigger the stateless reset.
// eslint-disable-next-line no-unused-vars
const s1b = await client1.createBidirectionalStream({
body: encoder.encode('trigger'),
});
await assert.rejects(client1.closed, { code: 'ERR_QUIC_TRANSPORT_ERROR' });
// Session 2: uses the same endpoint as session 1. The bug manifests
// as serverDone2 never resolving because the server's stream.closed
// for session 2 hangs.
const client2 = await connect(serverEndpoint.address, {
endpoint: clientEndpoint,
verifyPeer: 'manual',
onerror(err) { /* marks promises as handled */ },
});
await client2.opened;
const s2 = await client2.createBidirectionalStream({
body: encoder.encode('session2'),
});
await dump(s2);
await s2.closed;
// If the bug is present, this never resolves and the test hangs.
await serverDone2.promise;
await serverEndpoint.close();
await clientEndpoint.close();