Skip to content

Commit 8eae7e4

Browse files
pimterryaduh95
authored andcommitted
quic: fix edge cases where stream FIN would be lost
Signed-off-by: Tim Perry <pimterry@gmail.com> PR-URL: #66759 Reviewed-By: Efe Karasakal <hi@efe.dev> Reviewed-By: Ethan Arrowood <ethan@arrowood.dev> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 73f7796 commit 8eae7e4

2 files changed

Lines changed: 68 additions & 12 deletions

File tree

‎src/quic/session.cc‎

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2083,27 +2083,26 @@ void Session::SendPendingData() {
20832083
closed = true;
20842084
return;
20852085
}
2086-
} else if (stream_data.id >= 0) {
2086+
} else if (ndatalen < 0 && nwrite >= 0 && stream_data.id >= 0) {
20872087
Debug(this,
20882088
"Session did not accept any bytes from stream %" PRIi64
20892089
" into packet",
20902090
stream_data.id);
2091+
// ndatalen < 0 means this stream's frame (data or FIN) was left out of
2092+
// the packet, due to congestion/pacing (nwrite == 0) or because other
2093+
// frames filled it. Reschedule the stream so it is retried later.
2094+
// This calls Application::ResumeStream directly (not
2095+
// Session::ResumeStream) to avoid creating a SendPendingDataScope -
2096+
// we're already inside SendPendingData.
2097+
if (stream_data.count > 0 || stream_data.fin) {
2098+
application().ResumeStream(stream_data.id);
2099+
}
20912100
}
20922101

20932102
// When nwrite is zero, it means we are congestion limited or it is
2094-
// just not our turn to send something. Re-schedule the stream if it
2095-
// had unsent data (payload or FIN) so the next timer-triggered
2096-
// SendPendingData retries it. Without this, a FIN-only send that
2097-
// hits nwrite=0 is lost forever — the stream already returned EOS
2098-
// from Pull and won't be re-scheduled by anyone else.
2099-
// We call Application::ResumeStream directly (not Session::ResumeStream)
2100-
// to avoid creating a SendPendingDataScope — we're already inside
2101-
// SendPendingData and re-entering would just hit nwrite=0 again.
2103+
// just not our turn to send something.
21022104
if (nwrite == 0 && (stream_data.id >= 0 || !HasPendingDatagrams())) {
21032105
Debug(this, "Congestion or not our turn to send");
2104-
if (stream_data.id >= 0 && (stream_data.count > 0 || stream_data.fin)) {
2105-
application().ResumeStream(stream_data.id);
2106-
}
21072106
return;
21082107
}
21092108

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Flags: --experimental-quic --experimental-stream-iter --no-warnings
2+
3+
// Test: a FIN that does not fit in the current packet is still sent.
4+
//
5+
// For a fixed body the final data and the FIN go out as separate STREAM
6+
// frames, packed into the same packet where possible. When the data leaves
7+
// only a few bytes free, the FIN-only frame does not fit and the packet is
8+
// sent without it. The stream must then be retried, or the peer never sees
9+
// the end of the stream.
10+
//
11+
// Which body size leaves too little room depends on per-packet overhead, so
12+
// this sweeps sizes around one packet's payload, one stream at a time.
13+
14+
import { hasQuic, skip, mustCall } from '../common/index.mjs';
15+
import assert from 'node:assert';
16+
17+
if (!hasQuic) {
18+
skip('QUIC is not enabled');
19+
}
20+
21+
const { listen, connect } = await import('../common/quic.mjs');
22+
const { bytes } = await import('stream/iter');
23+
24+
const kMinSize = 1000;
25+
const kMaxSize = 1300;
26+
const kCount = kMaxSize - kMinSize + 1;
27+
28+
const received = [];
29+
let next = Promise.withResolvers();
30+
31+
const serverEndpoint = await listen(mustCall((serverSession) => {
32+
serverSession.onstream = mustCall(async (stream) => {
33+
const data = await bytes(stream);
34+
received.push(data.byteLength);
35+
next.resolve();
36+
}, kCount);
37+
}));
38+
39+
const clientSession = await connect(serverEndpoint.address);
40+
await clientSession.opened;
41+
const closed = clientSession.closed.then(() => 'closed', () => 'closed');
42+
43+
for (let size = kMinSize; size <= kMaxSize; size++) {
44+
const stream = await clientSession.createUnidirectionalStream();
45+
stream.setBody(new Uint8Array(size));
46+
const result = await Promise.race([next.promise.then(() => 'ok'), closed]);
47+
assert.strictEqual(result, 'ok',
48+
`session closed before the FIN of a ${size}-byte ` +
49+
'stream arrived');
50+
next = Promise.withResolvers();
51+
assert.strictEqual(received.at(-1), size);
52+
}
53+
54+
assert.strictEqual(received.length, kCount);
55+
56+
await clientSession.close();
57+
await serverEndpoint.close();

0 commit comments

Comments
 (0)