diff --git a/js/core/load.ts b/js/core/load.ts index 74b3701..3661fe6 100644 --- a/js/core/load.ts +++ b/js/core/load.ts @@ -213,18 +213,25 @@ export async function loadBytes(src: BytesSource, opts: LoadOptions = {}): Promi */ export async function loadKernelModule(src: KernelSource, opts: LoadOptions = {}): Promise { if (src instanceof WebAssembly.Module) return src; + // Aborted (by the caller, or because the initrd beside it failed): skip the steps left. + const checkAborted = () => { + if (opts.signal?.aborted) throw new BootError("download", "aborted", { cause: opts.signal.reason }); + }; const compile = async (bytes: Uint8Array) => { + checkAborted(); try { bytes = await gunzipBytes(bytes); } catch (e) { throw new BootError("download", `The kernel is gzip that does not decompress: ${(e as Error).message}`, { cause: e }); } + checkAborted(); if (opts.smp) { try { bytes = (await smpKernelFromBundle(bytes)) ?? bytes; } catch (e) { throw new BootError("compile", `Deriving the SMP kernel from the bundle failed: ${(e as Error).message}`, { cause: e }); } + checkAborted(); } try { return await WebAssembly.compile(bytes as Uint8Array); diff --git a/test/api.test.ts b/test/api.test.ts index 5148e6f..f305d6a 100644 --- a/test/api.test.ts +++ b/test/api.test.ts @@ -430,6 +430,11 @@ console.log(stdout.trim()); await Promise.all([kernel.cancelled, initrd.cancelled]); }); + test("loadKernelModule() with an aborted signal does not compile kernel bytes", async () => { + await assert.rejects(sdk.loadKernelModule(new Uint8Array(kernelBytes), { signal: AbortSignal.abort(), smp: true }), + (e: unknown) => e instanceof sdk.BootError && e.stage === "download" && e.message === "aborted"); + }); + test("loadKernelModule() and loadBytes() read file: URLs and paths from disk; one Module boots several kernels, inline and in a worker, the SMP one on 2 CPUs", async (t) => { const url = testKernel ? pathToFileURL(testKernel) : bundledKernelUrl; const up = await sdk.loadKernelModule(url);