Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
worker: make Worker async disposable
PR-URL: #58385
Backport-PR-URL: #58455
Reviewed-By: LiviaMedeiros <livia@cirno.name>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Daeyeon Jeong <daeyeon.dev@gmail.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
  • Loading branch information
jasnell authored and aduh95 committed Jul 28, 2025
commit 8dbc6b210f8ed666cc93f7709141d4ca87367429
8 changes: 8 additions & 0 deletions doc/api/worker_threads.md
Original file line number Diff line number Diff line change
Expand Up @@ -1846,6 +1846,14 @@ Calling `unref()` on a worker allows the thread to exit if this is the only
active handle in the event system. If the worker is already `unref()`ed calling
`unref()` again has no effect.

### `worker[Symbol.asyncDispose]()`

<!-- YAML
added: REPLACEME
-->

Alias for [`worker.terminate()`][].

## Notes

### Synchronous blocking of stdio
Expand Down
6 changes: 5 additions & 1 deletion lib/internal/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ const {
const { createMainThreadPort, destroyMainThreadPort } = require('internal/worker/messaging');
const { deserializeError } = require('internal/error_serdes');
const { fileURLToPath, isURL, pathToFileURL } = require('internal/url');
const { kEmptyObject } = require('internal/util');
const { kEmptyObject, SymbolAsyncDispose } = require('internal/util');
const { validateArray, validateString } = require('internal/validators');
const {
throwIfBuildingSnapshot,
Expand Down Expand Up @@ -406,6 +406,10 @@ class Worker extends EventEmitter {
});
}

async [SymbolAsyncDispose]() {
await this.terminate();
}

ref() {
if (this[kHandle] === null) return;

Expand Down