Skip to content

Commit d111d7b

Browse files
addaleaxBridgeAR
authored andcommitted
stream: give error message if write() cb called twice
Otherwise, this condition would result in an error that just reads `cb is not a function`, and which additionally could have lost stack trace context through a `process.nextTick()` call. PR-URL: #19510 Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent cdfe47b commit d111d7b

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

lib/_stream_writable.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ const { getHighWaterMark } = require('internal/streams/state');
3737
const {
3838
ERR_INVALID_ARG_TYPE,
3939
ERR_METHOD_NOT_IMPLEMENTED,
40+
ERR_MULTIPLE_CALLBACK,
4041
ERR_STREAM_CANNOT_PIPE,
4142
ERR_STREAM_DESTROYED,
4243
ERR_STREAM_NULL_VALUES,
@@ -449,6 +450,9 @@ function onwrite(stream, er) {
449450
var sync = state.sync;
450451
var cb = state.writecb;
451452

453+
if (typeof cb !== 'function')
454+
throw new ERR_MULTIPLE_CALLBACK();
455+
452456
onwriteStateUpdate(state);
453457

454458
if (er)
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
'use strict';
2+
const common = require('../common');
3+
const { Writable } = require('stream');
4+
5+
{
6+
// Sync + Sync
7+
const writable = new Writable({
8+
write: common.mustCall((buf, enc, cb) => {
9+
cb();
10+
common.expectsError(cb, {
11+
code: 'ERR_MULTIPLE_CALLBACK',
12+
type: Error
13+
});
14+
})
15+
});
16+
writable.write('hi');
17+
}
18+
19+
{
20+
// Sync + Async
21+
const writable = new Writable({
22+
write: common.mustCall((buf, enc, cb) => {
23+
cb();
24+
process.nextTick(() => {
25+
common.expectsError(cb, {
26+
code: 'ERR_MULTIPLE_CALLBACK',
27+
type: Error
28+
});
29+
});
30+
})
31+
});
32+
writable.write('hi');
33+
}
34+
35+
{
36+
// Async + Async
37+
const writable = new Writable({
38+
write: common.mustCall((buf, enc, cb) => {
39+
process.nextTick(cb);
40+
process.nextTick(() => {
41+
common.expectsError(cb, {
42+
code: 'ERR_MULTIPLE_CALLBACK',
43+
type: Error
44+
});
45+
});
46+
})
47+
});
48+
writable.write('hi');
49+
}

0 commit comments

Comments
 (0)