Skip to content

Commit

Permalink
stream: fix disparity between buffer and the count
Browse files Browse the repository at this point in the history
This changes the disparity of bufferedRequestCount and the actual buffer
on file _stream_writable.js

PR-URL: #15661
Fixes: #6758
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
jlvivero authored and MylesBorins committed Dec 20, 2017
1 parent 8a32b04 commit 612baca
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/_stream_writable.js
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,7 @@ function clearBuffer(stream, state) {
} else {
state.corkedRequestsFree = new CorkedRequest(state);
}
state.bufferedRequestCount = 0;
} else {
// Slow case, write chunks one-by-one
while (entry) {
Expand All @@ -439,6 +440,7 @@ function clearBuffer(stream, state) {

doWrite(stream, state, false, len, chunk, encoding, cb);
entry = entry.next;
state.bufferedRequestCount--;
// if we didn't call the onwrite immediately, then
// it means that we need to wait until it does.
// also, that means that the chunk and cb are currently
Expand All @@ -452,7 +454,6 @@ function clearBuffer(stream, state) {
state.lastBufferedRequest = null;
}

state.bufferedRequestCount = 0;
state.bufferedRequest = entry;
state.bufferProcessing = false;
}
Expand Down
34 changes: 34 additions & 0 deletions test/sequential/test-stream-writable-clear-buffer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'use strict';
const common = require('../common');
const Stream = require('stream');
// This test ensures that the _writeableState.bufferedRequestCount and
// the actual buffered request count are the same
const assert = require('assert');

class StreamWritable extends Stream.Writable {
constructor() {
super({ objectMode: true });
}

// We need a timeout like on the original issue thread
// otherwise the code will never reach our test case
// this means this should go on the sequential folder.
_write(chunk, encoding, cb) {
setTimeout(cb, common.platformTimeout(10));
}
}

const testStream = new StreamWritable();
testStream.cork();

for (let i = 1; i <= 5; i++) {
testStream.write(i, function() {
assert.strictEqual(
testStream._writableState.bufferedRequestCount,
testStream._writableState.getBuffer().length,
'bufferedRequestCount variable is different from the actual length of' +
' the buffer');
});
}

testStream.end();

0 comments on commit 612baca

Please sign in to comment.