Repository navigation
Conversation
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'.
|
Warning Review paused — included plan limit reachedKeep your review moving with free on-demand reviews.
On-demand reviews are free for one more day.
Reviews can continue after your included limit without a manual trigger. An admin must approve usage-based billing. Promotion and pricing detailsOn-demand reviews are free for one more day. After that, they cost $0.25 per reviewed file. Review limit detailsOr wait 3 minutes for your next included review. Limit details: You’ve used all 10 included reviews currently available. Review configuration: ⚙️ Run configurationConfiguration used: Repository: oven-sh/bun/.coderabbit.yaml Review profile: ASSERTIVE Plan: Essentials Run ID: 📒 Files selected for processing (4)
Comment |
|
Updated 6:34 PM PT - Sep 19th, 2026
❌ @robobun, your commit b691a66 has 3 failures in
🧪 To try this PR locally: bunx bun-pr 43558That installs a local version of the PR into your bun-43558 --bun |
|
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):
CI (build 118693, b691a66, finished): the diff is green. 178 of 181 jobs passed, and 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. |
…) 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.
There was a problem hiding this comment.
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 norefuses 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
closedguard inrespond()is itself unchanged from base, but the PR description claims this PR adds it plus arefuses respond()test, and neither is in the diff; the newWritableClosedgate now turns the sequence into a clean, complete response) — triggered whenever a server'stream'handler callsstream.close(...)and thenstream.respond(...)on a… | normal —…
…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.
|
On the |
|
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. |
Problem
node:http2client accepts DATA before any response HEADERS, and the bytes become the body. A raw server that sendsDATA("early") HEADERS(200) DATA("tail")gives body"earlytail". node v26.3.0 ends the session withGOAWAY(PROTOCOL_ERROR).handle_dataandbegin_streamed_data(src/runtime/api/bun/h2/connection.rs) never checkrecv_final_headers.close(),destroy()orend()beforerespond()writesDATA(END_STREAM)with no HEADERS, becauseHttp2Stream#_final(src/js/node/http2.ts) always writes the empty end frame.Fix
fetch()HTTP/2 client does the same (src/http/h2_client/dispatch.rs:435)._finalwrites nothing before HEADERS, and a laterrespond()puts END_STREAM on its HEADERS frame.close()anddestroy()then send only RST_STREAM, andrespond()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'.test/js/node/http2/h2-conformance.test.ts(17 new tests, 16 fail on main). Also the 261 vendoredtest-http2-*files,test/js/node/http2/, grpc-js, the fetch HTTP/2 tests and theBun.serveHTTP/2 tests.Background
recv_final_headersbecomes true when a non-1xx HEADERS block completes._finalis thestream.Writablehook that runs afterend().close()anddestroy()callend(), 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(unexpectedNGHTTP2_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 toonStreamError, which destroys the stream on the next tick. Two effects on a client on main:'close'without'end'(node:end,close).test-http2-compat-write-head-after-close.jswaits for that'end'once the server sends a bare RST_STREAM(0).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'beforedestroy(). The server role keeps the old path (destroy on the next tick).The writable side does not
end()at that point, unlike node'scloseStream. node raisesERR_STREAM_WRITE_AFTER_ENDfor 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._destroyfails the held write withERR_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 gets413plus a body plus RST_STREAM(NO_ERROR), with a late reader and no'error'listener:respond()on a closed stream. With the_finalchange,close(code); respond()would send HEADERS with END_STREAM and lose the reset.respond(),respondWithFile()andrespondWithFD()now throwERR_HTTP2_INVALID_STREAMon a closed stream, like node. The two error branches ofdoSendFileFDcallrespond()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 sameclosedchecks and the same_finallogic (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:DATA(END_STREAM)with no HEADERS forclose(),destroy(),res.destroy()andend()beforerespond(). Measured against a bun 1.4.3-canary server that destroys one stream while other requests run on the same session:fetch()HTTP/2 client already resets the stream with PROTOCOL_ERROR whenstatus_code == 0, and keeps the session.push stream statesinh2-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):
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(), compatres.destroy()RST_STREAM(0)DATA(END_STREAM)RST_STREAM(0)end()DATA(END_STREAM)end(); respond()HEADERS 0x5DATA(END_STREAM) HEADERS 0x4HEADERS 0x5additionalHeaders(103); end(); respond()HEADERS HEADERS 0x5HEADERS DATA(END_STREAM) HEADERS 0x4HEADERS HEADERS 0x5session.destroy()GOAWAYGOAWAY DATA(END_STREAM)GOAWAYclose(); respond()RST_STREAM(0)DATA(END_STREAM) HEADERS 0x4 RST_STREAM(0)RST_STREAM(0)respond(); end(),end("hi"),cork(); write(); end()A bare
end()with norespond()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), emptyDATA(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 (thebegin_streamed_datapath) and for DATA after only a 103 block.Snapshot change.
re-entrant read() from a trailer-value toStringloses itsreq error ERR_HTTP2_STREAM_CANCELline. The fixture feeds the client RST_STREAM(NO_ERROR) and then callsclient.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 andsignalCode.Review history. The first push routed a NO_ERROR reset to
onStreamEndon both roles and calledend()on the writable side. The review showed that a paused server stream with an empty buffer then never closed, and that a write betweenend()and'finish'raised an uncaughtERR_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. Theclosedchecks left in the second push and are back with thedoSendFileFDguard. The DATA admission rule moved into one helper,Stream::data_refusal.Three existing gaps that this PR can reach in a new way:
session.close()waits. On main a cancelled request with its response in flight does the same. node:http2: don't track client streams opened by inbound HEADERS #42369 fixes it.once("end", destroySelfOnEnd)branch that a stream reset with NO_ERROR also takes, so it covers that stream too.stream.statethrowsInvalid stream idon a closed stream that still has unread data, after the next inbound read. On main a response that ends with END_STREAM and is not read shows it (node returns the state object). node:http2: make the stream lifecycle getters total, like node #41945 fixes the same throw forendAfterHeadersonly. No PR coversstateyet.pushStream(headers, { endStream: true })followed by a plainrespond()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. Wholeh2-conformance.test.ts: 87 pass. Vendoredtest-http2-*: 261 of 261 pass.test/js/node/http2/: 593 pass, 0 fail.test/js/web/fetch/fetch-http2-client.test.tsand the threetest/js/bun/http/serve-http2*.test.tsfiles pass. grpc-js: the failures need DNS or crates.io, exceptOutlier 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:864has a comment that says Bun's server sends an empty DATA frame forstream.close(code). That is no longer true after this PR. The fetch client's branch for DATA before the final HEADERS stays covered byDATA after only a 1xx HEADERS is a stream PROTOCOL_ERROR.