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: avoid unhandled exception when destroy w/ callback. #30970

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions lib/internal/streams/destroy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

const nop = () => {};

function needError(stream, err) {
if (!err) {
return false;
Expand Down Expand Up @@ -52,6 +54,11 @@ function destroy(err, cb) {
r.destroyed = true;
}

if (cb) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps here and elsewhere in this function we should instead check typeof cb === 'function'?

Copy link
Member Author

@ronag ronag Dec 15, 2019

Choose a reason for hiding this comment

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

We don't do that anywhere else in this file. If you'd prefer it like that, I would prefer to handle that in a separate PR and change it everywhere in this file.

// Error is handled through callback. Avoid unhandled exception.
this.on('error', nop);
}

this._destroy(err || null, (err) => {
const emitClose = (w && w.emitClose) || (r && r.emitClose);
if (cb) {
Expand Down
10 changes: 10 additions & 0 deletions test/parallel/test-stream-writable-destroy.js
Original file line number Diff line number Diff line change
Expand Up @@ -344,3 +344,13 @@ const assert = require('assert');
}));
write.destroy(new Error('asd'));
}

{
const write = new Writable({
write(chunk, enc, cb) { cb(); }
});
// Make sure no unhandled exception.
write.destroy(new Error('asd'), common.mustCall((err) => {
assert.strictEqual(err.message, 'asd');
}));
}