js: skip the rest of a kernel compile from bytes once its signal aborts

This commit is contained in:
Pooya Parsa
2026-12-04 23:44:46 +00:00
parent f4a0b72cf0
commit a41458c687
2 changed files with 12 additions and 0 deletions

View File

@@ -213,18 +213,25 @@ export async function loadBytes(src: BytesSource, opts: LoadOptions = {}): Promi
*/
export async function loadKernelModule(src: KernelSource, opts: LoadOptions = {}): Promise<WebAssembly.Module> {
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<ArrayBuffer>);

View File

@@ -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);