Skip to content

http2: settle pending write callbacks on stream destroy - #66016

Open
mcollina wants to merge 1 commit into
nodejs:mainfrom
mcollina:fix-http2-close-while-writing
Open

http2: settle pending write callbacks on stream destroy#66016
mcollina wants to merge 1 commit into
nodejs:mainfrom
mcollina:fix-http2-close-while-writing

Conversation

@mcollina

Copy link
Copy Markdown
Member

When an HTTP/2 stream is destroyed while a write is still in flight, nghttp2 may have already handed the data off to the socket and never report the write's completion once the stream or session tears down. In that case the native completion callback that resolves the Writable write (stream[kState].writeCb) is never invoked, so the Writable stays stuck and the event loop never drains, causing test-http2-close-while-writing to time out on macOS.

In Http2Stream._destroy, settle any write that is still in flight by invoking its callback with the destroy error and resetting writePending. A later native completion callback is then a no-op because writeCb is null and writePending is 0, so the settle is idempotent. Passing the error (rather than null) also drains any buffered writes.

Fixed: #58252

Assisted-by: pi-coding-agent

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/http2
  • @nodejs/net

@nodejs-github-bot nodejs-github-bot added http2 Issues and PRs related to the http2 subsystem. needs-ci PRs that need a full CI run. labels Sep 13, 2026
@codecov

codecov Bot commented Sep 13, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.20%. Comparing base (6f41e41) to head (9d88a60).
⚠️ Report is 175 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #66016      +/-   ##
==========================================
+ Coverage   90.17%   90.20%   +0.03%     
==========================================
  Files         770      785      +15     
  Lines      264483   269356    +4873     
  Branches    50262    51515    +1253     
==========================================
+ Hits       238491   242970    +4479     
+ Misses      16981    16899      -82     
- Partials     9011     9487     +476     
Files with missing lines Coverage Δ
lib/internal/http2/core.js 95.03% <100.00%> (-0.05%) ⬇️

... and 182 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@mcollina
mcollina force-pushed the fix-http2-close-while-writing branch from 4720388 to 2e34a6f Compare September 13, 2026 17:27
When a stream is destroyed while a write is still in flight, nghttp2 may
have handed the data to the socket and never report the write completion
once the stream or session tears down. That leaves the write callback
unresolved, the Writable stuck, and the event loop never drains, which
times out test-http2-close-while-writing on macOS.

Settle the in-flight write in Http2Stream._destroy by invoking its
callback with the destroy error and resetting writePending. A later
native completion callback is then a no-op because writeCb is null and
writePending is 0, so the settle is idempotent. Passing the error rather
than null also drains any buffered writes.

Fixes: nodejs#58252

Signed-off-by: Matteo Collina <hello@matteocollina.com>
Assisted-by: pi-coding-agent
@mcollina
mcollina force-pushed the fix-http2-close-while-writing branch from 2e34a6f to 9d88a60 Compare September 13, 2026 19:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

http2 Issues and PRs related to the http2 subsystem. needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

parallel/test-http2-close-while-writing is flaky

3 participants