Skip to content

Commit 20d009d

Browse files
ronagBridgeAR
authored andcommitted
stream: pipe should not swallow error
PR-URL: #30993 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
1 parent db9539b commit 20d009d

File tree

3 files changed

+30
-5
lines changed

3 files changed

+30
-5
lines changed

lib/_stream_readable.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -798,7 +798,9 @@ Readable.prototype.pipe = function(dest, pipeOpts) {
798798
unpipe();
799799
dest.removeListener('error', onerror);
800800
if (EE.listenerCount(dest, 'error') === 0) {
801-
if (!dest.destroyed) {
801+
const s = dest._writableState || dest._readableState;
802+
if (s && !s.errorEmitted) {
803+
// User incorrectly emitted 'error' directly on the stream.
802804
errorOrDestroy(dest, er);
803805
} else {
804806
dest.emit('error', er);
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict';
2+
const common = require('../common');
3+
const stream = require('stream');
4+
5+
process.on('uncaughtException', common.mustCall());
6+
7+
const r = new stream.Readable();
8+
r._read = function(size) {
9+
r.push(Buffer.allocUnsafe(size));
10+
};
11+
12+
const w = new stream.Writable();
13+
w._write = function(data, encoding, cb) {
14+
cb(null);
15+
};
16+
17+
r.pipe(w);
18+
19+
// end() after pipe should cause unhandled exception
20+
w.end();

test/parallel/test-stream2-finish-pipe.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ w._write = function(data, encoding, cb) {
3535

3636
r.pipe(w);
3737

38-
// This might sound unrealistic, but it happens in net.js. When
39-
// `socket.allowHalfOpen === false`, EOF will cause `.destroySoon()` call which
40-
// ends the writable side of net.Socket.
41-
w.end();
38+
// end() must be called in nextTick or a WRITE_AFTER_END error occurs.
39+
process.nextTick(() => {
40+
// This might sound unrealistic, but it happens in net.js. When
41+
// socket.allowHalfOpen === false, EOF will cause .destroySoon() call which
42+
// ends the writable side of net.Socket.
43+
w.end();
44+
});

0 commit comments

Comments
 (0)