Skip to content

fix(h2): honour headersTimeout - #5604

Merged
mcollina merged 1 commit into
v7.xfrom
fix/h2-headers-timeout-v7
Jul 29, 2026
Merged

fix(h2): honour headersTimeout#5604
mcollina merged 1 commit into
v7.xfrom
fix/h2-headers-timeout-v7

Conversation

@mcollina

Copy link
Copy Markdown
Member

writeH2() armed the stream timer from bodyTimeout for the whole request:

const requestTimeout = request.bodyTimeout ?? client[kBodyTimeout]

so headersTimeout had no effect at all over HTTP/2. A server that accepted the stream and never sent headers was only cut off after bodyTimeout.

With headersTimeout: 200, bodyTimeout: 5000 against a server that accepts the stream and never responds:

before after
fails after 5131ms ~200ms
error InformationalError: HTTP/2: "stream timeout after 5000" HeadersTimeoutError: HTTP/2: "headers timeout after 200"

Change

  • arm the stream timer with headersTimeout
  • switch it to bodyTimeout once the response headers arrive
  • report whichever deadline actually elapsed as HeadersTimeoutError (UND_ERR_HEADERS_TIMEOUT) or BodyTimeoutError (UND_ERR_BODY_TIMEOUT), matching the HTTP/1.1 path and main

The upgrade / CONNECT paths also use headersTimeout, since those streams are waiting for a response.

Behaviour changes worth a look

Two, both on the h2 path only:

  1. Requests now time out earlier when headersTimeout < bodyTimeout — that is the point of the fix, but anyone who had (unknowingly) been relying on bodyTimeout covering the headers phase will see requests fail sooner. The defaults are equal (300s), so only users who set the two differently are affected.
  2. The error type and message changed for h2 timeouts: UND_ERR_INFO / "stream timeout after N" becomes UND_ERR_HEADERS_TIMEOUT / UND_ERR_BODY_TIMEOUT with "headers timeout after N" / "body timeout after N". This aligns v7 with h1 and with main, so it removes a surprise on upgrade, but it is a visible change for anyone matching on the old code or string. test/http2-timeout.js is updated accordingly.

Happy to drop (2) and keep the InformationalError shape if you would rather not change error codes on a maintenance branch — the timing fix stands on its own.

Tests

test/http2-headers-timeout.js covers both directions: headersTimeout firing while bodyTimeout is much larger, and bodyTimeout applying once headers have arrived. Both were verified to fail before this change (the first waiting 5131ms and reporting the wrong error).

test/+(http2|h2)*.js: 52 pass. Full unit suite: 1305 pass, 0 fail.

🤖 Generated with Claude Code

https://claude.ai/code/session_01A49JamgF2TkZHu5h58ChUM

writeH2() armed the stream timer from bodyTimeout for the whole request:

    const requestTimeout = request.bodyTimeout ?? client[kBodyTimeout]

so headersTimeout had no effect at all over HTTP/2. A server that accepted
the stream and never sent headers was only cut off after bodyTimeout — with
headersTimeout: 200 and bodyTimeout: 5000 the request failed after 5131ms
instead of ~200ms.

Arm the timer with headersTimeout, switch it to bodyTimeout once the
response headers arrive, and report whichever deadline actually elapsed as
HeadersTimeoutError or BodyTimeoutError instead of a generic
InformationalError, matching the HTTP/1.1 path.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01A49JamgF2TkZHu5h58ChUM
Signed-off-by: Matteo Collina <hello@matteocollina.com>
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.15%. Comparing base (8d347dd) to head (32889bb).

Additional details and impacted files
@@            Coverage Diff             @@
##             v7.x    #5604      +/-   ##
==========================================
- Coverage   93.17%   93.15%   -0.02%     
==========================================
  Files         112      112              
  Lines       36751    36760       +9     
==========================================
+ Hits        34241    34245       +4     
- Misses       2510     2515       +5     

☔ View full report in Codecov by Harness.
📢 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.

@mcollina
mcollina merged commit cd388b9 into v7.x Jul 29, 2026
37 checks passed
@mcollina
mcollina deleted the fix/h2-headers-timeout-v7 branch July 29, 2026 07:19
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.

3 participants