diff --git a/src/quic/http3.cc b/src/quic/http3.cc index d77accd10cdc..4cc3c12accae 100644 --- a/src/quic/http3.cc +++ b/src/quic/http3.cc @@ -563,12 +563,9 @@ class Http3ApplicationImpl final : public Session::Application { void StreamRemoved(stream_id id) override { 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); + // Once the Stream is gone, nothing can supply its body data. + // So the on_read_data_callback should detect the nullptr user_data + // and signal EOF. nghttp3_conn_set_stream_user_data(*this, id, nullptr); } @@ -1202,6 +1199,10 @@ class Http3ApplicationImpl final : public Session::Application { CHECK_NOT_NULL(ptr); auto& app = *ptr; NgHttp3CallbackScope scope(&app.session()); + if (stream_user_data == nullptr) { + // stream is gone on js side, we exit silently + return NGHTTP3_DATA_FLAG_EOF; + } BaseObjectPtr stream(static_cast(stream_user_data)); if (!stream) [[unlikely]] { diff --git a/test/parallel/test-quic-h3-destroy-after-headers.mjs b/test/parallel/test-quic-h3-destroy-after-headers.mjs new file mode 100644 index 000000000000..f61315c2085d --- /dev/null +++ b/test/parallel/test-quic-h3-destroy-after-headers.mjs @@ -0,0 +1,79 @@ +// Flags: --experimental-quic --no-warnings + +// Test: an HTTP/3 server that submits non-terminal response headers and then +// destroys the stream without an error code still delivers the headers and +// ends the stream cleanly, and the session keeps working. Nothing resets the +// QUIC stream here, so nghttp3 must flush the queued headers and send FIN +// after the stream is removed on the JS side. + +import { hasQuic, skip, mustCall, mustNotCall } 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'); + +function request(session, path) { + return session.createBidirectionalStream({ + headers: { + ':method': 'GET', + ':path': path, + ':scheme': 'https', + ':authority': 'localhost', + }, + }); +} + +const serverEndpoint = await listen(mustCall((serverSession) => { + serverSession.onerror = () => {}; +}), { + sni: { '*': { keys: [key], certs: [cert] } }, + onheaders: mustCall(function(headers) { + const abandon = headers[':path'] === '/abandon'; + this.sendHeaders({ ':status': '200' }, { terminal: !abandon }); + if (abandon) { + // Destroy in the same tick, before nghttp3 has written the headers, + // and without an error code, so no RESET_STREAM is sent. + this.onerror = () => {}; + this.destroy(); + } + }, 2), +}); + +const clientSession = await connect(serverEndpoint.address, { + servername: 'localhost', + verifyPeer: 'manual', +}); +await clientSession.opened; + +// The abandoned request still gets its headers and a clean end of stream. +const abandoned = await request(clientSession, '/abandon'); +abandoned.onreset = mustNotCall(); +const abandonedStatus = Promise.withResolvers(); +abandoned.onheaders = mustCall((headers) => { + abandonedStatus.resolve(headers[':status']); +}); +assert.strictEqual(await abandonedStatus.promise, 200); +let bytes = 0; +for await (const chunks of abandoned) { + for (const chunk of chunks) bytes += chunk.byteLength; +} +assert.strictEqual(bytes, 0); +await abandoned.closed; + +// 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); +await ok.closed; + +await clientSession.close(); +await serverEndpoint.close();