-
Notifications
You must be signed in to change notification settings - Fork 30k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
stream: ensure awaitDrain is increased once
Guard against the call to write() inside pipe's ondata pushing more data back onto the Readable, thus causing ondata to be called again. This is fine but results in awaitDrain being increased more than once. The problem with that is when the destination does drain, only a single 'drain' event is emitted, so awaitDrain in this case will never reach zero and we end up with a permanently paused stream. Fixes: #7278 PR-URL: #7292 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
- Loading branch information
1 parent
ad1045c
commit b0225e5
Showing
2 changed files
with
36 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
test/parallel/test-stream-pipe-await-drain-push-while-write.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const stream = require('stream'); | ||
|
||
// A writable stream which pushes data onto the stream which pipes into it, | ||
// but only the first time it's written to. Since it's not paused at this time, | ||
// a second write will occur. If the pipe increases awaitDrain twice, we'll | ||
// never get subsequent chunks because 'drain' is only emitted once. | ||
const writable = new stream.Writable({ | ||
write: common.mustCall((chunk, encoding, cb) => { | ||
if (chunk.length === 32 * 1024) { // first chunk | ||
readable.push(new Buffer(33 * 1024)); // above hwm | ||
} | ||
cb(); | ||
}, 3) | ||
}); | ||
|
||
// A readable stream which produces two buffers. | ||
const bufs = [new Buffer(32 * 1024), new Buffer(33 * 1024)]; // above hwm | ||
const readable = new stream.Readable({ | ||
read: function() { | ||
while (bufs.length > 0) { | ||
this.push(bufs.shift()); | ||
} | ||
} | ||
}); | ||
|
||
readable.pipe(writable); |