Skip to content
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

stream: catch and forward error from dest.write #55270

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

jakecastelli
Copy link
Member

@jakecastelli jakecastelli commented Oct 5, 2024

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:

const { Readable, Writable } = require("node:stream");

const write = new Writable({
  write(data, enc, cb) {
    // do something with the data
    cb();
  },
});

write.on("error", (err) => {
  console.log(err);
});

Readable.from("hello hello").pipe(write);

@nodejs-github-bot
Copy link
Collaborator

Review requested:

  • @nodejs/streams

@nodejs-github-bot nodejs-github-bot added errors Issues and PRs related to JavaScript errors originated in Node.js core. needs-ci PRs that need a full CI run. stream Issues and PRs related to the stream subsystem. labels Oct 5, 2024
@lpinca
Copy link
Member

lpinca commented Oct 5, 2024

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

I don't think it should be supported. It is most likely a programmer error.

@jakecastelli
Copy link
Member Author

I don't think it should be supported. It is most likely a programmer error.

I agree, but I am afraid this would break many user land packages.

@lpinca
Copy link
Member

lpinca commented Oct 5, 2024

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.

Copy link

codecov bot commented Oct 5, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 87.92%. Comparing base (b02cd41) to head (5c1a3e6).
Report is 4 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #55270      +/-   ##
==========================================
- Coverage   88.42%   87.92%   -0.50%     
==========================================
  Files         654      654              
  Lines      187852   187857       +5     
  Branches    36134    35832     -302     
==========================================
- Hits       166102   165170     -932     
- Misses      14989    15877     +888     
- Partials     6761     6810      +49     
Files with missing lines Coverage Δ
lib/internal/streams/readable.js 96.20% <100.00%> (+0.01%) ⬆️

... and 95 files with indirect coverage changes

@jakecastelli
Copy link
Member Author

jakecastelli commented Oct 10, 2024

I've been thinking about it, and I think we should more carefully consider whether we should throw directly in pipe method or not.

The reasons being:

  • Readable.from will have objectMode: true by default which means the destination stream has to have objectMode: true, and we have a few tests to demonstrate that this would fail e.g.
    const w = new Writable({

    The Writable would require objectMode: true
  • This could potentially break many user land code - previously I mentioned user land package, without CITGM I wouldn't have a confident answer, however I have spoken to a few friends and they have had a scan in their projects and found they would need to add objectMode: true in many places (keep in mind, this could also be a very biased result)

To wrap up, what I am trying to say is that we may wanna consider this breaking change more carefully as all the Readable.from will default to objectMode: true and we support passing non-object as data when objectMode is true.

cc. @nodejs/streams

Copy link
Member

@mcollina mcollina left a 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 mcollina added the semver-major PRs that contain breaking changes and should be released in the next major version. label Oct 10, 2024
@ronag
Copy link
Member

ronag commented Oct 10, 2024

@mcollina I think you misunderstand this PR. It basically fixes an uncatchable error. Doesn't break anything as far as I can tell.

@ronag
Copy link
Member

ronag commented Oct 10, 2024

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.

@lpinca
Copy link
Member

lpinca commented Oct 10, 2024

@mcollina at the moment this only catches the error thrown by dest.write() and call dest.destroy() with it, so I think it will not break anything. It only addresses #54945. That being said, I think the crash in #54945 is expected and "wanted". We make the process crash on programmer errors.

Copy link
Member

@mcollina mcollina left a 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.

lib/internal/streams/readable.js Outdated Show resolved Hide resolved
@ronag ronag removed the semver-major PRs that contain breaking changes and should be released in the next major version. label Oct 10, 2024
@jakecastelli
Copy link
Member Author

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 dest. As @lpinca noted that this is a programmatic error and that a crash is expected.

@ronag
Copy link
Member

ronag commented Oct 10, 2024

Another point for discussion is whether we should catch the error and call destroy on the dest. As @lpinca noted that this is a programmatic error and that a crash is expected.

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.

@lpinca
Copy link
Member

lpinca commented Oct 10, 2024

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 dest.destry() with the same error) and is not recoverable. If that error occurs, there is something wrong in the user code and not Node.js code.

I would also argue that if we want this behavior we should make writable.write() call writable.destroy() on invalid input instead of throwing an error.

@mcollina
Copy link
Member

@ronag this change of behavior would be semver-major anyway.

@jakecastelli
Copy link
Member Author

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 dest.write function.

@ronag
Copy link
Member

ronag commented Oct 12, 2024

I think your PR was fine other than there is no need to convert it into another error.

@ronag ronag added the semver-major PRs that contain breaking changes and should be released in the next major version. label Oct 12, 2024
@jakecastelli jakecastelli force-pushed the fix-54945 branch 2 times, most recently from c01a0b9 to 52d26c2 Compare October 18, 2024 11:00
@jakecastelli jakecastelli changed the title stream: catch and re-throw objectMode incompatible error stream: catch and forward error from dest.write Oct 18, 2024
@jakecastelli
Copy link
Member Author

Would be great to have some reviews, no hard feeling if folks would like to block it @nodejs/streams

@mcollina
Copy link
Member

CITGM: https://ci.nodejs.org/view/Node.js-citgm/job/citgm-smoker/3497/

Copy link
Member

@benjamingr benjamingr left a comment

Choose a reason for hiding this comment

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

LGTM with green CITGM and semver-major

@jakecastelli jakecastelli added the request-ci Add this label to start a Jenkins CI on a PR. label Oct 27, 2024
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Oct 27, 2024
@nodejs-github-bot

This comment was marked as outdated.

@jakecastelli
Copy link
Member Author

Do you mind taking another quick look? @mcollina 🙏

@jakecastelli jakecastelli requested review from mcollina and removed request for mcollina November 1, 2024 03:18
Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

lgtm

I have a bad feeling this would be massively breaking.

@ronag
Copy link
Member

ronag commented Nov 11, 2024

lgtm

I have a bad feeling this would be massively breaking.

I don't see how. Is it just a feeling or do you have some ideas in regards to how?

@mcollina
Copy link
Member

I don't see how. Is it just a feeling or do you have some ideas in regards to how?

Bad feeling, but I don't see how.

Let's do another CITGM run: https://ci.nodejs.org/view/Node.js-citgm/job/citgm-smoker/3507/

main: https://ci.nodejs.org/view/Node.js-citgm/job/citgm-smoker/3508/

@jakecastelli jakecastelli added author ready PRs that have at least one approval, no pending requests for changes, and a CI started. request-ci Add this label to start a Jenkins CI on a PR. labels Nov 16, 2024
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Nov 16, 2024
@nodejs-github-bot
Copy link
Collaborator

@nodejs-github-bot
Copy link
Collaborator

nodejs-github-bot commented Nov 16, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
author ready PRs that have at least one approval, no pending requests for changes, and a CI started. errors Issues and PRs related to JavaScript errors originated in Node.js core. needs-ci PRs that need a full CI run. semver-major PRs that contain breaking changes and should be released in the next major version. stream Issues and PRs related to the stream subsystem.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[stream] uncatchable error thrown during piping if source and dest don't have same objectMode setting
7 participants