-
-
Notifications
You must be signed in to change notification settings - Fork 34.2k
stream: catch and forward error from dest.write #55270
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
Review requested:
|
I don't think it should be supported. It is most likely a programmer error. |
76823e6 to
6dfdf7e
Compare
I agree, but I am afraid this would break many user land packages. |
|
We could mark it as semver-major and run CITGM. I think it should be fixed in the "broken" packages and I don't think there are many of them. |
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #55270 +/- ##
=======================================
Coverage 87.95% 87.96%
=======================================
Files 656 656
Lines 188310 188315 +5
Branches 35963 35973 +10
=======================================
+ Hits 165630 165645 +15
+ Misses 15854 15848 -6
+ Partials 6826 6822 -4
|
|
I've been thinking about it, and I think we should more carefully consider whether we should throw directly in The reasons being:
To wrap up, what I am trying to say is that we may wanna consider this breaking change more carefully as all the cc. @nodejs/streams |
mcollina
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will likely break a lot of modules.
Many people (including myself) use object mode streams with strings or buffers. Even Node.js Core implies that, as we assumed this when we flipped Readable.from() to always be in object mode: Readable.from(['a', 'b']).pipe(fs.createWriteStream())
|
@mcollina I think you misunderstand this PR. It basically fixes an uncatchable error. Doesn't break anything as far as I can tell. |
|
Though I do think this PR is a little over engineered. It's enough to catch the error and forward it "as is" to destroy. |
mcollina
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I totally misunderstood this. I've left a few comments.
|
Thank you for the review! 🙏 I appreciate the feedback and am happy to remove this error. As @ronag pointed out, it seems like I may have over-engineered it. My initial intention was to help developers understand the error better, but looking back, it appears I might've complicated things more than I've fixed. Another point for discussion is whether we should catch the error and call destroy on the |
I disagree with @lpinca on this one and would prefer a catchable error. The exception guarantee is understandable and it's possible to isolate the error from other working parts of a service, i.e. better to have a http server return 500 on a broken path than stop responding on all paths. |
The error is already understandable (your suggestion is to call I would also argue that if we want this behavior we should make |
|
@ronag this change of behavior would be semver-major anyway. |
|
I'm still unsure about whether we should catch the error or proceed with a hard crash. Is there a similar example in Node.js core that we could use for comparison? On the other hand, does those lines make sense? diff --git a/lib/internal/streams/writable.js b/lib/internal/streams/writable.js
index ac14b202b6..5225aa13f4 100644
--- a/lib/internal/streams/writable.js
+++ b/lib/internal/streams/writable.js
@@ -477,8 +477,9 @@ function _write(stream, chunk, encoding, cb) {
chunk = Stream._uint8ArrayToBuffer(chunk);
encoding = 'buffer';
} else {
- throw new ERR_INVALID_ARG_TYPE(
- 'chunk', ['string', 'Buffer', 'TypedArray', 'DataView'], chunk);
+ stream.destroy(new ERR_INVALID_ARG_TYPE(
+ 'chunk', ['string', 'Buffer', 'TypedArray', 'DataView'], chunk));
+ return
}
}or I should move what I've done in the catch block into |
|
I think your PR was fine other than there is no need to convert it into another error. |
c01a0b9 to
52d26c2
Compare
52d26c2 to
de9f68a
Compare
That's the expected error.
Looks related to pipeline transforms like: Duplex.from(async function* (files: AsyncGenerator<MemFsEditorFile>) {
for await (const file of files) {
if (isFilePending(file)) {
yield file;
}
}
}),
I use
It's not a single place, yeoman/generator#1577, SBoudrias/mem-fs-editor#282, SBoudrias/mem-fs#40. |
|
Hi @aduh95 I see you've helped with the CITGM result here - #55270 (comment) 🙏 I have to admit, I'm not entirely sure how to interpret the results. Is there any documentation that explains how to understand the CITGM results? For this PR, are there any user-land packages I should examine more closely? |
|
Landed in fd8de67 |
| const assert = require('node:assert'); | ||
| const { Readable, Transform, Writable } = require('node:stream'); | ||
|
|
||
| // Pipeine objects from object mode to non-object mode should throw an error and |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jakecastelli sorry, just out curiosity, is pipeine a typo? should it be pipeline?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it is a typo, thanks for spotting it and PR welcome 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had an other look, my origin intention should be piping, I've created a quick PR here
Fixes: #54945
Wanted to give a quick mention why checking object modes at the start of the pipe function wouldn't work (even though I liked the idea). Because if the chunk from the source stream is not object while source stream is in object mode and destination stream is not in object mode, it should still work.
Consider the following example: