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

http.ServerResponse doesn't invoke error callbacks when an object is received from the source stream #34876

Closed
Noor0 opened this issue Aug 22, 2020 · 5 comments
Labels
stream Issues and PRs related to the stream subsystem.

Comments

@Noor0
Copy link

Noor0 commented Aug 22, 2020

ServerResponse doesn't invoke error callbacks when an object is received from the source stream.

  • Version: 12.18.3
  • Platform: Linux user 5.4.0-42-generic Tracking / Assuring Compatibility #46~18.04.1-Ubuntu SMP Fri Jul 10 07:21:24 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
  • Subsystem: streams, http

What steps will reproduce the bug?

pipe an object from source stream to ServerResponse

const server = http.createServer((req, res) => {
  const stream = new Readable({
    objectMode: true,
    read() {
      this.push({ a: "b" });
      this.push(null);
    },
  });

  stream.on("error", (e) => console.log("Error in stream"));
  res.on("error", (e) => console.log("Error in response stream"));

  stream.pipe(res);
  // server crashes and error handlers are not called
});

server.listen(3000);

How often does it reproduce? Is there a required condition?

pipe an object from source stream to ServerResponse

What is the expected behavior?

error callbacks attached to res should be called.

What do you see instead?

Process crashes with the following error
_http_outgoing.js:653
throw new ERR_INVALID_ARG_TYPE('first argument',
^

TypeError [ERR_INVALID_ARG_TYPE]: The first argument must be of type string or an instance of Buffer. Received an instance of Object
at write_ (_http_outgoing.js:653:11)
at ServerResponse.write (_http_outgoing.js:621:15)
at Readable.ondata (_stream_readable.js:716:22)
at Readable.emit (events.js:315:20)
at Readable.read (_stream_readable.js:505:10)
at flow (stream_readable.js:1005:34)
at resume
(_stream_readable.js:986:3)
at processTicksAndRejections (internal/process/task_queues.js:84:21) {
code: 'ERR_INVALID_ARG_TYPE'
}

@ronag
Copy link
Member

ronag commented Aug 22, 2020

This works as intended. It was decided that we throw on write with invalid type. There’s been quite a lot of back in forth on this.

@nodejs/streams in case anyone have further thoughts.

@Noor0
Copy link
Author

Noor0 commented Aug 22, 2020

Oh, Thanks for letting me know I'll close this then

@ronag
Copy link
Member

ronag commented Aug 22, 2020

@mcollina one reason not to throw and go with the usual destroy(err) path.

@mcollina
Copy link
Member

We can fix this by wrapping the .write() within pipe() in a try catch and route that to .destroy().

@ronag ronag reopened this Aug 22, 2020
@ronag ronag added the stream Issues and PRs related to the stream subsystem. label Aug 22, 2020
@ronag
Copy link
Member

ronag commented Sep 23, 2020

As mentioned in the referenced PR. This is a won't fix.

@ronag ronag closed this as completed Sep 23, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stream Issues and PRs related to the stream subsystem.
Projects
None yet
Development

No branches or pull requests

3 participants