Skip to content

Commit 7041a45

Browse files
committed
quic: fix edge cases where stream FIN would be lost
Signed-off-by: Tim Perry <pimterry@gmail.com>
1 parent 12ce819 commit 7041a45

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
@@ -2077,27 +2077,26 @@ void Session::SendPendingData() {
20772077
closed = true;
20782078
return;
20792079
}
2080-
} else if (stream_data.id >= 0) {
2080+
} else if (ndatalen < 0 && nwrite >= 0 && stream_data.id >= 0) {
20812081
Debug(this,
20822082
"Session did not accept any bytes from stream %" PRIi64
20832083
" into packet",
20842084
stream_data.id);
2085+
// ndatalen < 0 means this stream's frame (data or FIN) was left out of
2086+
// the packet, due to congestion/pacing (nwrite == 0) or because other
2087+
// frames filled it. Reschedule the stream so it is retried later.
2088+
// This calls Application::ResumeStream directly (not
2089+
// Session::ResumeStream) to avoid creating a SendPendingDataScope -
2090+
// we're already inside SendPendingData.
2091+
if (stream_data.count > 0 || stream_data.fin) {
2092+
application().ResumeStream(stream_data.id);
2093+
}
20852094
}
20862095

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

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)