Skip to content

fix: prevent race condition between onEnd and onTrailers in HTTP/2 client (#5216)#5343

Open
mcollina wants to merge 3 commits into
mainfrom
fix/flaky-http2-trailers-test
Open

fix: prevent race condition between onEnd and onTrailers in HTTP/2 client (#5216)#5343
mcollina wants to merge 3 commits into
mainfrom
fix/flaky-http2-trailers-test

Conversation

@mcollina
Copy link
Copy Markdown
Member

@mcollina mcollina commented Jun 2, 2026

Description

Fixes a flaky test test/http2-trailers.js (issue #5216) caused by a race condition between the onEnd and onTrailers event handlers in the HTTP/2 client (lib/dispatcher/client-h2.js).

Root Cause

On Windows, Node.js can emit 'end' before 'trailers' (reversing the expected event order). When onEnd fires first, it calls request.onResponseEnd({}) with empty trailers and removes the 'trailers' listener via releaseRequestStream(stream). By the time onTrailers fires, it sees request.completed === true and returns without passing the actual trailers. The test assertion t.strictEqual(trailers['x-trailer'], 'hello') then fails.

Fix

Consolidate all response completion and stream cleanup into onRequestStreamClose, which fires on the stream's natural 'close' event (always after 'end' and 'trailers'). Individual event handlers now only store state:

  • onEnd: sets state.pendingEnd = true, returns — no releaseRequestStream, no onResponseEnd
  • onTrailers: stores trailers in state.trailers, removes 'data' listener, returns — no releaseRequestStream, no onResponseEnd
  • onTimeout/onError/onFrameError: abort the request, return — no releaseRequestStream
  • onRequestStreamClose: calls releaseRequestStream, then completes the response with state.trailers || {} if pendingEnd && !aborted && !completed

This eliminates the deferredCompletion flag, the process.nextTick fallback, and all complex branching — the stream lifecycle ('end''trailers''close') handles everything regardless of event ordering.

Test Adaptation

Both tests in test/http2-late-data.js now emit 'close' after 'end' to trigger onRequestStreamClose, which is the natural event order in real HTTP/2 streams.

Testing

All 295 HTTP/2 and client tests pass, including test/http2-trailers.js.

…ient

When the 'end' event fires before 'trailers' (observed on Windows),
onEnd would call onResponseEnd({}) with empty trailers and remove
the 'trailers' listener, causing the flaky test in issue #5216.

Fix: in onEnd, check if the 'trailers' listener is still registered.
If so, defer completion to onTrailers via a process.nextTick fallback.
This ensures onTrailers gets a chance to pass the actual trailers,
regardless of event ordering.

Adapts the test 'Should remove request-owned http2 stream listeners
after completion' to check listener counts after the async fallback.
@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Jun 2, 2026

Codecov Report

❌ Patch coverage is 91.30435% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 93.24%. Comparing base (7235cbb) to head (0967b99).
⚠️ Report is 13 commits behind head on main.

Files with missing lines Patch % Lines
lib/dispatcher/client-h2.js 91.30% 2 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main    #5343   +/-   ##
=======================================
  Coverage   93.23%   93.24%           
=======================================
  Files         110      110           
  Lines       36631    36678   +47     
=======================================
+ Hits        34154    34200   +46     
- Misses       2477     2478    +1     

☔ 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 marked this pull request as ready for review June 5, 2026 08:49
@mcollina mcollina requested a review from metcoder95 June 5, 2026 14:33
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.

2 participants