mirror of
https://github.com/pithings/unkernel.git
synced 2026-12-05 16:19:36 +08:00
js: skip the rest of a kernel compile from bytes once its signal aborts
This commit is contained in:
@@ -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>);
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user