Skip to content

fix(client-h2): stop double-decrementing kOpenStreams on stream timeout#5076

Open
SAY-5 wants to merge 1 commit intonodejs:mainfrom
SAY-5:fix/h2-timeout-double-decrement-5073
Open

fix(client-h2): stop double-decrementing kOpenStreams on stream timeout#5076
SAY-5 wants to merge 1 commit intonodejs:mainfrom
SAY-5:fix/h2-timeout-double-decrement-5073

Conversation

@SAY-5
Copy link
Copy Markdown
Contributor

@SAY-5 SAY-5 commented Apr 20, 2026

Fixes #5073.

The HTTP/2 stream timeout handler in lib/dispatcher/client-h2.js does:

stream.on('timeout', () => {
  const err = new InformationalError(`HTTP/2: "stream timeout after ${requestTimeout}"`)
  stream.removeAllListeners('data')
  session[kOpenStreams] -= 1
  if (session[kOpenStreams] === 0) session.unref()
  abort(err)
})

abort(err) ultimately destroys the http2 stream, which fires close, and the close listener a few lines above already decrements kOpenStreams:

stream.once('close', () => {
  stream.removeAllListeners('data')
  session[kOpenStreams] -= 1
  if (session[kOpenStreams] === 0) session.unref()
})

Result: a single timed-out request knocks the counter down twice and it ends at -1 instead of 0. Future stream accounting on the session is then permanently skewed — the === 0 unref check can never match, and reused-session flows that rely on the counter reaching 0 to release the socket start holding sessions open longer than they should.

Leave the decrement to the close handler. The timeout handler only needs to stop the data listener and call abort(); destruction via abort() guarantees close fires exactly once. After this change the repro in the issue reports open streams after timeout: 0 as expected.

@codecov-commenter
Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.10%. Comparing base (c7a2901) to head (60ba16b).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #5076      +/-   ##
==========================================
- Coverage   93.10%   93.10%   -0.01%     
==========================================
  Files         110      110              
  Lines       35805    35799       -6     
==========================================
- Hits        33337    33331       -6     
  Misses       2468     2468              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

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

Copy link
Copy Markdown
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

Thanks for opening a PR! Can you please add a unit test?

Both the 'timeout' handler and the 'close' handler on an h2 stream
decremented session[kOpenStreams] and called session.unref when the
counter hit zero. Because the 'timeout' path is always followed by the
stream actually closing, every timed-out stream ran the decrement twice,
leaving the counter one lower than the real number of open streams. On
a session that sees N timed-out streams the counter drifts to -N, and
the 'close' handler's 'if (session[kOpenStreams] === 0) session.unref()'
check never fires again for the lifetime of the session.

In single-request scenarios this manifests as the socket never being
re-unref'd after a timeout even though no streams remain; in
multi-stream scenarios it also causes session.unref to fire while a
concurrent stream is still active, because the decrement runs before
the close of the timed-out stream.

Remove the duplicate decrement from the 'timeout' handler and rely on
the unified bookkeeping in 'close', which is guaranteed to run for every
stream regardless of how it ended.

Add a regression test that drives three sequential bodyTimeout aborts
on the same h2 session and asserts the open-streams counter returns to
0. On the unpatched code the counter settles at -3.

Closes nodejs#5073

Signed-off-by: SAY-5 <SAY-5@users.noreply.github.com>
@SAY-5 SAY-5 force-pushed the fix/h2-timeout-double-decrement-5073 branch from 60ba16b to 12f1734 Compare April 21, 2026 01:49
@SAY-5
Copy link
Copy Markdown
Contributor Author

SAY-5 commented Apr 21, 2026

Thanks @mcollina — added a regression test in test/http2-timeout.js.

The test drives three sequential bodyTimeout aborts on the same h2 session, then reads the module-local open-streams counter off the session via its Symbol description. With the fix applied the counter returns to 0 after each stream closes; on master (pre-patch) it settles at -3 after the three cycles because the 'timeout' handler and the 'close' handler were both decrementing it.

Verified locally on macOS, Node 25.9:

  • npm run lint: clean (eslint --cache)
  • node --test --test-isolation=none test/http2-timeout.js: 2/2 pass
  • Reverted lib/dispatcher/client-h2.js to master and re-ran the new test to confirm it fails with open-stream counter should be 0 after all streams close, got -3, i.e. the assertion pins the exact drift the fix corrects.

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.

HTTP/2 stream timeouts decrement session open-stream count twice

3 participants