Skip to content

http2: DATA or HEADERS after a request's END_STREAM resets its stream with STREAM_CLOSED, and HEADERS without END_STREAM on a body-reader request with PROTOCOL_ERROR (#615) - #626

Closed
MDA2AV wants to merge 1 commit into
mainfrom
fix-615

Conversation

@MDA2AV

@MDA2AV MDA2AV commented Nov 14, 2026 •

Copy link
Copy Markdown
Owner

Fixes #615.

What was wrong

RFC 9113 5.1: once a request's END_STREAM has arrived its stream is half-closed (remote), and any frame on it other than WINDOW_UPDATE, PRIORITY or RST_STREAM is a stream error of STREAM_CLOSED; 6.1 says the same of DATA. RFC 9113 8.1: a HEADERS without END_STREAM after the block that opens a request makes the request malformed, a stream error of PROTOCOL_ERROR (8.1.1).

ioxide.http2 checked neither in these cases:

  • Buffered request (a handler without a body reader). The request leaves _streams at its END_STREAM, and its answer's window waits in _responseWindows until the answer has ended. DATA on it reached the not-found branch of HandleData, which dropped it, and credited it back with a stream WINDOW_UPDATE when it did not end the stream. HEADERS on it was decoded and dropped as a closed stream's.
  • Body-reader request. A HEADERS after its END_STREAM took the BodyReader branch of HandleHeaders, which decodes trailers aside and keeps only their END_STREAM, so it was dropped. So was a HEADERS without END_STREAM while the body was still arriving, which the buffered path already resets with PROTOCOL_ERROR.

Reproduced at main over a real h2c connection: a scratch program served Http2Connection on a test server and wrote each case below to a raw socket in one write, so every frame lands in one read, followed by a GET on stream 3.

Frames on stream 1 main this PR
GET, then DATA with END_STREAM answered RST_STREAM STREAM_CLOSED
GET, then DATA without END_STREAM WINDOW_UPDATE +4, answered RST_STREAM STREAM_CLOSED
GET, then HEADERS answered RST_STREAM STREAM_CLOSED
GET, then HEADERS and CONTINUATION answered RST_STREAM STREAM_CLOSED
body reader: POST, DATA with END_STREAM, then HEADERS answered RST_STREAM STREAM_CLOSED
body reader: POST, DATA, HEADERS without END_STREAM, DATA with END_STREAM answered, the last DATA read as more body RST_STREAM PROTOCOL_ERROR
control, body reader: POST, DATA, trailers answered answered
control: a GET alone answered answered

Stream 3 was answered in every case. The new unit tests fail at main the same way.

What changes

src/protocols/ioxide.http2/Http2Connection.Frames.cs only.

  • HandleData: DATA on a stream that is answering but no longer in _streams resets it with STREAM_CLOSED. Without a body reader such a stream has always ended its request (see below for the body-reader case).
  • DiscardHeaderBlock: once a block decoded aside is whole, CONTINUATION included, it resets its stream when the stream cannot take it: with STREAM_CLOSED when the stream is answering out of _streams, or, for a stream still in _streams, when its HEADERS marked it.
  • HandleHeaders, BodyReader branch: a block after the request's END_STREAM, or one without END_STREAM, sets the request's existing Malformed flag before the block is decoded aside, and the block's END_HEADERS resets the stream: STREAM_CLOSED once the request has ended, PROTOCOL_ERROR while it is still arriving. Only trailers that end the request still end its body.
  • All three reset through ResetAnswering, the three steps the window-overrun and content-length resets take: the answer's window leaves _responseWindows, ResetStream, then ReleaseCreditWaiters.

Each of these resets waits until its header block has been decoded whole, so HPACK stays in step and a block split over CONTINUATION is reset at its END_HEADERS. Nothing new is kept: the decision reads _streams, _responseWindows, and the request's RequestEnded and Malformed.

With a body reader, main retires a request whose buffered answer is parked on credit while its upload may still be arriving: RetireStream takes it out of _streams and keeps its window. On such a connection a stream answering out of _streams may still be uploading, so both new checks there apply only without a body reader, and those streams keep main's handling: DATA is credited, HEADERS dropped. #604 keeps such a request in _streams, after which the gate can go (see Overlaps).

What a caller sees

  • A client that sends DATA or HEADERS on a stream after its END_STREAM, while the answer is still being produced or sent, gets RST_STREAM(STREAM_CLOSED) on that stream. A buffered answer is not written; a streamed writer's next FlushAsync reports IsCompleted, as after the peer's own reset; an answer parked on credit stops and frees its writer without waiting for credit the client will not send. The connection serves on.
  • A body-reader handler whose request gets a HEADERS without END_STREAM while its body arrives reads an IOException (body cut off) instead of more body, and its answer is not sent: RST_STREAM(PROTOCOL_ERROR).
  • A body-reader handler that had read its body to the end when a block came after it reads an empty end again; one whose body was still queued is cut off, as at any reset.
  • Late DATA without END_STREAM on a buffered stream no longer gets a stream WINDOW_UPDATE.
  • Frames that arrive once the answer has ended still find a closed stream, and are dropped (decoded first, for a header block), as before.

Tests

tests/Ioxide.Tests.Unit/Http2StreamStateTests.cs, 14 tests over the strict test pipe. Each sends stream 1's request, the late frames, then a GET on stream 3, and checks every frame the server wrote:

  1. DATA after a buffered request's END_STREAM, in the same read / while its handler is parked (2 tests): the request ended by its HEADERS, its last DATA or its trailers, times DATA with END_STREAM, without, and empty. Only stream 1 is reset, with STREAM_CLOSED, nothing else goes out on it, and stream 3 is answered.
  2. HEADERS after a buffered request's END_STREAM, same read / parked (2 tests): the same three endings, times HEADERS with END_STREAM, without, and split over CONTINUATION. The late block inserts a field into HPACK's dynamic table and stream 3's request refers to it, so a block skipped rather than decoded ends the connection with COMPRESSION_ERROR instead.
  3. HEADERS split over CONTINUATION, buffered and with a body reader: no reset until the CONTINUATION that ends the block.
  4. HEADERS after a body-reader request's END_STREAM, same read / while its handler runs (2 tests): STREAM_CLOSED, HPACK in step, and a handler that had read its body reads an empty end again.
  5. HEADERS without END_STREAM while a body-reader request's body arrives, same read / while its handler runs (2 tests), one frame or split: PROTOCOL_ERROR, the handler's read raises, the DATA after the reset is neither read nor credited, HPACK in step.
  6. A streamed answer, without a body reader (late DATA, late HEADERS) and with one (late HEADERS): its next flush reports completed, and stream 1 carries HEADERS, DATA, RST_STREAM and nothing after.
  7. A buffered answer parked on its stream window, late DATA and late HEADERS: its writer is back in the pool at once, and the client's later WINDOW_UPDATE sends nothing more on stream 1.
  8. A streamed answer parked on its stream window, late HEADERS on a body-reader request: the flush wakes and reports completed without credit.
  9. Control: DATA and HEADERS after the answer has ended draw no reset and no credit, and HPACK stays in step.
  10. Control: with a body reader, an upload behind a buffered answer parked on credit still ends with its trailers, unreset, and the answer completes once credited.

Each part of the fix reverted on its own, with the Unit suite rebuilt and rerun (the first failing check of each test quoted):

Reverted Unit result Fails with
the whole fix (main's Http2Connection.Frames.cs) 265 passed, 12 failed 1-8, e.g. DATA with END_STREAM came after a request's END_STREAM on its HEADERS, and the server wrote []; 9 and 10 pass
the reset in HandleData 4 failed 1 (both), 6 DATA, without a body reader came after the request's END_STREAM, and the handler's next flush: flush not completed, 7 DATA came after the request's END_STREAM, and its parked answer was left waiting on credit
the not-found reset in DiscardHeaderBlock 5 failed 2 (both), 3 buffered, and the server wrote [], 6 (HEADERS, without a body reader), 7 (HEADERS)
marking a body-reader block after END_STREAM 5 failed 3 with a body reader, and the server wrote [], 4 (both), 6 (HEADERS, with a body reader), 8 expected [read ended 'aaaaa', flush completed], got [never got past its flush]
marking a body-reader HEADERS without END_STREAM 2 failed 5 (both) one HEADERS without END_STREAM came while the body arrived, and the server wrote []
_responseWindows.Remove in ResetAnswering 12 failed the answer goes out after the reset: stream 1 carried [RST_STREAM STREAM_CLOSED, HEADERS, DATA]
ReleaseCreditWaiters in ResetAnswering 2 failed 7 left waiting on credit, 8 got [never got past its flush]
the body-reader gate in HandleData 2 failed the existing h2 early answer: an upload still arriving while a buffered answer is parked on credit is credited on its stream (expected [16384], got [0]), 10 drew [1 STREAM_CLOSED]
the body-reader gate in DiscardHeaderBlock 1 failed 10 an upload that ended with its trailers drew [1 STREAM_CLOSED]
resetting at the HEADERS frame instead of at its block's END_HEADERS 1 failed 3 buffered, the stream was reset with [1 STREAM_CLOSED] before its block's CONTINUATION came (with a body reader, ... with only that path changed)

Suites

Built pinned with dotnet build -c Release ioxide.slnx, each suite run alone:

Suite Result
Unit 277 passed, 0 failed, 0 skipped (main's 263 and the 14 new)
E2E 730 passed, 0 failed, 0 skipped
Chaos 105 passed, 0 failed, 0 skipped

Http, Tls, File, Pg and Redis were not run locally: none of them reaches Http2Connection's frame handling, the only code changed. CI runs them all.

Overlaps

git merge-tree --write-tree origin/fix-<n> HEAD against every open fix branch: exit 0 for fix-581, fix-583 to fix-587 and fix-589 to fix-592; exit 1 for fix-588 only.

Of the issues being fixed in parallel, #614 (the preface flush and the first read, in Http2Connection.cs and Http2Connection.Write.cs) and #617 (Http2ResponseWriter.EnsureStaging) also touch ioxide.http2, in files this PR leaves alone. The branches already pushed for the others (fix-605 to fix-609, fix-611) merge-tree cleanly with this one.

For the reviewer

  • Only frames that arrive while the answer is under way are reset. Once the answer has ended the server keeps nothing for the stream, so a later frame cannot be told from one after its own RST_STREAM, which 5.1 says to ignore; the issue leaves that case out, and so does this PR. h2spec's http2/5.1 half-closed (remote) cases can therefore pass only when the late frame lands before the answer has gone out; h2spec is not on this box, so they were not run.
  • "Under way" means the answer's window is still in _responseWindows. A streamed answer, and a buffered rest parked on credit, give it up only once the flush carrying their END_STREAM has completed, so a late frame landing during that flush is reset just behind the END_STREAM. A scratch test (not committed) holds the flush of a streamed answer that completes outside the read pass and sends DATA without END_STREAM meanwhile: this PR writes HEADERS, DATA END_STREAM, RST_STREAM STREAM_CLOSED on the stream, main HEADERS, DATA END_STREAM, WINDOW_UPDATE +5. http2: DATA after a request's END_STREAM resets its stream with STREAM_CLOSED instead of reading as more of its body (#589) #599 has the same window for DATA on a body-reader request.
  • Because a block is decoded whole first, a split block whose answer ends between its HEADERS and its END_HEADERS finds the stream closed by then, and is dropped like any closed stream's block.
  • The body-reader gate means that until http2: an answer waiting on the client's credit has its unread upload credited, so a client that uploads everything first gets it (#588) #604 lands, a body-reader connection whose buffered answer is parked on credit still credits DATA, and drops HEADERS, that arrive after its request has ended. Main cannot tell those from the upload still arriving, because it has already recycled the request.
  • The buffered path's existing PROTOCOL_ERROR for a HEADERS without END_STREAM still resets at that HEADERS frame, ahead of a CONTINUATION that ends its block. HPACK stays in step there too, since the CONTINUATION still reaches the decoder; marking the request Malformed instead would move that reset to the END_HEADERS as well. Left as it is.
  • Malformed gains a second use: on a body-reader request already being served it marks a block that request cannot take, so that the block's END_HEADERS resets the stream. That is the only state that has to outlive a HEADERS split over CONTINUATION, and nothing else reads Malformed once the request has been dispatched.

… with STREAM_CLOSED, and HEADERS without END_STREAM on a body-reader request with PROTOCOL_ERROR (#615)

A request's stream is half-closed (remote) once its END_STREAM has arrived, and RFC 9113 5.1 makes any frame on it other than WINDOW_UPDATE, PRIORITY or RST_STREAM a stream error of STREAM_CLOSED; 6.1 says the same of DATA. A buffered request leaves _streams at its END_STREAM, and its answer's window waits in _responseWindows until the answer has ended. HandleData dropped DATA it found only there, and credited it back with a stream WINDOW_UPDATE when it did not end the stream; HandleHeaders decoded such a HEADERS and dropped it as a closed stream's. A body-reader request stays in _streams, and a HEADERS after its END_STREAM was decoded aside and dropped like trailers. So was a HEADERS without END_STREAM while its body still arrived, which 8.1 makes malformed, a stream error of PROTOCOL_ERROR, and which the buffered path already resets.

Those frames now reset the stream as a window overrun does: the answer's window goes, the stream is reset, and a writer parked on its credit wakes. A buffered answer is not written, a streamed writer's next flush reports the peer gone, and a rest parked on credit stops and frees its writer. A header block is decoded whole first, across its CONTINUATION frames, so HPACK stays in step: a body-reader request's HEADERS marks the stream, and the block's END_HEADERS resets it. Frames on a stream whose answer has ended are still dropped, as a closed stream's.

With a body reader, a buffered answer parked on credit leaves _streams while its request may still be arriving, so on such a connection DATA and HEADERS found only in _responseWindows keep their old handling.

Http2StreamStateTests (Unit): DATA (ending the stream, not ending it, empty) and HEADERS (ending it, not ending it, split over CONTINUATION) after a buffered request's END_STREAM on its HEADERS, its last DATA or its trailers, in the same read and while its handler is parked, reset only that stream with STREAM_CLOSED, send nothing else on it, keep HPACK in step and leave the connection serving; the same for HEADERS after a body-reader request's END_STREAM, whose handler reads an empty end again, and for HEADERS without END_STREAM while its body arrives, with PROTOCOL_ERROR and a read that raises. A split block is reset only at its END_HEADERS. A streamed answer's next flush reports the stream gone, and an answer parked on credit, buffered or streamed, wakes and lets go without credit. Two controls: frames after the answer has ended draw no reset or credit, and with a body reader an upload behind a buffered answer parked on credit still ends with its trailers.
@MDA2AV

MDA2AV commented Nov 15, 2026

Copy link
Copy Markdown
Owner Author

Closed: frames after a request's END_STREAM come only from a client already in error, and no real client sends them, so the extra reset paths aren't worth their code.

@MDA2AV MDA2AV closed this Nov 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

http2: DATA and HEADERS after END_STREAM on a buffered stream, and HEADERS after END_STREAM on a body-reader stream, get no STREAM_CLOSED

1 participant