Skip to content

Commit 60c71dc

Browse files
jasnellcodebytere
authored andcommittedFeb 27, 2020
test: add known issue test for sync writable callback
If the write callbacks are invoked synchronously with an error, onwriteError would cause the error event to be emitted synchronously, making it impossible to attach an error handler after the call that triggered it. PR-URL: #31756 Refs: nodejs/quic@b0d469c Refs: nodejs/quic#341 Reviewed-By: Robert Nagy <ronagy@icloud.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Minwoo Jung <nodecorelab@gmail.com>
1 parent f952605 commit 60c71dc

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
'use strict';
2+
const common = require('../common');
3+
4+
// Tests for the regression in _stream_writable discussed in
5+
// https://github.com/nodejs/node/pull/31756
6+
7+
// Specifically, when a write callback is invoked synchronously
8+
// with an error, and autoDestroy is not being used, the error
9+
// should still be emitted on nextTick.
10+
11+
const { Writable } = require('stream');
12+
13+
class MyStream extends Writable {
14+
#cb = undefined;
15+
16+
constructor() {
17+
super({ autoDestroy: false });
18+
}
19+
20+
_write(_, __, cb) {
21+
this.#cb = cb;
22+
}
23+
24+
close() {
25+
// Synchronously invoke the callback with an error.
26+
this.#cb(new Error('foo'));
27+
}
28+
}
29+
30+
const stream = new MyStream();
31+
32+
const mustError = common.mustCall(2);
33+
34+
stream.write('test', () => {});
35+
36+
// Both error callbacks should be invoked.
37+
38+
stream.on('error', mustError);
39+
40+
stream.close();
41+
42+
// Without the fix in #31756, the error handler
43+
// added after the call to close will not be invoked.
44+
stream.on('error', mustError);

0 commit comments

Comments
 (0)
Please sign in to comment.