Skip to content

Commit 695db97

Browse files
ronagTrott
authored andcommitted
doc: explain stream.finished cleanup
PR-URL: #28935 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent b82ffd9 commit 695db97

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

doc/api/stream.md

+20
Original file line numberDiff line numberDiff line change
@@ -1481,6 +1481,8 @@ added: v10.0.0
14811481
**Default**: `true`.
14821482
* `callback` {Function} A callback function that takes an optional error
14831483
argument.
1484+
* Returns: {Function} A cleanup function which removes all registered
1485+
listeners.
14841486

14851487
A function to get notified when a stream is no longer readable, writable
14861488
or has experienced an error or a premature close event.
@@ -1521,6 +1523,20 @@ run().catch(console.error);
15211523
rs.resume(); // Drain the stream.
15221524
```
15231525

1526+
`stream.finished()` leaves dangling event listeners (in particular
1527+
`'error'`, `'end'`, `'finish'` and `'close'`) after `callback` has been
1528+
invoked. The reason for this is so that unexpected `'error'` events (due to
1529+
incorrect stream implementations) do not cause unexpected crashes.
1530+
If this is unwanted behavior then the returned cleanup function needs to be
1531+
invoked in the callback:
1532+
1533+
```js
1534+
const cleanup = finished(...streams, err => {
1535+
cleanup();
1536+
// ...
1537+
});
1538+
```
1539+
15241540
### stream.pipeline(...streams, callback)
15251541
<!-- YAML
15261542
added: v10.0.0
@@ -1574,6 +1590,10 @@ async function run() {
15741590
run().catch(console.error);
15751591
```
15761592

1593+
`stream.pipeline()` leaves dangling event listeners on the streams
1594+
after the `callback` has been invoked. In the case of reuse of streams after
1595+
failure, this can cause event listener leaks and swallowed errors.
1596+
15771597
### stream.Readable.from(iterable, [options])
15781598
<!-- YAML
15791599
added: v12.3.0

0 commit comments

Comments
 (0)