Skip to content

fix(node_stream): handle Web Stream adapter errors - #36193

Merged
nathanwhit merged 2 commits into
denoland:mainfrom
nathanwhit:fix/node-stream-web-adapter-errors
Jul 21, 2026
Merged

nathanwhit merged 2 commits into
denoland:mainfrom
nathanwhit:fix/node-stream-web-adapter-errors

Conversation

@nathanwhit

Copy link
Copy Markdown
Member

Summary

  • bind the internal destroy helper to the adapted Node stream
  • normalize scalar, array, and missing writev completion values
  • add focused adapter coverage and enable the corresponding upstream Node regression

Details

Several Web Stream adapter failure paths called an instance-oriented destroy helper as a plain function. The batch write completion path also assumed every value was an array, although promise rejection handlers receive a scalar error. These paths could replace the original stream failure with a secondary exception or unhandled rejection.

Bind each destroy call to the appropriate stream, pass the first write error through the Node callback, and route terminal promise-chain failures through the same completion path.

Validation

  • ./tools/format.js
  • cargo build
  • cargo test -p unit_node_tests --test unit_node stream_test -- --nocapture
  • cargo test -p node_compat_tests --test node_compat test-webstreams-duplex-fromweb-writev-unhandled-rejection -- --nocapture

@bartlomieju bartlomieju left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. The core fix is correct and complete: Deno's destroy (internal/streams/destroy.js) is a prototype method that reads this._readableState/this._writableState, so the old plain destroy(duplex, error) calls set this=undefined and passed the stream as the error arg. Converting every site to destroy.call(stream, error) is the right fix, and I confirmed all the buggy call sites are covered. The writev normalization correctly forwards a single Error to the Node callback (Promise.all only rejects with the first scalar error, so nothing is dropped), and I traced every settle path for the added .catch(done): it cannot double-invoke the callback since done is throw-safe, so there is no ERR_MULTIPLE_CALLBACK risk.

A few optional, non-blocking nits:

  • tests/node_compat/config.jsonc: the new entry breaks the alphabetical ordering (duplex sorts after compose); it belongs after test-webstreams-decompression-reject-trailing.js.
  • adapters.js: the .catch(done) is near-dead and diverges from upstream. Every settle path already routes through done, so it only fires on a synchronous throw inside the fulfillment arrow, which writer.write does not normally do. A one-line comment naming the case it guards, or dropping it, would be clearer.
  • The 6-line writev error-normalization block is duplicated verbatim across the Writable and Duplex adapters. Upstream keeps the two done() functions separate so the duplication is defensible, but a small file-local helper would read nicer.

Nice test coverage.

@nathanwhit
nathanwhit merged commit d48d5a4 into denoland:main Jul 21, 2026
136 checks passed
bartlomieju pushed a commit that referenced this pull request Jul 23, 2026
## Summary

- bind the internal destroy helper to the adapted Node stream
- normalize scalar, array, and missing writev completion values
- add focused adapter coverage and enable the corresponding upstream
Node regression

## Details

Several Web Stream adapter failure paths called an instance-oriented
destroy helper as a plain function. The batch write completion path also
assumed every value was an array, although promise rejection handlers
receive a scalar error. These paths could replace the original stream
failure with a secondary exception or unhandled rejection.

Bind each destroy call to the appropriate stream, pass the first write
error through the Node callback, and route terminal promise-chain
failures through the same completion path.

## Validation

- ./tools/format.js
- cargo build
- cargo test -p unit_node_tests --test unit_node stream_test --
--nocapture
- cargo test -p node_compat_tests --test node_compat
test-webstreams-duplex-fromweb-writev-unhandled-rejection -- --nocapture
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