Skip to content

quic: fix fix h3 session close on early str. destr - #67706

Open
martenrichter wants to merge 2 commits into
nodejs:mainfrom
martenrichter:fixfix-quic-h3-reset-after-headers
Open

martenrichter wants to merge 2 commits into
nodejs:mainfrom
martenrichter:fixfix-quic-h3-reset-after-headers

Conversation

@martenrichter

Copy link
Copy Markdown
Contributor

This fixes the fix from the PR #67446.
The previous PR did not allow already submitted
data, such as a Webtransport close capsule to be
transmitted to ngtcp2.
This PR instead uses the nullptr reader,
to detect a closed stream and signals eof.

This is of course only an intermediate solution,
instead quic RESET_AT semantics are a far better
solution.

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/quic

@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. needs-ci PRs that need a full CI run. quic Issues and PRs related to the QUIC transport implementation. labels Nov 22, 2026
@martenrichter

martenrichter commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor Author

This addresses a problem in my webtransport PR, that the PR #67446 essential was discarding all data already written into the stream. In my case the webtransport close capsule.

I think this solution should address the issue of the underlying PR without breaking the data to be send.
Still we should have on our todo list to change the reset logic, with the new RESET_AT semantics, which I have seen to be at least in nghttp3/ngtcp2 main, I do not know if it is released yet. This can certainly change the tear-down for good.

@Ethan-Arrowood I hope you don't mind that I have to change sth here; I was very puzzled about what changed after rebasing.
I also ping @pimterry and @jasnell, who reviewed the initial PR.

P.S: I do not know how to test it without webtransport. You need a write to the stream within the same tick of closing it.

This fixes the fix from the PR nodejs#67446.
The previous PR did not allow already submitted
data, such as a Webtransport close capsule to be
transmitted to ngtcp2.
This PR instead uses the nullptr reader,
to detect a closed stream and signals eof.

This is of course only an intermediate solution,
instead quic RESET_AT semantics are a far better
solution.

Signed-off-by: Marten Richter <marten.richter@freenet.de>
@martenrichter
martenrichter force-pushed the fixfix-quic-h3-reset-after-headers branch from 93427c5 to 82db30b Compare November 22, 2026 17:53
@jasnell jasnell added the request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. label Nov 22, 2026
@github-actions github-actions Bot removed the request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. label Nov 22, 2026
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@codecov

codecov Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.54%. Comparing base (ce44266) to head (87211e6).
⚠️ Report is 113 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #67706      +/-   ##
==========================================
- Coverage   90.60%   90.54%   -0.07%     
==========================================
  Files         801      803       +2     
  Lines      281056   285094    +4038     
  Branches    54253    55028     +775     
==========================================
+ Hits       254660   258145    +3485     
- Misses      16577    17097     +520     
- Partials     9819     9852      +33     

see 80 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@Ethan-Arrowood Ethan-Arrowood left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All good! You definitely know this code better than I do. I think your approach of flushing what is queued and then sending FIN is better overall. There is one path still broken though: non-terminal sendHeaders() followed by destroy() without a code makes the request hang, because nothing ends the stream. This comes from the return NGHTTP3_DATA_FLAG_EOF; part.

Here is a test case; it hangs on main, crashes on the PR as is, and passes with the suggested fix.

test-quic-h3-destroy-after-headers.mjs:

// 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();

If you want to add this test as a new test case file on this pr that might be good.

Note, I did have an agent assist with this review, but independently verified everything myself plus these words are all my own per my human voice policy!

@martenrichter

Copy link
Copy Markdown
Contributor Author

All good! You definitely know this code better than I do.

I am not sure, I still do not have a complete understanding on the nghttp3 interface. But the external webtransport test knew better.

I think your approach of flushing what is queued and then sending FIN is better overall.

But it is still a bit patchy, anyway for now it is good.

Thanks for the test I have added it.

Co-authored-by: Ethan Arrowood <ethan@arrowood.dev>
@martenrichter
martenrichter force-pushed the fixfix-quic-h3-reset-after-headers branch from 941c108 to 87211e6 Compare November 24, 2026 06:53
@martenrichter

Copy link
Copy Markdown
Contributor Author

Now, I see what @Ethan-Arrowood meant. It was not crashing on my windows build though, that will be a though one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

c++ Issues and PRs that require attention from people who are familiar with C++. needs-ci PRs that need a full CI run. quic Issues and PRs related to the QUIC transport implementation.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants