Skip to content

node:http2: never send or accept DATA before the response HEADERS - #43558

Closed
robobun wants to merge 4 commits into
mainfrom
robobun/bf746f5c/h2-client-data-before-headers
Closed

robobun wants to merge 4 commits into
mainfrom
robobun/bf746f5c/h2-client-data-before-headers

Conversation

@robobun

@robobun robobun commented Sep 19, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

  • The node:http2 client accepts DATA before any response HEADERS, and the bytes become the body. A raw server that sends DATA("early") HEADERS(200) DATA("tail") gives body "earlytail". node v26.3.0 ends the session with GOAWAY(PROTOCOL_ERROR).
  • Cause: handle_data and begin_streamed_data (src/runtime/api/bun/h2/connection.rs) never check recv_final_headers.
  • Bun's server sends that shape. close(), destroy() or end() before respond() writes DATA(END_STREAM) with no HEADERS, because Http2Stream#_final (src/js/node/http2.ts) always writes the empty end frame.

Fix

  • Client: DATA ahead of the final response HEADERS (none yet, or only 1xx) resets the stream with PROTOCOL_ERROR. Nothing is delivered. The fetch() HTTP/2 client does the same (src/http/h2_client/dispatch.rs:435).
  • It is a stream error, not nghttp2's connection error. Released Bun servers send this shape, and a connection error fails every request on the session (Notes).
  • Server: _final writes nothing before HEADERS, and a later respond() puts END_STREAM on its HEADERS frame. close() and destroy() then send only RST_STREAM, and respond() throws on a closed stream. The client keeps a stream that the server reset with NO_ERROR readable: 'aborted' if the request body was open, unread data kept, 'end' before 'close'.
  • Verified: test/js/node/http2/h2-conformance.test.ts (17 new tests, 16 fail on main). Also the 261 vendored test-http2-* files, test/js/node/http2/, grpc-js, the fetch HTTP/2 tests and the Bun.serve HTTP/2 tests.

Background

  • RFC 9113 §8.1: a response is zero or more 1xx HEADERS blocks, one final HEADERS block, then DATA. §8.1.1 makes another order a stream error (PROTOCOL_ERROR).
  • The inbound engine sees received frames only. recv_final_headers becomes true when a non-1xx HEADERS block completes.
  • _final is the stream.Writable hook that runs after end(). close() and destroy() call end(), then schedule the RST_STREAM.
Notes

