Skip to content

Conversation

@Han5991
Copy link
Contributor

@Han5991 Han5991 commented Dec 29, 2025

This PR fixes a race condition in Writable.toWeb() where the resulting WritableStream would hang if the underlying Node.js
Writable emitted the 'drain' event synchronously during a write() call.

This issue typically manifests when highWaterMark is set to 0, or in custom stream implementations that do not defer 'drain'
emissions. In such cases, the 'drain' event could fire before the adapter had a chance to set up the backpressurePromise and the
listener, causing the adapter to wait indefinitely for an event that had already occurred.

The fix involves checking streamWritable.writableNeedDrain immediately after write() returns false. If the stream is already
drained (i.e., writableNeedDrain is false), the backpressure promise is resolved immediately.

Related Issue

Fixes: #61145

image

A race condition in the Writable.toWeb() adapter caused the stream
to hang if the underlying Node.js Writable emitted a 'drain' event
synchronously during a write() call. This often happened when
highWaterMark was set to 0.

By checking writableNeedDrain immediately after a backpressured write,
the adapter now correctly detects if the stream has already drained,
resolving the backpressure promise instead of waiting indefinitely
for an event that has already occurred.

Fixes: nodejs#61145
@nodejs-github-bot nodejs-github-bot added needs-ci PRs that need a full CI run. web streams labels Dec 29, 2025
@codecov
Copy link

codecov bot commented Dec 29, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 88.55%. Comparing base (05d6b9b) to head (92d4e9d).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #61197      +/-   ##
==========================================
+ Coverage   88.53%   88.55%   +0.01%     
==========================================
  Files         703      703              
  Lines      208599   208602       +3     
  Branches    40229    40228       -1     
==========================================
+ Hits       184685   184720      +35     
+ Misses      15939    15906      -33     
- Partials     7975     7976       +1     
Files with missing lines Coverage Δ
lib/internal/webstreams/adapters.js 85.61% <100.00%> (+0.04%) ⬆️

... and 31 files with indirect coverage changes

🚀 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

needs-ci PRs that need a full CI run. web streams

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Stream created by Writable.toWeb() hangs when highWaterMark is set to 0

2 participants