Skip to content

Commit 3ba6464

Browse files
ronagBridgeAR
authored andcommitted
doc: explain stream.finished cleanup
PR-URL: #28935 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent 84b353c commit 3ba6464

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
@@ -1478,6 +1478,8 @@ added: v10.0.0
14781478
**Default**: `true`.
14791479
* `callback` {Function} A callback function that takes an optional error
14801480
argument.
1481+
* Returns: {Function} A cleanup function which removes all registered
1482+
listeners.
14811483

14821484
A function to get notified when a stream is no longer readable, writable
14831485
or has experienced an error or a premature close event.
@@ -1518,6 +1520,20 @@ run().catch(console.error);
15181520
rs.resume(); // Drain the stream.
15191521
```
15201522

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

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

0 commit comments

Comments
 (0)