Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 6 additions & 1 deletion ipc-runtime/ts/src/wasm/backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,12 @@ export class WasmFfiBackend implements IpcClientAsync {
entry: opts.entry,
},
});
await ready;
try {
await ready;
} catch (e) {
await worker.terminate();
throw e;
}
return backend;
}

Expand Down
18 changes: 18 additions & 0 deletions ipc-runtime/ts/src/wasm_backend.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import assert from "node:assert/strict";
import { test } from "node:test";
import { createWasmFfiBackend } from "./wasm/node/index.js";
import type { WorkerHandle } from "./wasm/platform.js";

const I32 = 0x7f;
const leb = (n: number) => {
Expand Down Expand Up @@ -135,3 +136,20 @@ test("a worker-mode backend starts the threads its module spawns (node)", async
await backend.destroy();
}
});

test("a backend terminates a main worker that fails to start", async () => {
const handlers: ((message: unknown) => void)[] = [];
let terminated = false;
const worker: WorkerHandle = {
postMessage: () => handlers.forEach((h) => h({ type: "init-error", message: "no entry" })),
onMessage: (handler) => handlers.push(handler),
onError: () => {},
terminate: async () => {
terminated = true;
},
unref: () => {},
};
const module = new Uint8Array([0x00, 0x61, 0x73, 0x6d, 1, 0, 0, 0]);
await assert.rejects(createWasmFfiBackend({ module, createMainWorker: () => worker }), /no entry/);
assert.equal(terminated, true);
});
Loading