Why the server change is in this PR. With only the client check, three vendored tests fail on every run: test-http2-server-rst-stream.js (rstCode 1, not the server's code), test-http2-compat-serverresponse-destroy.js (unexpected NGHTTP2_PROTOCOL_ERROR), test-http2-compat-write-head-after-close.js (hangs). Bun's client talks to Bun's server there.

RST_STREAM(NO_ERROR) from the server. on_stream_reset (src/runtime/api/bun/h2_frame_parser.rs) sends every code except CANCEL to onStreamError, which destroys the stream on the next tick. Two effects on a client on main:

  • An unread client stream emits 'close' without 'end' (node: end, close). test-http2-compat-write-head-after-close.js waits for that 'end' once the server sends a bare RST_STREAM(0).
  • RFC 9113 §8.1 lets a server answer early and then reset the stream with NO_ERROR. On main that reset discards the response body that nothing has read yet.

On a client the reset now goes to onStreamEnd(CLOSED) with a flag. The handler emits 'aborted' when the request body is still open, and the existing code waits for 'end' before destroy(). The server role keeps the old path (destroy on the next tick).

The writable side does not end() at that point, unlike node's closeStream. node raises ERR_STREAM_WRITE_AFTER_END for a write in that window, and with no 'error' listener that is an uncaught exception. A write to the reset stream is held instead (holdWriteAfterPeerReset): its callback does not run, so backpressure stops a piped source, and nothing reaches the native stream, which can already be evicted. _destroy fails the held write with ERR_STREAM_DESTROYED, which the destroyed stream does not emit as 'error'. When the readable side is already done the stream is destroyed at once, without a wait for 'finish', like node.

An upload through pipe() (200 chunks) that gets 413 plus a body plus RST_STREAM(NO_ERROR), with a late reader and no 'error' listener:

node:         UNCAUGHT ERR_STREAM_WRITE_AFTER_END
bun main:     response 413, aborted, close rstCode=0                            (body lost, 5 chunks read)
this branch:  response 413, aborted, end body="too large", close rstCode=0     (26 chunks read)

respond() on a closed stream. With the _final change, close(code); respond() would send HEADERS with END_STREAM and lose the reset. respond(), respondWithFile() and respondWithFD() now throw ERR_HTTP2_INVALID_STREAM on a closed stream, like node. The two error branches of doSendFileFD call respond() from an fs callback, so they skip it for a closed or destroyed stream.

Overlap with open PRs. #33380 has the same respond() hunk, the same closed checks and the same _final logic (the comments differ now). It also keeps the reset when HEADERS were already sent. Its RST_STREAM(NO_ERROR) hunk covers both roles and does not handle an open writable side. #38170 carried the same server fix and is closed in favor of this PR. #42467 edits the closed-stream arms next to the new client check, with a different condition.

Stream error or connection error. nghttp2 treats DATA on a stream with no response HEADERS as a connection error (session_on_data_received_fail_fast, "DATA: stream not opened"). RFC 9113 §8.1.1 requires a stream error. Reasons for the stream error:

  • Released Bun servers send DATA(END_STREAM) with no HEADERS for close(), destroy(), res.destroy() and end() before respond(). Measured against a bun 1.4.3-canary server that destroys one stream while other requests run on the same session:
    node client:  /ok 200 | /abort ERR_HTTP2_ERROR | /ok ERR_HTTP2_ERROR | /close7 ERR_HTTP2_ERROR | later /ok ERR_HTTP2_INVALID_SESSION
    bun main:     /ok 200 | /abort end, no response | /ok 200 | /close7 end, no response | later /ok 200
    this branch:  /ok 200 | /abort NGHTTP2_PROTOCOL_ERROR | /ok 200 | /close7 NGHTTP2_PROTOCOL_ERROR | later /ok 200
    
  • The fetch() HTTP/2 client already resets the stream with PROTOCOL_ERROR when status_code == 0, and keeps the session.
  • The same engine already answers DATA on a promised stream before its response HEADERS with a stream error (push stream states in h2-conformance.test.ts), where nghttp2 also ends the session.

Frames the server sends on stream 1 for a GET (raw client, node v26.3.0 / bun 1.4.3-canary / this branch):

handler node main this branch
close(2) RST_STREAM(2) DATA(END_STREAM) RST_STREAM(2)
close() RST_STREAM(0) DATA(END_STREAM) RST_STREAM(0) RST_STREAM(0)
destroy(), compat res.destroy() RST_STREAM(0) DATA(END_STREAM) RST_STREAM(0)
end() nothing DATA(END_STREAM) nothing
end(); respond() HEADERS 0x5 DATA(END_STREAM) HEADERS 0x4 HEADERS 0x5
additionalHeaders(103); end(); respond() HEADERS HEADERS 0x5 HEADERS DATA(END_STREAM) HEADERS 0x4 HEADERS HEADERS 0x5
session.destroy() GOAWAY GOAWAY DATA(END_STREAM) GOAWAY
close(); respond() throws, RST_STREAM(0) DATA(END_STREAM) HEADERS 0x4 RST_STREAM(0) throws, RST_STREAM(0)
respond(); end(), end("hi"), cork(); write(); end() unchanged

A bare end() with no respond() leaves the stream open until the session ends, as in node.

Client result for the three raw-server shapes (DATA HEADERS DATA, DATA(END_STREAM), empty DATA(END_STREAM)): 'error' ERR_HTTP2_STREAM_ERROR (NGHTTP2_PROTOCOL_ERROR), rstCode 1, no 'response', no body, RST_STREAM(PROTOCOL_ERROR) on the wire, and the session still answers a PING. The same result for a DATA frame that arrives in two socket reads (the begin_streamed_data path) and for DATA after only a 103 block.

Snapshot change. re-entrant read() from a trailer-value toString loses its req error ERR_HTTP2_STREAM_CANCEL line. The fixture feeds the client RST_STREAM(NO_ERROR) and then calls client.destroy(). On main the deferred error dispatch of that reset picked up the session's cancel error. The stream now closes cleanly before the destroy. The test still asserts the exit code and signalCode.

Review history. The first push routed a NO_ERROR reset to onStreamEnd on both roles and called end() on the writable side. The review showed that a paused server stream with an empty buffer then never closed, and that a write between end() and 'finish' raised an uncaught ERR_STREAM_WRITE_AFTER_END. The second push took the server role off that path and dropped late writes, which let a piped source drain to nothing (200 of 200 chunks in the probe above). Late writes are now held. The closed checks left in the second push and are back with the doSendFileFD guard. The DATA admission rule moved into one helper, Stream::data_refusal.

Three existing gaps that this PR can reach in a new way:

pushStream(headers, { endStream: true }) followed by a plain respond() still sends HEADERS without END_STREAM. That is the subject of #38104.

Test runs (debug + ASAN build). New describe: 17 pass. With main's src/: 16 of the 17 fail (the control test passes), and the snapshot test fails. Whole h2-conformance.test.ts: 87 pass. Vendored test-http2-*: 261 of 261 pass. test/js/node/http2/: 593 pass, 0 fail. test/js/web/fetch/fetch-http2-client.test.ts and the three test/js/bun/http/serve-http2*.test.ts files pass. grpc-js: the failures need DNS or crates.io, except Outlier detection > Success rate > Should eject a server if it is failing requests, which needs 55 to 60 s in a debug build with and without this change.

Not changed here. test/js/web/fetch/fetch-http2-client.test.ts:864 has a comment that says Bun's server sends an empty DATA frame for stream.close(code). That is no longer true after this PR. The fetch client's branch for DATA before the final HEADERS stays covered by DATA after only a 1xx HEADERS is a stream PROTOCOL_ERROR.

The client resets a stream that receives DATA ahead of its final response
HEADERS with PROTOCOL_ERROR and delivers nothing. The session stays open.

A server stream that has sent no HEADERS writes nothing from _final, and a
later respond() puts END_STREAM on its HEADERS frame. close() and destroy()
before respond() then send only RST_STREAM. respond(), respondWithFile()
and respondWithFD() throw on a closed stream.

A peer RST_STREAM(NO_ERROR) closes a stream like node: 'aborted' if the
writable side was open, the writable side ends, unread data stays readable,
and 'end' comes before 'close'.
@coderabbitai

coderabbitai Bot commented Sep 19, 2026 •

Copy link
Copy Markdown
Contributor

Warning

Review paused — included plan limit reached

Keep your review moving with free on-demand reviews.

  • Run this review for free

On-demand reviews are free for one more day.

  • Ask an admin to make reviews automatic

Open in CodeRabbit

Reviews can continue after your included limit without a manual trigger. An admin must approve usage-based billing.

Promotion and pricing details

On-demand reviews are free for one more day. After that, they cost $0.25 per reviewed file.

Review limit details

Or wait 3 minutes for your next included review.

Check out review usage here.

Limit details: You’ve used all 10 included reviews currently available.

Learn how review limits work.

Review configuration:

⚙️ Run configuration

Configuration used: Repository: oven-sh/bun/.coderabbit.yaml

Review profile: ASSERTIVE

Plan: Essentials

Run ID: 41fe864b-f819-4d44-969a-ece057606549

📥 Commits

Reviewing files that changed from the base of the PR and between 9b7c982 and b691a66.

📒 Files selected for processing (4)
  • src/js/node/http2.ts
  • src/runtime/api/bun/h2/connection.rs
  • src/runtime/api/bun/h2_frame_parser.rs
  • test/js/node/http2/h2-conformance.test.ts

Comment @coderabbitai help to get the list of available commands.

@robobun

robobun commented Sep 19, 2026 •

Copy link
Copy Markdown
Collaborator Author
Updated 6:34 PM PT - Sep 19th, 2026

❌ @robobun, your commit b691a66 has 3 failures in Build #118693 (All Failures):


🧪   To try this PR locally:

bunx bun-pr 43558

That installs a local version of the PR into your bun-43558 executable, so you can run:

bun-43558 --bun

@robobun

robobun commented Sep 19, 2026 •

Copy link
Copy Markdown
Collaborator Author

Status: ready for review. All review threads are answered and resolved.

How I reproduced it (bun 1.4.3-canary.1+367d939d9, node v26.3.0):

  • Client: a raw TCP HTTP/2 server answers stream 1 with DATA("early"), HEADERS(:status 200), DATA("tail", END_STREAM). http2.connect() plus request() gives response 200 with body earlytail. node gives a session error and sends GOAWAY(PROTOCOL_ERROR).
  • Server: a raw TCP client sends a GET to http2.createServer(). The handler calls stream.close(2) before respond(). Bun sends DATA(END_STREAM, len=0) on stream 1. node sends RST_STREAM(2).
  • test/js/node/http2/h2-conformance.test.ts, describe a response starts with HEADERS (RFC 9113 §8.1): 16 of the 17 new tests fail on main and all 17 pass on this branch.

CI (build 118693, b691a66, finished): the diff is green. 178 of 181 jobs passed, and h2-conformance.test.ts passed on every lane. The 3 failed jobs are all debian 13 x64-asan - test-bun shards. Their red tests are timeouts in code that this PR does not touch: test/js/workerd/html-rewriter-leak.test.ts, test/cli/run/require-cache.test.ts and test/js/web/timers/setInterval.test.js.

These tests fail in different combinations on other branches built in the same window (builds 118690, 118688, 118608, 118606), and they pass on main's own builds (118450). That points to load on the asan lane, not to this change. A retry of the 3 failed asan jobs in Buildkite is enough to check. I did not push a commit to rerun all 181 jobs, because every PR build in that window hit the same timeouts.

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Findings marked 🟡 are optional suggestions and need no follow-up push.

Comment thread src/js/node/http2.ts Resolved
Comment thread src/js/node/http2.ts Resolved
Comment thread src/runtime/api/bun/h2/connection.rs Resolved
Comment thread src/js/node/http2.ts Resolved
Comment thread src/js/node/http2.ts Resolved
…) the reset stream

