GeezerVison is showing github.com — stream: ensure awaitDrain is increased once · nodejs/node@a08a017
File tree
Expand file treeCollapse file tree
Open diff view settings
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: lib/_stream_readable.js +8-1Lines changed: 8 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -530,11 +530,17 @@ Readable.prototype.pipe = function(dest, pipeOpts) { |
530 | 530 | ondrain(); |
531 | 531 | } |
532 | 532 |
|
| 533 | + // If the user pushes more data while we're writing to dest then we'll end up |
| 534 | + // in ondata again. However, we only want to increase awaitDrain once because |
| 535 | + // dest will only emit one 'drain' event for the multiple writes. |
| 536 | + // => Introduce a guard on increasing awaitDrain. |
| 537 | + var increasedAwaitDrain = false; |
533 | 538 | src.on('data', ondata); |
534 | 539 | function ondata(chunk) { |
535 | 540 | debug('ondata'); |
| 541 | + increasedAwaitDrain = false; |
536 | 542 | var ret = dest.write(chunk); |
537 | | - if (false === ret) { |
| 543 | + if (false === ret && !increasedAwaitDrain) { |
538 | 544 | // If the user unpiped during `dest.write()`, it is possible |
539 | 545 | // to get stuck in a permanently paused state if that write |
540 | 546 | // also returned false. |
@@ -544,6 +550,7 @@ Readable.prototype.pipe = function(dest, pipeOpts) { |
544 | 550 | !cleanedUp) { |
545 | 551 | debug('false write response, pause', src._readableState.awaitDrain); |
546 | 552 | src._readableState.awaitDrain++; |
| 553 | + increasedAwaitDrain = true; |
547 | 554 | } |
548 | 555 | src.pause(); |
549 | 556 | } |
|
Collapse file
Copy file name to clipboard +28Lines changed: 28 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
| 1 | +'use strict'; |
| 2 | +const common = require('../common'); |
| 3 | +const stream = require('stream'); |
| 4 | + |
| 5 | +// A writable stream which pushes data onto the stream which pipes into it, |
| 6 | +// but only the first time it's written to. Since it's not paused at this time, |
| 7 | +// a second write will occur. If the pipe increases awaitDrain twice, we'll |
| 8 | +// never get subsequent chunks because 'drain' is only emitted once. |
| 9 | +const writable = new stream.Writable({ |
| 10 | + write: common.mustCall((chunk, encoding, cb) => { |
| 11 | + if (chunk.length === 32 * 1024) { // first chunk |
| 12 | + readable.push(new Buffer(33 * 1024)); // above hwm |
| 13 | + } |
| 14 | + cb(); |
| 15 | + }, 3) |
| 16 | +}); |
| 17 | + |
| 18 | +// A readable stream which produces two buffers. |
| 19 | +const bufs = [new Buffer(32 * 1024), new Buffer(33 * 1024)]; // above hwm |
| 20 | +const readable = new stream.Readable({ |
| 21 | + read: function() { |
| 22 | + while (bufs.length > 0) { |
| 23 | + this.push(bufs.shift()); |
| 24 | + } |
| 25 | + } |
| 26 | +}); |
| 27 | + |
| 28 | +readable.pipe(writable); |