|
| 1 | +import assert from "node:assert/strict" |
| 2 | +import { mkdtemp, rm, writeFile } from "node:fs/promises" |
| 3 | +import { tmpdir } from "node:os" |
| 4 | +import { join, resolve } from "node:path" |
| 5 | +import { pathToFileURL } from "node:url" |
| 6 | +import { Worker } from "node:worker_threads" |
| 7 | + |
| 8 | +const root = await mkdtemp(join(tmpdir(), "wp-codebox-playground-worker-rejection-")) |
| 9 | +const preloadPath = join(root, "reject-on-command.mjs") |
| 10 | + |
| 11 | +await writeFile(preloadPath, ` |
| 12 | +import { parentPort } from "node:worker_threads" |
| 13 | +parentPort?.on("message", (message) => { |
| 14 | + if (message !== "wp-codebox-trigger-runtime-rejection") return |
| 15 | + const error = new WebAssembly.RuntimeError("null function or function signature mismatch") |
| 16 | + error.stack = "RuntimeError: null function or function signature mismatch\\n at php.wasm.zif_mysqli_poll (wasm://wasm/php.wasm-05996276:wasm-function[12986]:0x9949a8)" |
| 17 | + Promise.reject(error) |
| 18 | +}) |
| 19 | +`, "utf8") |
| 20 | + |
| 21 | +try { |
| 22 | + for (const version of ["v1", "v2"]) { |
| 23 | + await assertWorkerTerminates(version) |
| 24 | + } |
| 25 | +} finally { |
| 26 | + await rm(root, { recursive: true, force: true }) |
| 27 | +} |
| 28 | + |
| 29 | +async function assertWorkerTerminates(version: string): Promise<void> { |
| 30 | + const workerPath = resolve(`node_modules/@wp-playground/cli/worker-thread-${version}.js`) |
| 31 | + const worker = new Worker(pathToFileURL(workerPath), { |
| 32 | + execArgv: ["--import", pathToFileURL(preloadPath).href], |
| 33 | + }) |
| 34 | + |
| 35 | + let workerError: Error | undefined |
| 36 | + const exitCode = await Promise.race([ |
| 37 | + new Promise<number>((resolveExit) => { |
| 38 | + worker.once("message", (message: { command?: unknown; phpPort?: { close?: () => void } }) => { |
| 39 | + if (message?.command !== "worker-script-initialized") return |
| 40 | + message.phpPort?.close?.() |
| 41 | + worker.postMessage("wp-codebox-trigger-runtime-rejection") |
| 42 | + }) |
| 43 | + worker.once("error", (error) => { |
| 44 | + workerError = error |
| 45 | + }) |
| 46 | + worker.once("exit", resolveExit) |
| 47 | + }), |
| 48 | + new Promise<never>((_resolve, reject) => { |
| 49 | + setTimeout(() => reject(new Error(`Playground ${version} worker remained alive after an unhandled runtime rejection`)), 3_000).unref() |
| 50 | + }), |
| 51 | + ]).finally(() => worker.terminate()) |
| 52 | + |
| 53 | + assert.notEqual(exitCode, 0, `Playground ${version} worker must exit nonzero after an unhandled runtime rejection`) |
| 54 | + assert.ok(workerError, `Playground ${version} worker must propagate its unhandled runtime rejection`) |
| 55 | + assert.match(workerError.message, /null function or function signature mismatch/) |
| 56 | +} |
| 57 | + |
| 58 | +console.log("playground worker runtime rejection terminalization ok") |
0 commit comments