doc: discourage AbortSignal cleanup for long-lived resources by efekrskl · Pull Request #64342 · nodejs/node · GitHub
Skip to content
Merged
Show file tree
Hide file tree
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/child_process.md
79 changes: 79 additions & 0 deletions doc/api/deprecations.md
Original file line number Diff line number Diff line change
Expand Up @@ -4718,6 +4718,85 @@ replacing it keeps being called by [`server.listen()`][], and it will be
removed in a future version of Node.js. Use [`server.listen()`][] instead of
calling or overriding `_listen2`.

### DEP0209: Using `AbortSignal` to dispose of resources

<!-- YAML
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/64342
description: Documentation-only deprecation.
-->

Type: Documentation-only

Using `AbortSignal` to destroy long-lived resources is deprecated. Prefer
`using` for resource cleanup.

`AbortSignal` is still a good fit for canceling actions, propagating
cancellation from the outside, and timeouts.

```js
// Deprecated
async function example() {
const ac = new AbortController();
const server = http.createServer(handler);
server.listen({ port: 3000, signal: ac.signal });

await doWork();
ac.abort();
}
```

```js
// Use this instead
async function example() {
await using server = http.createServer(handler);
server.listen(3000);

await doWork();
}
```

```js
// Deprecated
async function example() {
const ac = new AbortController();
const stream = addAbortSignal(ac.signal, fs.createReadStream(file));

await consume(stream);
ac.abort();
}
```

```js
// Use this instead
async function example() {
await using stream = fs.createReadStream(file);

await consume(stream);
}
```

```js
// Deprecated
async function example() {
const ac = new AbortController();
const child = spawn(command, args, { signal: ac.signal });

await doWork();
ac.abort();
}
```

```js
// Use this instead
async function example() {
using child = spawn(command, args);

await doWork();
}
```

[DEP0142]: #dep0142-repl_builtinlibs
[DEP0156]: #dep0156-aborted-property-and-abort-aborted-event-in-http
[NIST SP 800-38D]: https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38d.pdf
Expand Down
5 changes: 5 additions & 0 deletions doc/api/net.md
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,11 @@ Otherwise, if `path` is specified, it behaves the same as
[`server.listen(path[, backlog][, callback])`][`server.listen(path)`].
If none of them is specified, an error will be thrown.

> Using the `signal` option to destroy a long-lived server as a resource cleanup
> mechanism is deprecated. The `signal` option remains appropriate for
> cancellation, externally propagated aborts, and timeouts. See
> [DEP0209](deprecations.md#dep0209-using-abortsignal-to-dispose-of-resources).

If `exclusive` is `false` (default), then cluster workers will use the same
underlying handle, allowing connection handling duties to be shared. When
`exclusive` is `true`, the handle is not shared, and attempted port sharing
Expand Down
4 changes: 4 additions & 0 deletions doc/api/stream.md
Loading