Skip to content

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

@jakecastelli jakecastelli requested review from lpinca and ronag October 5, 2024 05:20
@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.

@codecov
Copy link

codecov bot commented Oct 5, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 87.96%. Comparing base (3bb1d28) to head (b1c5b00).
Report is 161 commits behind head on main.

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     
Files with missing lines Coverage Δ
lib/internal/streams/readable.js 96.20% <100.00%> (+0.01%) ⬆️

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

@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
@mshima
Copy link

mshima commented Dec 11, 2024

This is the error I've got

That's the expected error.
With node 22.12.0 the command just hang without showing the error. Like:

$ jhipster --defaults
.
.
.
identical src/test/java/com/mycompany/myapp/service/UserServiceIT.java
identical .lintstagedrc.cjs
$ echo $?
0

do you mind explaining a bit more on why you think this is related to the pipeline?

Looks related to pipeline transforms like:

      Duplex.from(async function* (files: AsyncGenerator<MemFsEditorFile>) {
        for await (const file of files) {
          if (isFilePending(file)) {
            yield file;
          }
        }
      }),

And how do I test with node v22.4 vs v22.x

I use n to switch between node versions.

n 22.12.0
jhipster --defaults

n 22.4.0
jhipster --defaults

would also be good if you could also pin point me to the code in the generator-jhipster project that you think might be the issue. Cheers.

It's not a single place, yeoman/generator#1577, SBoudrias/mem-fs-editor#282, SBoudrias/mem-fs#40.
With those changes the error is correctly shown in node 22.12.0.

@jakecastelli
Copy link
Member Author

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?

@mcollina mcollina added the commit-queue Add this label to land a pull request using GitHub Actions. label Dec 19, 2024
@nodejs-github-bot nodejs-github-bot removed the commit-queue Add this label to land a pull request using GitHub Actions. label Dec 19, 2024
@nodejs-github-bot nodejs-github-bot merged commit fd8de67 into nodejs:main Dec 19, 2024
59 checks passed
@nodejs-github-bot
Copy link
Collaborator

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
Copy link
Member

@dario-piotrowicz dario-piotrowicz Jan 26, 2025

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?

Copy link
Member Author

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 👍

Copy link
Member Author

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

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

10 participants