Skip to content

Commit ae91ffe

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 <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>
1 parent 7842f63 commit ae91ffe

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
@@ -502,6 +502,7 @@ function clearBuffer(stream, state) {
502502
corkReq.finish = onCorkedFinish.bind(undefined, corkReq, state);
503503
state.corkedRequestsFree = corkReq;
504504
}
505+
state.bufferedRequestCount = 0;
505506
} else {
506507
// Slow case, write chunks one-by-one
507508
while (entry) {
@@ -512,6 +513,7 @@ function clearBuffer(stream, state) {
512513

513514
doWrite(stream, state, false, len, chunk, encoding, cb);
514515
entry = entry.next;
516+
state.bufferedRequestCount--;
515517
// if we didn't call the onwrite immediately, then
516518
// it means that we need to wait until it does.
517519
// also, that means that the chunk and cb are currently
@@ -525,7 +527,6 @@ function clearBuffer(stream, state) {
525527
state.lastBufferedRequest = null;
526528
}
527529

528-
state.bufferedRequestCount = 0;
529530
state.bufferedRequest = entry;
530531
state.bufferProcessing = false;
531532
}
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)