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

Merged
merged 1 commit into from
Dec 19, 2024

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 ronag and lpinca 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.

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.

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

Had some unrelated coverage failure on Windows, rebased and rerun everything

@StefanStojanovic
Copy link
Contributor

Had some unrelated coverage failure on Windows, rebased and rerun everything

This error is related to the VS v17.12 which was released a week ago and has a compiler change making Node.js compilation impossible. I've already opened an issue to track this in build: nodejs/build#3963. I've also reported this to Microsoft via the developer community issue.

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

@nodejs-github-bot
Copy link
Collaborator

@nodejs-github-bot
Copy link
Collaborator

nodejs-github-bot commented Nov 29, 2024

@jakecastelli
Copy link
Member Author

CI is green again, can I get another approval or can someone take another look (since I need to rebase to pass the GHA) 🙏

@jakecastelli jakecastelli removed the errors Issues and PRs related to JavaScript errors originated in Node.js core. label Dec 5, 2024
@jakecastelli
Copy link
Member Author

This PR is technically ready to land, would you mind taking another quick look on CITGM @mcollina

Also need another approval since this PR needed a rebase

@mshima
Copy link

mshima commented Dec 10, 2024

Any news about this PR?
In yo/jhipster we use a lot of streams and pipeline in objectMode.
The result is that the program hang silently with code 0 and no error is shown.

We couldn’t found a workaround. And I couldn’t confirm if this PR fixes the issue.

@jakecastelli
Copy link
Member Author

Hi @mshima I have a few questions here:

  1. what node version does you folks use?
  2. do you have a minimal repro I can look into? or steps to repro the issue you mentioned

@mshima
Copy link

mshima commented Dec 11, 2024

@jakecastelli thanks for your attention.

In my tests, node 22.4.0 works as expected.
The issue started in v22.5.0 and happens in v22.12.0.

Creating a minimal reproduction to match our use case is quite difficult, but seems likely this issue.
To reproduce we need to force an error in pipeline by creating an error:

npm install -g [email protected]
vim $(jhipster --install-path)/generators/angular/templates/jest.conf.js.ejs

replace:

const { pathsToModuleNameMapper } = require('ts-jest');

with (to make prettier fail):

const { pathsToModuleNameMapper } require('ts-jest');

Generate with offending file in an empty folder:

jhipster --defaults

@jakecastelli
Copy link
Member Author

Thanks for providing some more info, I haven't run the script yet to install the pkg, could you confirm if the package name is correct? I searched on npm and found this https://www.npmjs.com/package/jhipster?activeTab=readme and is probably not the one I suppose to install

@mshima
Copy link

mshima commented Dec 11, 2024

@jakecastelli sorry it’s generator-jhipster

@jakecastelli
Copy link
Member Author

This is the error I've got, do you mind explaining a bit more on why you think this is related to the pipeline? And how do I test with node v22.4 vs v22.x, 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.

error
ERROR! ERROR! Error parsing file jest.conf.js: SyntaxError: Missing initializer in destructuring declaration. (1:34)
> 1 | const { pathsToModuleNameMapper } require('ts-jest');
    |                                  ^
  2 |
  3 | const {
  4 |   compilerOptions: { paths = {}, baseUrl = './' },

At: 1: const { pathsToModuleNameMapper } require('ts-jest');
2:
3: const {
4:   compilerOptions: { paths = {}, baseUrl = './' },
5: } = require('./tsconfig.json');
6: const environment = require('./webpack/environment');
7:
8: module.exports = {
9:   transformIgnorePatterns: ['node_modules/(?!.*\\.mjs$|dayjs/esm)'],
10:   resolver: 'jest-preset-angular/build/resolvers/ng-jest-resolver.js',
11:   globals: {
12:     ...environment,
13:   },
14:   roots: ['<rootDir>', `<rootDir>/${baseUrl}`],
15:   modulePaths: [`<rootDir>/${baseUrl}`],
16:   setupFiles: ['jest-date-mock'],
17:   cacheDirectory: '<rootDir>/target/jest-cache',
18:   coverageDirectory: '<rootDir>/target/test-results/',
19:   moduleNameMapper: pathsToModuleNameMapper(paths, { prefix: `<rootDir>/${baseUrl}/` }),
20:   reporters: [
21:     'default',
22:     ['jest-junit', { outputDirectory: '<rootDir>/target/test-results/', outputName: 'TESTS-results-jest.xml' }],
23:     ['jest-sonar', { outputDirectory: './target/test-results/jest', outputName: 'TESTS-results-sonar.xml' }],
24:   ],
25:   testMatch: ['<rootDir>/src/main/webapp/app/**/@(*.)@(spec.ts)'],
26:   testEnvironmentOptions: {
27:     url: 'https://jhipster.tech',
28:   },
29: };
30:
Error: Error parsing file jest.conf.js: SyntaxError: Missing initializer in destructuring declaration. (1:34)
> 1 | const { pathsToModuleNameMapper } require('ts-jest');
    |                                  ^
  2 |
  3 | const {
  4 |   compilerOptions: { paths = {}, baseUrl = './' },

At: 1: const { pathsToModuleNameMapper } require('ts-jest');
2:
3: const {
4:   compilerOptions: { paths = {}, baseUrl = './' },
5: } = require('./tsconfig.json');
6: const environment = require('./webpack/environment');
7:
8: module.exports = {
9:   transformIgnorePatterns: ['node_modules/(?!.*\\.mjs$|dayjs/esm)'],
10:   resolver: 'jest-preset-angular/build/resolvers/ng-jest-resolver.js',
11:   globals: {
12:     ...environment,
13:   },
14:   roots: ['<rootDir>', `<rootDir>/${baseUrl}`],
15:   modulePaths: [`<rootDir>/${baseUrl}`],
16:   setupFiles: ['jest-date-mock'],
17:   cacheDirectory: '<rootDir>/target/jest-cache',
18:   coverageDirectory: '<rootDir>/target/test-results/',
19:   moduleNameMapper: pathsToModuleNameMapper(paths, { prefix: `<rootDir>/${baseUrl}/` }),
20:   reporters: [
21:     'default',
22:     ['jest-junit', { outputDirectory: '<rootDir>/target/test-results/', outputName: 'TESTS-results-jest.xml' }],
23:     ['jest-sonar', { outputDirectory: './target/test-results/jest', outputName: 'TESTS-results-sonar.xml' }],
24:   ],
25:   testMatch: ['<rootDir>/src/main/webapp/app/**/@(*.)@(spec.ts)'],
26:   testEnvironmentOptions: {
27:     url: 'https://jhipster.tech',
28:   },
29: };

@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

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
9 participants