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

console: avoid adding infinite error listeners #16770

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
5 changes: 4 additions & 1 deletion lib/console.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,10 @@ function createWriteErrorHandler(stream) {
// an `error` event. Adding a `once` listener will keep that error
// from becoming an uncaught exception, but since the handler is
// removed after the event, non-console.* writes won’t be affected.
stream.once('error', noop);
// we are only adding noop if there is no one else listening for 'error'
Copy link
Contributor

Choose a reason for hiding this comment

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

if this can happen only with writev(), its worth mentioning specifically in the comment

if (stream.listenerCount('error') === 0) {
stream.on('error', noop);
}
}
};
}
Expand Down
24 changes: 24 additions & 0 deletions test/parallel/test-console-log-stdio-broken-dest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use strict';

const common = require('../common');
const { Writable } = require('stream');
const { Console } = require('console');
const { EventEmitter } = require('events');

const stream = new Writable({
write(chunk, enc, cb) {
cb();
},
writev(chunks, cb) {
setTimeout(cb, 10, new Error('kaboom'));
}
});
const myConsole = new Console(stream, stream);

process.on('warning', common.mustNotCall);

stream.cork();
for (let i = 0; i < EventEmitter.defaultMaxListeners + 1; i++) {
myConsole.log('a message');
}
stream.uncork();