A RST_STREAM(NO_ERROR) from a client keeps the old path on the server: the
stream is destroyed on the next tick. Only the client keeps a stream that
the server reset with NO_ERROR alive for its reader.

That client stream emits 'aborted' when its writable side is open, but the
writable side does not end(): a write() in that window would raise
ERR_STREAM_WRITE_AFTER_END. The writes are dropped, and _destroy ends the
writable side.

respond(), respondWithFile() and respondWithFD() check `destroyed` only, as
before.

The DATA admission rule of the inbound engine lives in one helper,
Stream::data_refusal.
Comment thread src/js/node/http2.ts Resolved
Comment thread src/js/node/http2.ts Resolved
Comment thread src/js/node/http2.ts Resolved
Comment thread src/js/node/http2.ts Resolved
Comment thread src/runtime/api/bun/h2/connection.rs Resolved
Comment thread src/runtime/api/bun/h2_frame_parser.rs Resolved

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Additional findings (outside the current diff — GitHub can't attach inline comments there):

  • 🟣 src/js/node/http2.ts — A server handler that calls stream.close() and then respond() now sends the client a complete, clean response instead of the ERR_HTTP2_INVALID_STREAM throw node raises. respond() at http2.ts:3515 only checks destroyed, not closed, and close() marks Closed synchronously while destroy is deferred to 'finish'. Because close() ends the writable, WritableClosed is set, so the new gate at http2.ts:3624 forces END_STREAM onto the HEADERS frame. Fix: respond(), respondWithFile() and respondWithFD() must throw ERR_HTTP2_INVALID_STREAM when this.closed is set (the three sites at http2.ts:3515, http2.ts:3388 and the respondWithFile check near http2.ts:3240), which the PR description says was added but the diff does not contain. [also at: src/js/node/http2.ts:3618 - pre-existing gap, now with a different wire result: a server that calls close(code) and then respond() now sends a complete HEADERS(END_STREAM) response before the RST_STREAM, so the client sees a successful empty 200 where node throws ERR_HTTP2_INVALID_STREAM. respond() at http2.ts:3510 checks…]

    Extended reasoning...

    The PR table claims close(); respond() throws on this branch and sends RST_STREAM(0); the diff has no closed check, and no refuses respond() test exists in the test file. Trace: handler runs stream.close(INTERNAL_ERROR) then stream.respond({':status':200}). close() at http2.ts:2518-2566 calls…

    Verification: normal (the missing closed guard in respond() is itself unchanged from base, but the PR description claims this PR adds it plus a refuses respond() test, and neither is in the diff; the new WritableClosed gate now turns the sequence into a clean, complete response) — triggered whenever a server 'stream' handler calls stream.close(...) and then stream.respond(...) on a… | normal —…

Comment thread src/js/node/http2.ts Resolved
…om respond() on a closed stream

A write to a client stream that the server reset with NO_ERROR is held, not
dropped, so backpressure stops a piped source. _destroy fails the held write
with ERR_STREAM_DESTROYED. Such a stream is destroyed without a wait for
'finish' when its readable side is done.

respond(), respondWithFile() and respondWithFD() throw ERR_HTTP2_INVALID_STREAM
on a closed stream, like node, so close(code) followed by respond() sends only
the RST_STREAM. The error branches of doSendFileFD do not call respond() on a
closed or destroyed stream.
@robobun

robobun commented Sep 19, 2026

Copy link
Copy Markdown
Collaborator Author

On the close(); respond() finding: correct, the second push removed the closed checks and the WritableClosed gate then turned that sequence into a clean response. b691a66 puts the checks back in respond(), respondWithFile() and respondWithFD(), and the two error branches of doSendFileFD skip respond() for a closed or destroyed stream, so the fstat callback cannot throw. Two new tests cover it: respond() throws on a stream that close() closed, and only the RST_STREAM goes out and respondWithFD() with a bad fd does not throw from its fstat callback when close() came first. The second one fails with an uncaught ERR_HTTP2_INVALID_STREAM without the doSendFileFD guard. The PR body describes the current state.

Comment thread src/js/node/http2.ts Resolved
Comment thread src/runtime/api/bun/h2/connection.rs Resolved
Comment thread src/js/node/http2.ts Resolved
@robobun

robobun commented Nov 17, 2026

Copy link
Copy Markdown
Collaborator Author

Closing as stale: this PR conflicts with main, and its last commit is from 2026-09-19.

The branch stays, so the work is not lost. To pick it up again, rebase the branch on main and reopen this PR.

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant