From 2878879c4e98452139240f86da7640bca00506fd Mon Sep 17 00:00:00 2001 From: pi0x Date: Wed, 2 Dec 2026 23:09:59 +0900 Subject: [PATCH] js: end the idle WASI program workers when the kernel exits (#160) Co-authored-by: Pooya Parsa --- .agents/js.md | 3 ++- js/core/wasi-exec.ts | 17 ++++++++--------- js/node/wasi.ts | 5 +++-- test/wasi-binfmt.ts | 3 +++ 4 files changed, 16 insertions(+), 12 deletions(-) diff --git a/.agents/js.md b/.agents/js.md index 4dfe74a..5e8077f 100644 --- a/.agents/js.md +++ b/.agents/js.md @@ -1125,7 +1125,8 @@ parts that are easy to get wrong. computation or not. `stats`: `running` programs, `threads` running besides them, live `workers` (Node: until its exit event), `idle` ones, `channels` whose owner loop still runs (shared path; 0 a task turn after - the program ended). `close()` stops listening and ends the idle workers. + the program ended). `close()` stops listening and ends the idle workers; + the kernel's exit (or `stop()`) does the same. - Snapshots: programs are not in them and do not hold the snapshot point up (no `hostBusy`, no `keep`), so the restored kernel resets kwwasi's connection and kwwasi exits 126 ("lost the connection to the host"); it diff --git a/js/core/wasi-exec.ts b/js/core/wasi-exec.ts index acb3f24..7be3ce2 100644 --- a/js/core/wasi-exec.ts +++ b/js/core/wasi-exec.ts @@ -132,7 +132,7 @@ export interface WasiExecServer { /** Requests go through a SharedArrayBuffer (WasiExecOptions.sharedMemory), not messages. */ readonly shared: boolean; readonly stats: WasiExecStats; - /** Stop listening and end the idle workers (running programs go on). */ + /** Stop listening and end the idle workers (running programs go on). The kernel's exit does it too. */ close(): void; } @@ -220,15 +220,14 @@ export function serveWasiProgramsWith(vsock: Vsock, opts: WasiExecOptions & Wasi const listener: VsockListener = vsock.listen(port, (conn) => { void accept(conn, ctx).catch((e) => log(`[wasi] ${errorText(e)}`)); }); - return { - port, - shared, - stats, - close: () => { - listener.close(); - pool.close(); - }, + const close = () => { + listener.close(); + pool.close(); }; + // With a handler, accept() resolves only with null: once the listener is closed or the kernel + // exited. Then end the idle workers, so that a stopped machine leaves no thread behind. + void listener.accept().then(close); + return { port, shared, stats, close }; } interface RunContext { diff --git a/js/node/wasi.ts b/js/node/wasi.ts index 0991b25..47b4740 100644 --- a/js/node/wasi.ts +++ b/js/node/wasi.ts @@ -17,14 +17,15 @@ import type { WasiFromProgram, WasiProgramEndpoint } from "../core/wasi-program. export function spawnNodeWasi(): WasiProgramEndpoint { const w = new Worker(new URL("./wasi-entry.ts", import.meta.url), { name: "unkernel-wasi" }); - // A program never keeps the process alive: it belongs to the machine, whose end ends it. - w.unref(); return { post: (msg, transfer) => w.postMessage(msg, transfer), listen(h) { w.on("message", (m: WasiFromProgram) => h.message(m)); w.on("error", h.error); w.on("exit", () => h.exit()); + // A program never keeps the process alive: it belongs to the machine, whose end ends it. + // After the "message" listener, which refs the worker again (Node 24, Bun). + w.unref(); }, terminate: () => void w.terminate(), // Read on the worker's thread (an interrupt, also while it computes or waits); Node 24.6 and later. diff --git a/test/wasi-binfmt.ts b/test/wasi-binfmt.ts index 46e1847..45ca4f3 100644 --- a/test/wasi-binfmt.ts +++ b/test/wasi-binfmt.ts @@ -686,6 +686,9 @@ async function snapshotCheck() { } finally { await vm2.stop(); } + // That program's worker was idle (kept for the next one); the machine's stop ends it, without close(). + await until(() => wasi.stats.workers === 0, 2000, "the resumed machine's idle worker to end at its stop"); + console.log(`ok stopping the resumed machine ended its idle worker (stats ${JSON.stringify(wasi.stats)})`); } console.log(failed ? `${failed} failure(s)` : "all passed"); process.exit(failed ? 1 : 0);