quic: fix h3 session close on early stream destroy

If the stream is destroyed right after sendHeaders(), nghttp3 still has
the response headers queued. On the next write it asks for the removed
stream's body, the read callback fails, and the whole session closes
without notifying the peer.

In an HTTP/3 session, before clearing nghttp3's pointer to a stream when
the stream is destroyed, shut down nghttp3's writes for that stream so
that other requests can continue to complete.

Assisted-by: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Ethan Arrowood <ethan@arrowood.dev>
PR-URL: https://github.com/nodejs/node/pull/67446
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Tim Perry <pimterry@gmail.com>
This commit is contained in:
Ethan Arrowood authored and Antoine du Hamel committed 2026-11-25 18:00:32 +01:00
1 parent b31c1bcf2f
commit 25ba2e8bae
2 files changed
+94 -1

No files matched your search

+8 -1
View File
@@ -562,7 +562,14 @@ class Http3ApplicationImpl final : public Session::Application {
}
void StreamRemoved(stream_id id) override {
if (conn_) nghttp3_conn_set_stream_user_data(*this, id, nullptr);
if (!conn_) return;
// Once the Stream is gone, nothing can supply its body data. Stop
// nghttp3 from writing to it. Otherwise a response whose headers were
// submitted but not yet sent makes nghttp3 call on_read_data_callback
// for a stream that no longer exists, which fails and closes the
// whole session.
nghttp3_conn_shutdown_stream_write(*this, id);
nghttp3_conn_set_stream_user_data(*this, id, nullptr);
}
bool SendHeaders(Stream& stream,
@@ -0,0 +1,86 @@
// Flags: --experimental-quic --no-warnings
// Test: an HTTP/3 server that abandons a stream right after submitting
// response headers delivers RESET_STREAM to the client, and the session
// keeps working. The headers are still queued in nghttp3 when the stream
// is destroyed. If nghttp3 later tries to write them, reading the body of
// the removed stream fails and the whole session closes silently.
import { hasQuic, skip, mustCall } from '../common/index.mjs';
import assert from 'node:assert';
import * as fixtures from '../common/fixtures.mjs';
if (!hasQuic) {
skip('QUIC is not enabled');
}
const { listen, connect } = await import('node:quic');
const { createPrivateKey } = await import('node:crypto');
const key = createPrivateKey(fixtures.readKey('agent1-key.pem'));
const cert = fixtures.readKey('agent1-cert.pem');
// RFC 9114 H3_REQUEST_CANCELLED.
const H3_REQUEST_CANCELLED = 0x10cn;
const cases = [
['resetStream() then destroy()', (stream) => {
stream.resetStream(H3_REQUEST_CANCELLED);
stream.destroy();
}],
['destroy() with a code', (stream) => {
stream.onerror = () => {};
stream.destroy(new Error('abandoned'), { code: H3_REQUEST_CANCELLED });
}],
];
function request(session, path) {
return session.createBidirectionalStream({
headers: {
':method': 'GET',
':path': path,
':scheme': 'https',
':authority': 'localhost',
},
});
}
for (const [label, abandon] of cases) {
const serverEndpoint = await listen(mustCall((serverSession) => {
serverSession.onerror = () => {};
}), {
sni: { '*': { keys: [key], certs: [cert] } },
onheaders: mustCall(function(headers) {
// Submit response headers, then abandon the first request in the
// same tick, before nghttp3 has written the headers.
this.sendHeaders({ ':status': '200' }, {
terminal: headers[':path'] !== '/abandon',
});
if (headers[':path'] === '/abandon') abandon(this);
}, 2),
});
const clientSession = await connect(serverEndpoint.address, {
servername: 'localhost',
verifyPeer: 'manual',
});
await clientSession.opened;
// The first request is abandoned with H3_REQUEST_CANCELLED.
const abandoned = await request(clientSession, '/abandon');
abandoned.onerror = () => {};
const reset = Promise.withResolvers();
abandoned.onreset = mustCall((error) => reset.resolve(error));
const error = await reset.promise;
assert.strictEqual(error?.errorCode, H3_REQUEST_CANCELLED, label);
// The session still works: a second request gets a response.
const ok = await request(clientSession, '/ok');
const status = Promise.withResolvers();
ok.onheaders = mustCall((headers) => status.resolve(headers[':status']));
assert.strictEqual(await status.promise, 200, label);
await ok.closed;
await clientSession.close();
await serverEndpoint.close();
}