mirror of
https://github.com/pithings/unkernel.git
synced 2026-12-05 17:59:28 +08:00
js: end the idle WASI program workers when the kernel exits (#160)
Co-authored-by: Pooya Parsa <pooya@pi0.io>
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user