Skip to content

Commit 5d896c7

Browse files
authored
fix(http2): keep flow-controlled end-stream DATA alive until it is flushed (#1998) (#2007)
> [!NOTE] > This PR was generated by an AI coding agent (Jetski) on behalf of @mosuem. ### Summary Fixes #1998: `sendData(bytes, endStream: true)` with more bytes than the peer's flow-control window stalled forever, because the stream was torn down before the flow-controlled remainder could be sent. Two things went wrong: 1. In `StreamMessageQueueOut.enqueueMessage`, `if (message.endStream) startClosing()` ran *before* `_messages.addLast(message)`. `startClosing()` synchronously calls `onCheckForClose()`, which saw an empty `_messages` and closed the queue while the `DataMessage` was still waiting for `WINDOW_UPDATE` credit. 2. In `StreamHandler._handleEndOfStreamRemote`, when the peer sent `END_STREAM` on a stream that was already `HalfClosedLocal` (the local side had called `sendData(..., endStream: true)`), the stream was removed from `_openStreams` immediately, even if `stream.outgoingQueue` still had `DATA` waiting on credit. Subsequent stream-level `WINDOW_UPDATE` frames from the peer were then ignored (RFC 9113 Section 6.9: a `WINDOW_UPDATE` on a closed stream is discarded), so the remaining data could never be sent. ### Changes - **`lib/src/flowcontrol/stream_queues.dart`**: Call `startClosing()` after `_messages.addLast(message)`, so `onCheckForClose()` only closes the queue once `_messages` has drained. - **`lib/src/streams/stream_handler.dart`**: In `_handleEndOfStreamRemote`, only remove the stream from `_openStreams` if `stream.outgoingQueue.wasClosed`; otherwise `_cleanupClosedStream` removes it once the outgoing queue has drained. ### Test Verification (Fails Before $\rightarrow$ Passes After) Added `send-data-exceeding-window-with-end-stream-completes` in `test/transport_test.dart` (server and client each send 100 000 bytes with `endStream: true` through a 65 535-byte window; the second case has the server respond with `END_STREAM` before the upload has drained). **Before fix:** ```text 00:05 +0 -1: transport-test flow-control send-data-exceeding-window-with-end-stream-completes [E] TimeoutException after 0:00:05.000000: Test timed out after 5 seconds. ``` **After fix:** ```text 00:00 +1: All tests passed! ```
1 parent c834aa0 commit 5d896c7

4 files changed

Lines changed: 62 additions & 8 deletions

File tree

‎pkgs/http2/CHANGELOG.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
- Ignore `PRIORITY` and unknown extension frames on open, idle, and closed
1515
streams, and treat `SETTINGS`, `PING`, and `GOAWAY` frames with a non-zero
1616
stream ID as a connection-level `PROTOCOL_ERROR`.
17+
- Fix `sendData(..., endStream: true)` stalling forever when the data exceeds
18+
the peer's flow-control window (#1998).
1719
- Bound peer-initiated concurrent streams and reject peers that ignore the
1820
advertised `SETTINGS_MAX_CONCURRENT_STREAMS` value. Reserved peer streams
1921
are also bounded locally. The default limit is 100.

‎pkgs/http2/lib/src/flowcontrol/stream_queues.dart‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,12 @@ class StreamMessageQueueOut extends Object
7171
void enqueueMessage(Message message) {
7272
if (message is! ResetStreamMessage) ensureNotClosingSync(() {});
7373
if (!wasTerminated) {
74-
if (message.endStream) startClosing();
75-
7674
if (message is DataMessage) {
7775
toBeWrittenBytes += message.bytes.length;
7876
}
7977

8078
_messages.addLast(message);
79+
if (message.endStream) startClosing();
8180
_trySendData();
8281

8382
if (_messages.isNotEmpty) {

‎pkgs/http2/lib/src/streams/stream_handler.dart‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -891,12 +891,13 @@ class StreamHandler extends Object with TerminatableMixin, ClosableMixin {
891891
_changeState(stream, StreamState.HalfClosedRemote);
892892
} else if (stream.state == StreamState.HalfClosedLocal) {
893893
_changeState(stream, StreamState.Closed);
894-
// TODO: We have to make sure that we
895-
// - remove the stream for data structures which only care about the
896-
// state
897-
// - keep the stream in data structures which need to be emptied
898-
// (e.g. MessageQueues which are not empty yet).
899-
_openStreams.remove(stream.id);
894+
// Only remove the stream from [_openStreams] once all outgoing messages
895+
// have been sent; if outgoing DATA is still waiting on flow-control
896+
// credit, keep the stream registered so incoming WINDOW_UPDATE frames
897+
// reach [stream.windowHandler].
898+
if (stream.outgoingQueue.wasClosed) {
899+
_openStreams.remove(stream.id);
900+
}
900901
} else {
901902
throw StateError(
902903
'Got an end-of-stream from the remote end, but this stream is '

‎pkgs/http2/test/transport_test.dart‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,58 @@ void main() {
627627
) async {
628628
await testWindowSize(client, server, 8096);
629629
}, clientSettings: const ClientSettings(streamWindowSize: 8096));
630+
631+
transportTest('send-data-exceeding-window-with-end-stream-completes', (
632+
ClientTransportConnection client,
633+
ServerTransportConnection server,
634+
) async {
635+
final uploadReceived = Completer<void>();
636+
server.incomingStreams.listen((ServerTransportStream s) async {
637+
var requestBytes = 0;
638+
String? path;
639+
await for (final m in s.incomingMessages) {
640+
if (m is HeadersStreamMessage && path == null) {
641+
path = String.fromCharCodes(m.headers.first.value);
642+
if (path == '/download') {
643+
s.sendHeaders([Header.ascii(':status', '200')]);
644+
s.sendData(Uint8List(100000), endStream: true);
645+
} else if (path == '/early-response-upload') {
646+
s.sendHeaders([
647+
Header.ascii(':status', '200'),
648+
], endStream: true);
649+
}
650+
} else if (m is DataStreamMessage) {
651+
requestBytes += m.bytes.length;
652+
}
653+
}
654+
if (path == '/early-response-upload') {
655+
expect(requestBytes, 100000);
656+
uploadReceived.complete();
657+
}
658+
});
659+
660+
// 1. Server sendData(> 65535, endStream: true)
661+
final download = client.makeRequest([
662+
Header.ascii(':path', '/download'),
663+
], endStream: true);
664+
var downloadBytes = 0;
665+
await for (final m in download.incomingMessages) {
666+
if (m is DataStreamMessage) downloadBytes += m.bytes.length;
667+
}
668+
expect(downloadBytes, 100000);
669+
670+
// 2. Client sendData(> 65535, endStream: true) when server sends
671+
// END_STREAM before the upload finishes draining.
672+
final upload = client.makeRequest([
673+
Header.ascii(':path', '/early-response-upload'),
674+
]);
675+
upload.sendData(Uint8List(100000), endStream: true);
676+
await upload.incomingMessages.drain<void>();
677+
await uploadReceived.future;
678+
679+
await client.finish();
680+
await server.finish();
681+
}, serverSettings: const ServerSettings(concurrentStreamLimit: 1));
630682
});
631683
});
632684
}

0 commit comments

Comments
 (0)