Skip to content

Commit

Permalink
stream: fix regression on duplex end
Browse files Browse the repository at this point in the history
Backport
Decide the return status of writeOrBuffer before
calling stream.write which can reset state.length

Refs: nodejs#35941
Fixes: nodejs#35926
  • Loading branch information
mmomtchev committed Nov 22, 2020
1 parent 1e8dfb9 commit c6fa9a6
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
12 changes: 6 additions & 6 deletions lib/_stream_writable.js
Expand Up @@ -336,6 +336,12 @@ function writeOrBuffer(stream, state, chunk, encoding, callback) {

state.length += len;

// stream._write resets state.length
const ret = state.length < state.highWaterMark;
// We must ensure that previous needDrain will not be reset to false.
if (!ret)
state.needDrain = true;

if (state.writing || state.corked || state.errored) {
state.buffered.push({ chunk, encoding, callback });
if (state.allBuffers && encoding !== 'buffer') {
Expand All @@ -353,12 +359,6 @@ function writeOrBuffer(stream, state, chunk, encoding, callback) {
state.sync = false;
}

const ret = state.length < state.highWaterMark;

// We must ensure that previous needDrain will not be reset to false.
if (!ret)
state.needDrain = true;

// Return false if errored or destroyed in order to break
// any synchronous while(stream.write(data)) loops.
return ret && !state.errored && !state.destroyed;
Expand Down
32 changes: 32 additions & 0 deletions test/parallel/test-stream-duplex-readable-end.js
@@ -0,0 +1,32 @@
'use strict';
// https://github.com/nodejs/node/issues/35926
require('../common');
const assert = require('assert');
const stream = require('stream');

let loops = 5;

const src = new stream.Readable({
read() {
if (loops--)
this.push(Buffer.alloc(20000));
}
});

const dst = new stream.Transform({
transform(chunk, output, fn) {
this.push(null);
fn();
}
});

src.pipe(dst);

function parser_end() {
assert.ok(loops > 0);
dst.removeAllListeners();
}

dst.on('data', () => { });
dst.on('end', parser_end);
dst.on('error', parser_end);

0 comments on commit c6fa9a6

Please sign in to comment.