fix: prevent race condition between onEnd and onTrailers in HTTP/2 client (#5216) by mcollina · Pull Request #5343 · nodejs/undici (original) (raw)

@mcollina

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

added 2 commits

June 3, 2026 17:28

@mcollina

@mcollina

@mcollina mcollina marked this pull request as ready for review

June 5, 2026 08:49

metcoder95

@mcollina mcollina deleted the fix/flaky-http2-trailers-test branch

June 7, 2026 13:17

This was referenced

Jun 8, 2026

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters

[ Show hidden characters]({{ revealButtonHref }})