Skip to content

stream: fix Writable.toWeb() hang on synchronous drain#61197

Open
Han5991 wants to merge 1 commit intonodejs:mainfrom
Han5991:fix/61145-stream-to-web-hang
Open

stream: fix Writable.toWeb() hang on synchronous drain#61197
Han5991 wants to merge 1 commit intonodejs:mainfrom
Han5991:fix/61145-stream-to-web-hang

Conversation

@Han5991
Copy link
Copy Markdown
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

@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
Copy Markdown

codecov bot commented Dec 29, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 89.69%. Comparing base (9e0dc8b) to head (6fc9709).
⚠️ Report is 7 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #61197      +/-   ##
==========================================
- Coverage   89.70%   89.69%   -0.01%     
==========================================
  Files         678      691      +13     
  Lines      207285   213946    +6661     
  Branches    39750    41044    +1294     
==========================================
+ Hits       185937   191895    +5958     
- Misses      13441    14129     +688     
- Partials     7907     7922      +15     
Files with missing lines Coverage Δ
lib/internal/webstreams/adapters.js 86.48% <100.00%> (+0.03%) ⬆️

... and 70 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.

@VoltrexKeyva VoltrexKeyva added the request-ci Add this label to start a Jenkins CI on a PR. label Dec 30, 2025
@github-actions github-actions bot added request-ci-failed An error occurred while starting CI via request-ci label, and manual interventon is needed. and removed request-ci Add this label to start a Jenkins CI on a PR. labels Dec 30, 2025
@github-actions
Copy link
Copy Markdown
Contributor

Failed to start CI
   ⚠  No approving reviews found
   ✘  Refusing to run CI on potentially unsafe PR
https://github.com/nodejs/node/actions/runs/20606939876

@Han5991 Han5991 force-pushed the fix/61145-stream-to-web-hang branch from 1e7c128 to ec84fd4 Compare January 17, 2026 07:16
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
@Han5991 Han5991 force-pushed the fix/61145-stream-to-web-hang branch from ec84fd4 to 6fc9709 Compare March 28, 2026 13:25
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. request-ci-failed An error occurred while starting CI via request-ci label, and manual interventon is needed. 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

3 participants