Repository navigation
quic: fix fix h3 session close on early str. destr - #67706
martenrichter wants to merge 4 commits into
Conversation
|
Review requested:
|
|
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. @Ethan-Arrowood I hope you don't mind that I have to change sth here; I was very puzzled about what changed after rebasing. 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>
93427c5 to
82db30b
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #67706 +/- ##
==========================================
- Coverage 90.60% 90.55% -0.06%
==========================================
Files 801 803 +2
Lines 281056 285126 +4070
Branches 54253 55040 +787
==========================================
+ Hits 254660 258183 +3523
- Misses 16577 17103 +526
- Partials 9819 9840 +21 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
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!
I am not sure, I still do not have a complete understanding on the nghttp3 interface. But the external webtransport test knew better.
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>
941c108 to
87211e6
Compare
|
Now, I see what @Ethan-Arrowood meant. It was not crashing on my windows build though, that will be a though one. |
Ethan-Arrowood
left a comment
There was a problem hiding this comment.
My bad; I thought I submitted this suggested change along with the comment about the test. Honestly I blame GH ui haha. Anyways, sorry about the confusion; this is the change thats needed to go along with my last comment.
Co-authored-by: Ethan Arrowood <ethan@arrowood.dev>
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.