Skip to content

Commit ffde243

Browse files
committed
doc: handle backpressure when write() return false
The doc specified that writable.write() was advisory only. However, ignoring that value might lead to memory leaks. This PR specifies that behavior. Moreover, it adds an example on how to listen for the 'drain' event correctly. See: nodejs@f347dad PR-URL: nodejs#10631 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Sam Roberts <vieuxtech@gmail.com> Reviewed-By: Evan Lucas <evanlucas@me.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
1 parent 0fe9ac3 commit ffde243

1 file changed

Lines changed: 42 additions & 4 deletions

File tree

doc/api/stream.md

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -727,11 +727,49 @@ The return value indicates if you should continue writing right now.
727727
If the data had to be buffered internally, then it will return
728728
`false`. Otherwise, it will return `true`.
729729

730-
This return value is strictly advisory. You MAY continue to write,
731-
even if it returns `false`. However, writes will be buffered in
732-
memory, so it is best not to do this excessively. Instead, wait for
733-
the [`'drain'`][] event before writing more data.
730+
The return value is `true` if the internal buffer does not exceed
731+
`highWaterMark` configured when the stream was created after admitting `chunk`.
732+
If `false` is returned, further attempts to write data to the stream should
733+
stop until the [`'drain'`][] event is emitted.
734+
735+
While a stream is not draining, calls to `write()` will buffer `chunk`, and
736+
return false. Once all currently buffered chunks are drained (accepted for
737+
delivery by the operating system), the `'drain'` event will be emitted.
738+
It is recommended that once write() returns false, no more chunks be written
739+
until the `'drain'` event is emitted. While calling `write()` on a stream that
740+
is not draining is allowed, Node.js will buffer all written chunks until
741+
maximum memory usage occurs, at which point it will abort unconditionally.
742+
Even before it aborts, high memory usage will cause poor garbage collector
743+
performance and high RSS (which is not typically released back to the system,
744+
even after the memory is no longer required). Since TCP sockets may never
745+
drain if the remote peer does not read the data, writing a socket that is
746+
not draining may lead to a remotely exploitable vulnerability.
747+
748+
Writing data while the stream is not draining is particularly
749+
problematic for a [Transform][], because the `Transform` streams are paused
750+
by default until they are piped or an `'data'` or `'readable'` event handler
751+
is added.
752+
753+
If the data to be written can be generated or fetched on demand, it is
754+
recommended to encapsulate the logic into a [Readable][] and use
755+
[`stream.pipe()`][]. However, if calling `write()` is preferred, it is
756+
possible to respect backpressure and avoid memory issues using the
757+
the [`'drain'`][] event:
734758

759+
```js
760+
function write (data, cb) {
761+
if (!stream.write(data)) {
762+
stream.once('drain', cb)
763+
} else {
764+
process.nextTick(cb)
765+
}
766+
}
767+
768+
// Wait for cb to be called before doing any other write.
769+
write('hello', () => {
770+
console.log('write completed, do more writes now')
771+
})
772+
```
735773

736774
## API for Stream Implementors
737775

0 commit comments

Comments
 (0)