Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

doc: stream.finished cleanup #28935

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions doc/api/stream.md
Original file line number Diff line number Diff line change
Expand Up @@ -1469,6 +1469,8 @@ added: v10.0.0
**Default**: `true`.
* `callback` {Function} A callback function that takes an optional error
argument.
* Returns: {Function} A cleanup function which removes all registered
listeners.

A function to get notified when a stream is no longer readable, writable
or has experienced an error or a premature close event.
Expand Down Expand Up @@ -1509,6 +1511,20 @@ run().catch(console.error);
rs.resume(); // Drain the stream.
```

`stream.finished()` leaves dangling event listeners (in particular
`'error'`, `'end'`, `'finish'` and `'close'`) after `callback` has been
invoked. The reason for this is so that unexpected `'error'` events (due to
incorrect stream implementations) do not cause unexpected crashes.
If this is unwanted behavior then the returned cleanup function needs to be
invoked in the callback:

```js
const cleanup = finished(...streams, (err) => {
cleanup();
// ...
});
```

### stream.pipeline(...streams, callback)
<!-- YAML
added: v10.0.0
Expand Down Expand Up @@ -1562,6 +1578,10 @@ async function run() {
run().catch(console.error);
```

`stream.pipeline()` leaves dangling event listeners on the streams
after the `callback` has been invoked. In the case of reuse of streams after
failure, this can cause event listener leaks and swallowed errors.

### stream.Readable.from(iterable, [options])
<!-- YAML
added: v12.3.0
Expand Down