Skip to content

Commit

Permalink
stream: do not emit drain if stream ended
Browse files Browse the repository at this point in the history
PR-URL: #29086
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
  • Loading branch information
ronag authored and BridgeAR committed Sep 3, 2019
1 parent 293c9f0 commit b36a6e9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
10 changes: 6 additions & 4 deletions lib/_stream_writable.js
Original file line number Diff line number Diff line change
Expand Up @@ -480,15 +480,17 @@ function onwrite(stream, er) {
}

if (sync) {
process.nextTick(afterWrite, stream, state, finished, cb);
process.nextTick(afterWrite, stream, state, cb);
} else {
afterWrite(stream, state, finished, cb);
afterWrite(stream, state, cb);
}
}
}

function afterWrite(stream, state, finished, cb) {
if (!finished && state.length === 0 && state.needDrain) {
function afterWrite(stream, state, cb) {
const needDrain = !state.ending && !stream.destroyed && state.length === 0 &&
state.needDrain;
if (needDrain) {
state.needDrain = false;
stream.emit('drain');
}
Expand Down
16 changes: 16 additions & 0 deletions test/parallel/test-stream-write-drain.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';
const common = require('../common');
const { Writable } = require('stream');

// Don't emit 'drain' if ended

const w = new Writable({
write(data, enc, cb) {
process.nextTick(cb);
},
highWaterMark: 1
});

w.on('drain', common.mustNotCall());
w.write('asd');
w.end();

0 comments on commit b36a6e9

Please sign in to comment.