GeezerVison is showing github.com — console: avoid adding infinite error listeners · nodejs/node@bed0560
File tree
Expand file treeCollapse file tree
Open diff view settings
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: lib/console.js +4-1Lines changed: 4 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -83,7 +83,10 @@ function createWriteErrorHandler(stream) { |
83 | 83 | // an `error` event. Adding a `once` listener will keep that error |
84 | 84 | // from becoming an uncaught exception, but since the handler is |
85 | 85 | // removed after the event, non-console.* writes won’t be affected. |
86 | | - stream.once('error', noop); |
| 86 | + // we are only adding noop if there is no one else listening for 'error' |
| 87 | + if (stream.listenerCount('error') === 0) { |
| 88 | + stream.on('error', noop); |
| 89 | + } |
87 | 90 | } |
88 | 91 | }; |
89 | 92 | } |
|
Collapse file
Copy file name to clipboard +24Lines changed: 24 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const common = require('../common'); |
| 4 | +const { Writable } = require('stream'); |
| 5 | +const { Console } = require('console'); |
| 6 | +const { EventEmitter } = require('events'); |
| 7 | + |
| 8 | +const stream = new Writable({ |
| 9 | + write(chunk, enc, cb) { |
| 10 | + cb(); |
| 11 | + }, |
| 12 | + writev(chunks, cb) { |
| 13 | + setTimeout(cb, 10, new Error('kaboom')); |
| 14 | + } |
| 15 | +}); |
| 16 | +const myConsole = new Console(stream, stream); |
| 17 | + |
| 18 | +process.on('warning', common.mustNotCall); |
| 19 | + |
| 20 | +stream.cork(); |
| 21 | +for (let i = 0; i < EventEmitter.defaultMaxListeners + 1; i++) { |
| 22 | + myConsole.log('a message'); |
| 23 | +} |
| 24 | +stream.uncork(); |