Skip to content
Open
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
7 changes: 7 additions & 0 deletions lib/internal/streams/writable.js
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,10 @@ function onwrite(stream, er) {
state.length -= state.writelen;
state.writelen = 0;

if (!er && (state[kState] & kDestroyed) !== 0) {
er = state[kErroredValue] ?? new ERR_STREAM_DESTROYED('write');
}

if (er) {
// Avoid V8 leak, https://github.com/nodejs/node/pull/34103#issuecomment-652002364
er.stack; // eslint-disable-line no-unused-expressions
Expand Down Expand Up @@ -889,6 +893,9 @@ function onFinish(stream, state, err) {
return;
}
state.pendingcb--;
if (!err && (state[kState] & kDestroyed) !== 0) {
err = state[kErroredValue] ?? new ERR_STREAM_DESTROYED('end');
}
if (err) {
callFinishedCallbacks(state, err);
errorOrDestroy(stream, err, (state[kState] & kSync) !== 0);
Expand Down
18 changes: 18 additions & 0 deletions test/parallel/test-stream-writable-destroy.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,24 @@ const assert = require('assert');
assert.strictEqual(write.destroyed, true);
}

{
const write = new Writable({
write(chunk, enc, cb) {
this.destroy();
cb();
}
});

write.on('error', common.mustNotCall());
write.on('finish', common.mustNotCall());
write.write('asd', common.expectsError({
code: 'ERR_STREAM_DESTROYED',
name: 'Error',
message: 'Cannot call write after a stream was destroyed'
}));
assert.strictEqual(write.destroyed, true);
}

{
const write = new Writable({
write(chunk, enc, cb) { cb(); }
Expand Down
21 changes: 21 additions & 0 deletions test/parallel/test-stream-writable-final-destroy.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,24 @@ const { Writable } = require('stream');
w.on('finish', common.mustNotCall());
w.on('close', common.mustCall());
}

{
const w = new Writable({
write(chunk, encoding, callback) {
callback(null);
},
final(callback) {
this.destroy();
callback();
}
});

w.on('error', common.mustNotCall());
w.on('finish', common.mustNotCall());
w.on('close', common.mustCall());
w.end(common.expectsError({
code: 'ERR_STREAM_DESTROYED',
name: 'Error',
message: 'Cannot call end after a stream was destroyed'
}));
}
Loading