Commit 88b034fa751 for nodejs
commit 88b034fa7519bbf61ecf4257881a40ad08c89954
Author: Ethan Arrowood <ethan@arrowood.dev>
Date: Thu Nov 19 10:13:16 2026 -0700
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>
diff --git a/src/quic/http3.cc b/src/quic/http3.cc
index 0f9afc9a9cd..d77accd10cd 100644
--- a/src/quic/http3.cc
+++ b/src/quic/http3.cc
@@ -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,
diff --git a/test/parallel/test-quic-h3-reset-after-headers.mjs b/test/parallel/test-quic-h3-reset-after-headers.mjs
new file mode 100644
index 00000000000..1f1d7bced60
--- /dev/null
+++ b/test/parallel/test-quic-h3-reset-after-headers.mjs
@@ -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();
+}