Skip to content

node:quic: send a close with code 0 as code 0, not INTERNAL_ERROR - #45702

Closed
robobun wants to merge 2 commits into
mainfrom
robobun/76dbce8d/quic-app-close-code-zero
Closed

robobun wants to merge 2 commits into
mainfrom
robobun/76dbce8d/quic-app-close-code-zero

Conversation

@robobun

@robobun robobun commented Oct 17, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #45499

Problem

  • session.close({ code: 0n, type: "application" }) and session.destroy(undefined, { code: 0n, type: "application" }) send a transport CONNECTION_CLOSE (0x1c) with INTERNAL_ERROR (0x1) and the reason "connection error". The peer's closed rejects with ERR_QUIC_TRANSPORT_ERROR errorCode=1. Node sends an application CONNECTION_CLOSE (0x1d) with code 0 and the peer's closed resolves. A transport close with code 0 and a reason has the same defect.
  • The cause is in lsquic. immediate_close() (lsquic_full_conn_ietf.c:4236) and ietf_full_conn_ci_abort_error() (:9028) read ifc_error.u.err != 0 as "an error is recorded". For code 0 the recorded error looks absent and immediate_close() falls through to CONN_ERR(0, TEC_INTERNAL_ERROR).

Fix

  • A new lsquic patch, patches/lsquic/abort-error-app-code-zero.patch, adds a recorded field to struct conn_err. The CONN_ERR macro, the only writer of ifc_error, sets it. The two readers test recorded instead of u.err != 0.
  • Correct because the connection is calloced, so recorded is 0 until an error is stored, and every store goes through CONN_ERR. The RFC 9000 section 10.2.3 rule that turns an application close into APPLICATION_ERROR in a Handshake packet is unchanged.
  • Verified: test/js/node/quic/quic-endpoint.test.ts, 8 new tests. 5 fail on bun 1.4.3 and all pass with this branch. Also test/js/node/quic/, test/js/bun/http/serve-http3.test.ts, and the 38 vendored test-quic-*close*.mjs and test-quic-session-*.mjs files.
  • Self-reviewed: 5 concerns raised, 5 addressed (the transport-with-reason cell gained a test, the text names the overlap with node:quic: send a client's coded close before the server's first packet, ignore close options without code #44846).

Background

Downsides

  • Behaviour change for close/destroy with { code: 0n, type: "transport", reason }: the peer got INTERNAL_ERROR "connection error" and now gets NO_ERROR with the reason, as with Node. A user who relied on the rejection sees closed resolve.
  • struct conn_err grows from 8 to 12 bytes. struct ietf_full_conn stays 10,232 bytes (padding). lsquic_full_conn_ietf.c.o .text grows 761 bytes in a non-LTO release compile (size), from the extra store at each of the 75 ABORT_QUIETLY sites. immediate_close stays 192 instructions, ci_abort_error goes from 100 to 101 (objdump -d). No change per packet or per tick.
Notes

Repro. The script from #45499, with bun 1.4.3:

destroy(undefined, { code: 0n, type: "application" }) -> peer closed rejected: ERR_QUIC_TRANSPORT_ERROR type=transport errorCode=1 (QUIC transport error 1: connection error)
close({ code: 0n, type: "application" })         -> peer closed rejected: ERR_QUIC_TRANSPORT_ERROR type=transport errorCode=1 (QUIC transport error 1: connection error)
destroy(undefined, { code: 2n, type: "application" }) -> peer closed rejected: ERR_QUIC_APPLICATION_ERROR type=application errorCode=2 (QUIC application error 2)
destroy(undefined, { code: 0n, type: "transport" }) -> peer closed resolved

With this branch the first two lines read peer closed resolved.

Fail-before. USE_SYSTEM_BUN=1 bun test test/js/node/quic/quic-endpoint.test.ts on 1.4.3: 5 of the 8 new tests fail (the three application code 0 cases, and transport code 0 with a reason through close() and destroy()). The other three (application code 2, transport code 0 without a reason through close() and destroy()) pass on both and pin the cells that do not change. The tests use a 600 s idle timeout: an idle timeout also resolves closed, so only a CONNECTION_CLOSE frame can pass a test inside its timeout. The fix lives in patches/, so a checkout of src/ alone does not remove it.

Writers of ifc_error. ABORT_QUIETLY (75 sites, all through CONN_ERR) and one direct conn->ifc_error = CONN_ERR(1, HEC_GENERAL_PROTOCOL_ERROR) at line 3845. No ABORT_QUIETLY site in lsquic stores code 0: all transport codes are nonzero, and HTTP/3 codes start at 0x100. So only lsquic_conn_abort_error callers reach the code 0 path. All five callers are in node:quic (session.rs 3, endpoint.rs 2). The Bun.serve HTTP/3 server and the fetch HTTP/3 client use lsquic_conn_close and lsquic_conn_abort.

Other suites. test/js/node/quic/ 27 pass. serve-http3.test.ts 79 pass. 38 of 39 vendored test-quic-*close*.mjs / test-quic-session-*.mjs pass on the debug build. test-quic-session-initial-rtt.mjs fails on the debug ASAN build with and without this change (it asserts a 10 ms RTT bound) and passes on the release binary.

Measurements. Single-file release compile of lsquic_full_conn_ietf.c with the flags from build/release/build.ninja minus -flto=thin, before and after the patch: .text 97,043 to 97,804 bytes. sizeof(struct conn_err) 8 to 12, sizeof(struct ietf_full_conn) 10,232 both.


[policy-decision:dep] gate passed · iteration 0 · 3 files touched

passes on PR (with fix)
Test-only change.

Debug/ASAN (expected pass):
$ bun bd test 'test/js/node/quic/quic-endpoint.test.ts'
$ BUN_DEBUG_QUIET_LOGS=1 bun scripts/build.ts --profile=debug --quiet test test/js/node/quic/quic-endpoint.test.ts
bun test v1.4.4 (367d939d9)

test/js/node/quic/quic-endpoint.test.ts:
(node:28737) ExperimentalWarning: quic is an experimental feature and might change at any time
(Use `bun-debug --trace-warnings ...` to show where the warning was created)
(pass) QuicEndpoint client-engine mode > an explicit endpoint rejects a connect() in the other mode [727.34ms]
(pass) endpoint blockList > applies to packets forwarded from a sibling endpoint on the same loop [469.16ms]
(pass) server ALPN list > rejects a list mixing HTTP/3 and non-HTTP/3 protocols [259.68ms]
(pass) node:quic under --isolate > a second test file in the same process gets its own callbacks [4213.41ms]
(pass) dual-mode endpoint > does not stateless-reset its own client connection [207.10ms]
(pass) transportParams.maxIdleTimeout > 0 disables the idle timeout instead of falling back to the default [106.39ms]
(pass) endpoint.close() while a session is live > stops accepting new sessions so closed can resolve [106.26ms]
(pass) session close with error code 0 > destroy(undefined, { code: 0n, type: 'application' }) resolves the peer's closed [57.95ms]
(pass) session close with error code 0 > close({ code: 0n, type: 'application' }) resolves the peer's closed [73.90ms]
(pass) session close with error code 0 > close({ code: 0n, type: 'application', reason }) resolves the peer's closed [38.42ms]
(pass) session close with error code 0 > a nonzero application code still reaches the peer as an application error [49.20ms]
(pass) session close with error code 0 > destroy(undefined, { code: 0n, type: 'transport' }) resolves the peer's closed [66.81ms]
(pass) session close with error code 0 > close({ code: 0n, type: 'transport' }) resolves the peer's closed [41.72ms]
(pass) session close with error code 0 > destroy(undefined, { code: 0n, type: 'transport', reason }) resolves the peer's closed [70.55ms]
... (truncated)
Exit: 0
diff hotspot
patches/lsquic/abort-error-app-code-zero.patch | 41 +++++++++++++
 scripts/build/deps/lsquic.ts                   |  6 ++
 test/js/node/quic/quic-endpoint.test.ts        | 83 ++++++++++++++++++++++++++
 3 files changed, 130 insertions(+)

gate history · 2 passed · 0 rejected · iteration 0

evidence per changed file
file                                            reads  edits  tests
patches/lsquic/abort-error-app-code-zero.patch      0      0     12
scripts/build/deps/lsquic.ts                        1      2     12
test/js/node/quic/quic-endpoint.test.ts             0      0     12

root cause · written by the author bot

The lsquic connection close path used a nonzero error code as its only signal that an application or transport error had been recorded, so an application close with code 0 (or a transport close with code 0 and a reason) was treated as having no recorded error and fell through to the default transport CONNECTION_CLOSE carrying INTERNAL_ERROR (0x1) and the reason "connection error". The fix records the error type and code explicitly when lsquic_conn_abort_error is called, independent of whether the code is zero, so the close frame is built from what the caller actually supplied. An applicatio…

lsquic read "error code != 0" as "an error is recorded". A close with
code 0 that came in through lsquic_conn_abort_error (an application
close, or a transport close with a reason) lost its code and type in
immediate_close() and went out as a transport CONNECTION_CLOSE with
INTERNAL_ERROR. Record the error with a flag of its own in struct
conn_err. The same line in immediate_close() is also changed by the
lsquic patch of #44846, which has to rebase onto this.
@coderabbitai

coderabbitai Bot commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository: oven-sh/bun/.coderabbit.yaml
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: 8dfbb7a8-3855-4b30-9675-15a4259fb2e9
📥 Commits

Reviewing files that changed from the base of the PR and between 7fea49a and 86e9f53.

📒 Files selected for processing: 1
  • test/js/node/quic/quic-endpoint.test.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 2 remain after this review.

Walkthrough

The lsquic patch tracks recorded errors separately from their numeric codes. The build applies the patch. Endpoint tests check peer results for zero-code application and transport closes, and for a nonzero application code.

Changes

QUIC close error handling

Layer / File(s) Summary
Record connection errors
patches/lsquic/abort-error-app-code-zero.patch, scripts/build/deps/lsquic.ts
The patch adds a recorded flag and uses it for close-error selection and the existing-error guard. The build applies the patch to lsquic.
Check peer close results
test/js/node/quic/quic-endpoint.test.ts
Tests check peer results for zero-code application and transport closes, with and without reasons, through destroy and close. They also check that application code 2n reaches the peer as an application error.

Suggested reviewers: jarred-sumner

Priority: ➖ Normal

Severity of issue fixed: Medium

Merge Risk: ⚪ Minimal · up to 86e9f

The change preserves zero-valued QUIC close codes, and peer tests exercise the shared close path. No concrete behavior or coverage issue remains; the change is ready to merge subject to normal checks.

Pre-merge checks: Passed 4
Check name Status Explanation
Linked Issues check Passed Issue #45499 requires application close code 0n to reach the peer as application CONNECTION_CLOSE code 0, not transport INTERNAL_ERROR. The new lsquic patch adds conn_err.recorded, sets it in `CONN_…
Out of Scope Changes check Passed The three changed files remain within issue #45499. The lsquic patch fixes the reported error-state check. The dependency file applies that patch. The endpoint tests verify the required application be…
Title check Passed The title clearly and concisely describes the main change: preserving close code 0 instead of sending INTERNAL_ERROR.
Description check Passed The description explains the problem, root cause, implementation, behavior change, and verification results. It does not use the exact template headings, but it provides the required information in eq…
Full details: Linked Issues check

Explanation

Issue #45499 requires application close code 0n to reach the peer as application CONNECTION_CLOSE code 0, not transport INTERNAL_ERROR. The new lsquic patch adds conn_err.recorded, sets it in CONN_ERR, and uses it in both close-path readers. scripts/build/deps/lsquic.ts applies the patch. Tests cover application code 0 through destroy and close, with and without a reason, and verify that a nonzero application code remains an application error. The tests also cover transport code 0 cases affected by the same path.

Full details: Out of Scope Changes check

Explanation

The three changed files remain within issue #45499. The lsquic patch fixes the reported error-state check. The dependency file applies that patch. The endpoint tests verify the required application behavior and related transport-close behavior. No unrelated product or build changes are present.

Full details: Description check

Explanation

The description explains the problem, root cause, implementation, behavior change, and verification results. It does not use the exact template headings, but it provides the required information in equivalent sections.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

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

@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.

Nothing blocking. The comments below are optional suggestions. There is no need to push a fix for them before merging.

Comment thread test/js/node/quic/quic-endpoint.test.ts Resolved
Comment thread test/js/node/quic/quic-endpoint.test.ts Resolved
@robobun

robobun commented Oct 17, 2026 •

Copy link
Copy Markdown
Collaborator Author
Updated 12:59 PM PT - Oct 18th, 2026

✅ @robobun, your commit 86e9f53281d01966cbce05dbea0ecea04ea2d86d passed in Build #128406! 🎉


🧪   To try this PR locally:

bunx bun-pr 45702

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

bun-45702 --bun

@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.

Code review completed

Nothing new to post: everything this review found is already covered by existing comments on this pull request or didn't merit a separate one.

@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-10-18.

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.

node:quic: an application close with error code 0 is sent as transport error INTERNAL_ERROR

1 participant