Skip to content

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

Draft
mcollina wants to merge 2 commits into
mainfrom
fix/flaky-http2-trailers-test
Draft

fix: prevent race condition between onEnd and onTrailers in HTTP/2 client (#5216)#5343
mcollina wants to merge 2 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

In onEnd, check stream.listenerCount('trailers') before handling completion:

  • If the 'trailers' listener is still registered, defer completion to onTrailers via a process.nextTick fallback instead of finalizing immediately.
  • If onTrailers never fires (no-trailers case), the fallback handles completion.
  • If onTrailers fires first (normal event order), it removes the 'end' listener and onEnd never runs.

Test Adaptation

The test 'Should remove request-owned http2 stream listeners after completion' now checks listener counts after the process.nextTick fallback has run, since listeners are now removed asynchronously in the no-trailers case.

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.23%. Comparing base (7235cbb) to head (866df8f).
⚠️ Report is 8 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.23%   -0.01%     
==========================================
  Files         110      110              
  Lines       36631    36688      +57     
==========================================
+ Hits        34154    34206      +52     
- Misses       2477     2482       +5     

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

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