Skip to content

Commit b6ce918

Browse files
jlviveroMylesBorins
authored andcommitted
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: #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]>
1 parent ef0213c commit b6ce918

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

lib/_stream_writable.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,7 @@ function clearBuffer(stream, state) {
429429
} else {
430430
state.corkedRequestsFree = new CorkedRequest(state);
431431
}
432+
state.bufferedRequestCount = 0;
432433
} else {
433434
// Slow case, write chunks one-by-one
434435
while (entry) {
@@ -439,6 +440,7 @@ function clearBuffer(stream, state) {
439440

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

455-
state.bufferedRequestCount = 0;
456457
state.bufferedRequest = entry;
457458
state.bufferProcessing = false;
458459
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
'use strict';
2+
const common = require('../common');
3+
const Stream = require('stream');
4+
// This test ensures that the _writeableState.bufferedRequestCount and
5+
// the actual buffered request count are the same
6+
const assert = require('assert');
7+
8+
class StreamWritable extends Stream.Writable {
9+
constructor() {
10+
super({ objectMode: true });
11+
}
12+
13+
// We need a timeout like on the original issue thread
14+
// otherwise the code will never reach our test case
15+
// this means this should go on the sequential folder.
16+
_write(chunk, encoding, cb) {
17+
setTimeout(cb, common.platformTimeout(10));
18+
}
19+
}
20+
21+
const testStream = new StreamWritable();
22+
testStream.cork();
23+
24+
for (let i = 1; i <= 5; i++) {
25+
testStream.write(i, function() {
26+
assert.strictEqual(
27+
testStream._writableState.bufferedRequestCount,
28+
testStream._writableState.getBuffer().length,
29+
'bufferedRequestCount variable is different from the actual length of' +
30+
' the buffer');
31+
});
32+
}
33+
34+
testStream.end();

0 commit comments

Comments
 (0)