stream: fix disparity between buffer and the count
This changes the disparity of bufferedRequestCount and the actual buffer on file _stream_writable.js PR-URL: https://github.com/nodejs/node/pull/15661 Fixes: https://github.com/nodejs/node/issues/6758 Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
parent
5dd65839a9
commit
34dbc9e4e8
@ -503,6 +503,7 @@ function clearBuffer(stream, state) {
|
|||||||
corkReq.finish = onCorkedFinish.bind(undefined, corkReq, state);
|
corkReq.finish = onCorkedFinish.bind(undefined, corkReq, state);
|
||||||
state.corkedRequestsFree = corkReq;
|
state.corkedRequestsFree = corkReq;
|
||||||
}
|
}
|
||||||
|
state.bufferedRequestCount = 0;
|
||||||
} else {
|
} else {
|
||||||
// Slow case, write chunks one-by-one
|
// Slow case, write chunks one-by-one
|
||||||
while (entry) {
|
while (entry) {
|
||||||
@ -513,6 +514,7 @@ function clearBuffer(stream, state) {
|
|||||||
|
|
||||||
doWrite(stream, state, false, len, chunk, encoding, cb);
|
doWrite(stream, state, false, len, chunk, encoding, cb);
|
||||||
entry = entry.next;
|
entry = entry.next;
|
||||||
|
state.bufferedRequestCount--;
|
||||||
// if we didn't call the onwrite immediately, then
|
// if we didn't call the onwrite immediately, then
|
||||||
// it means that we need to wait until it does.
|
// it means that we need to wait until it does.
|
||||||
// also, that means that the chunk and cb are currently
|
// also, that means that the chunk and cb are currently
|
||||||
@ -526,7 +528,6 @@ function clearBuffer(stream, state) {
|
|||||||
state.lastBufferedRequest = null;
|
state.lastBufferedRequest = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
state.bufferedRequestCount = 0;
|
|
||||||
state.bufferedRequest = entry;
|
state.bufferedRequest = entry;
|
||||||
state.bufferProcessing = false;
|
state.bufferProcessing = false;
|
||||||
}
|
}
|
||||||
|
34
test/sequential/test-stream-writable-clear-buffer.js
Normal file
34
test/sequential/test-stream-writable-clear-buffer.js
Normal file
@ -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();
|
Loading…
x
Reference in New Issue
Block a user