Skip to content

Commit 452e747

Browse files
efekrskladuh95
authored andcommitted
doc: discourage AbortSignal cleanup for long-lived resources
Signed-off-by: Efe Karasakal <hi@efe.dev> PR-URL: #64342 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
1 parent acc1578 commit 452e747

4 files changed

Lines changed: 108 additions & 0 deletions

File tree

‎doc/api/child_process.md‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,11 @@ can be used to specify the character encoding used to decode the stdout and
242242
stderr output. If `encoding` is `'buffer'`, or an unrecognized character
243243
encoding, `Buffer` objects will be passed to the callback instead.
244244

245+
> Using the `signal` option to destroy a long-lived child process as a resource
246+
> cleanup mechanism is deprecated. The `signal` option remains appropriate for
247+
> cancellation, externally propagated aborts, and timeouts. See
248+
> [DEP0209](deprecations.md#dep0209-using-abortsignal-to-dispose-of-resources).
249+
245250
```cjs
246251
const { exec } = require('node:child_process');
247252
exec('cat *.js missing_file | wc -l', (error, stdout, stderr) => {
@@ -390,6 +395,11 @@ except that it does not spawn a shell by default. Rather, the specified
390395
executable `file` is spawned directly as a new process making it slightly more
391396
efficient than [`child_process.exec()`][].
392397

398+
> Using the `signal` option to destroy a long-lived child process as a resource
399+
> cleanup mechanism is deprecated. The `signal` option remains appropriate for
400+
> cancellation, externally propagated aborts, and timeouts. See
401+
> [DEP0209](deprecations.md#dep0209-using-abortsignal-to-dispose-of-resources).
402+
393403
The same options as [`child_process.exec()`][] are supported. Since a shell is
394404
not spawned, behaviors such as I/O redirection and file globbing are not
395405
supported.
@@ -585,6 +595,11 @@ current process.
585595
The `shell` option available in [`child_process.spawn()`][] is not supported by
586596
`child_process.fork()` and will be ignored if set.
587597

598+
> Using the `signal` option to destroy a long-lived child process as a resource
599+
> cleanup mechanism is deprecated. The `signal` option remains appropriate for
600+
> cancellation, externally propagated aborts, and timeouts. See
601+
> [DEP0209](deprecations.md#dep0209-using-abortsignal-to-dispose-of-resources).
602+
588603
If the `signal` option is enabled, calling `.abort()` on the corresponding
589604
`AbortController` is similar to calling `.kill()` on the child process except
590605
the error passed to the callback will be an `AbortError`:
@@ -735,6 +750,11 @@ process, the default is [`process.env`][].
735750
736751
`undefined` values in `env` will be ignored.
737752
753+
> Using the `signal` option to destroy a long-lived child process as a resource
754+
> cleanup mechanism is deprecated. The `signal` option remains appropriate for
755+
> cancellation, externally propagated aborts, and timeouts. See
756+
> [DEP0209](deprecations.md#dep0209-using-abortsignal-to-dispose-of-resources).
757+
738758
Example of running `ls -lh /usr`, capturing `stdout`, `stderr`, and the
739759
exit code:
740760

‎doc/api/deprecations.md‎

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4522,6 +4522,85 @@ replacing it keeps being called by [`server.listen()`][], and it will be
45224522
removed in a future version of Node.js. Use [`server.listen()`][] instead of
45234523
calling or overriding `_listen2`.
45244524
4525+
### DEP0209: Using `AbortSignal` to dispose of resources
4526+
4527+
<!-- YAML
4528+
changes:
4529+
- version: REPLACEME
4530+
pr-url: https://github.com/nodejs/node/pull/64342
4531+
description: Documentation-only deprecation.
4532+
-->
4533+
4534+
Type: Documentation-only
4535+
4536+
Using `AbortSignal` to destroy long-lived resources is deprecated. Prefer
4537+
`using` for resource cleanup.
4538+
4539+
`AbortSignal` is still a good fit for canceling actions, propagating
4540+
cancellation from the outside, and timeouts.
4541+
4542+
```js
4543+
// Deprecated
4544+
async function example() {
4545+
const ac = new AbortController();
4546+
const server = http.createServer(handler);
4547+
server.listen({ port: 3000, signal: ac.signal });
4548+
4549+
await doWork();
4550+
ac.abort();
4551+
}
4552+
```
4553+
4554+
```js
4555+
// Use this instead
4556+
async function example() {
4557+
await using server = http.createServer(handler);
4558+
server.listen(3000);
4559+
4560+
await doWork();
4561+
}
4562+
```
4563+
4564+
```js
4565+
// Deprecated
4566+
async function example() {
4567+
const ac = new AbortController();
4568+
const stream = addAbortSignal(ac.signal, fs.createReadStream(file));
4569+
4570+
await consume(stream);
4571+
ac.abort();
4572+
}
4573+
```
4574+
4575+
```js
4576+
// Use this instead
4577+
async function example() {
4578+
await using stream = fs.createReadStream(file);
4579+
4580+
await consume(stream);
4581+
}
4582+
```
4583+
4584+
```js
4585+
// Deprecated
4586+
async function example() {
4587+
const ac = new AbortController();
4588+
const child = spawn(command, args, { signal: ac.signal });
4589+
4590+
await doWork();
4591+
ac.abort();
4592+
}
4593+
```
4594+
4595+
```js
4596+
// Use this instead
4597+
async function example() {
4598+
using child = spawn(command, args);
4599+
4600+
await doWork();
4601+
}
4602+
```
4603+
45254604
[DEP0142]: #dep0142-repl_builtinlibs
45264605
[NIST SP 800-38D]: https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38d.pdf
45274606
[RFC 6066]: https://tools.ietf.org/html/rfc6066#section-3

‎doc/api/net.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -739,6 +739,11 @@ Otherwise, if `path` is specified, it behaves the same as
739739
[`server.listen(path[, backlog][, callback])`][`server.listen(path)`].
740740
If none of them is specified, an error will be thrown.
741741

742+
> Using the `signal` option to destroy a long-lived server as a resource cleanup
743+
> mechanism is deprecated. The `signal` option remains appropriate for
744+
> cancellation, externally propagated aborts, and timeouts. See
745+
> [DEP0209](deprecations.md#dep0209-using-abortsignal-to-dispose-of-resources).
746+
742747
If `exclusive` is `false` (default), then cluster workers will use the same
743748
underlying handle, allowing connection handling duties to be shared. When
744749
`exclusive` is `true`, the handle is not shared, and attempted port sharing

‎doc/api/stream.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3531,6 +3531,10 @@ changes:
35313531
`WritableStream`.
35323532
-->
35333533

3534+
> Stability: 0 - Deprecated. Using [`stream.addAbortSignal()`][] to destroy
3535+
> long-lived stream resources is documentation-only deprecated. See
3536+
> [DEP0209](deprecations.md#dep0209-using-abortsignal-to-dispose-of-resources).
3537+
35343538
* `signal` {AbortSignal} A signal representing possible cancellation
35353539
* `stream` {Stream|ReadableStream|WritableStream} A stream to attach a signal
35363540
to.

0 commit comments

Comments
 (0)