From d42f1543121e7245789a96a485d1ef7645cb5fba Mon Sep 17 00:00:00 2001 From: Luca Casonato Date: Wed, 1 Nov 2023 20:26:12 +0100 Subject: feat: disposable Deno resources (#20845) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit implements Symbol.dispose and Symbol.asyncDispose for the relevant resources. Closes #20839 --------- Signed-off-by: Bartek Iwańczuk Co-authored-by: Bartek Iwańczuk --- runtime/js/40_process.js | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) (limited to 'runtime/js/40_process.js') diff --git a/runtime/js/40_process.js b/runtime/js/40_process.js index 664a4b303..4ddc4a02a 100644 --- a/runtime/js/40_process.js +++ b/runtime/js/40_process.js @@ -18,7 +18,11 @@ const { } = primordials; import { FsFile } from "ext:deno_fs/30_fs.js"; import { readAll } from "ext:deno_io/12_io.js"; -import { assert, pathFromURL } from "ext:deno_web/00_infra.js"; +import { + assert, + pathFromURL, + SymbolAsyncDispose, +} from "ext:deno_web/00_infra.js"; import * as abortSignal from "ext:deno_web/03_abort_signal.js"; import { readableStreamCollectIntoUint8Array, @@ -201,7 +205,6 @@ class ChildProcess { #rid; #waitPromiseId; #waitComplete = false; - #unrefed = false; #pid; get pid() { @@ -216,7 +219,6 @@ class ChildProcess { return this.#stdin; } - #stdoutRid; #stdout = null; get stdout() { if (this.#stdout == null) { @@ -225,7 +227,6 @@ class ChildProcess { return this.#stdout; } - #stderrRid; #stderr = null; get stderr() { if (this.#stderr == null) { @@ -254,12 +255,10 @@ class ChildProcess { } if (stdoutRid !== null) { - this.#stdoutRid = stdoutRid; this.#stdout = readableStreamForRidUnrefable(stdoutRid); } if (stderrRid !== null) { - this.#stderrRid = stderrRid; this.#stderr = readableStreamForRidUnrefable(stderrRid); } @@ -324,15 +323,22 @@ class ChildProcess { ops.op_spawn_kill(this.#rid, signo); } + async [SymbolAsyncDispose]() { + try { + ops.op_spawn_kill(this.#rid, "SIGTERM"); + } catch { + // ignore errors from killing the process (such as ESRCH or BadResource) + } + await this.#status; + } + ref() { - this.#unrefed = false; core.refOp(this.#waitPromiseId); if (this.#stdout) readableStreamForRidUnrefableRef(this.#stdout); if (this.#stderr) readableStreamForRidUnrefableRef(this.#stderr); } unref() { - this.#unrefed = true; core.unrefOp(this.#waitPromiseId); if (this.#stdout) readableStreamForRidUnrefableUnref(this.#stdout); if (this.#stderr) readableStreamForRidUnrefableUnref(this.#stderr); -- cgit v1.2.3