Skip to content

Commit 521ea75

Browse files
jlviveroaddaleax
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: nodejs/node#15661 Fixes: nodejs/node#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 8a9f1d5 commit 521ea75

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

lib/_stream_writable.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,7 @@ function clearBuffer(stream, state) {
503503
corkReq.finish = onCorkedFinish.bind(undefined, corkReq, state);
504504
state.corkedRequestsFree = corkReq;
505505
}
506+
state.bufferedRequestCount = 0;
506507
} else {
507508
// Slow case, write chunks one-by-one
508509
while (entry) {
@@ -513,6 +514,7 @@ function clearBuffer(stream, state) {
513514

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

529-
state.bufferedRequestCount = 0;
530531
state.bufferedRequest = entry;
531532
state.bufferProcessing = false;
532533
}
Lines changed: 34 additions & 0 deletions
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)