|
| 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-non-wasm-rejection") { |
| 15 | + Promise.reject(new Error("ordinary worker rejection")) |
| 16 | + setTimeout(() => parentPort?.postMessage("wp-codebox-worker-survived"), 25) |
| 17 | + } |
| 18 | + if (message === "wp-codebox-trigger-php-wasm-rejection") { |
| 19 | + const error = new WebAssembly.RuntimeError("null function or function signature mismatch") |
| 20 | + 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)" |
| 21 | + Promise.reject(error) |
| 22 | + } |
| 23 | +}) |
| 24 | +`, "utf8") |
| 25 | + |
| 26 | +try { |
| 27 | + for (const version of ["v1", "v2"]) { |
| 28 | + await assertWorkerTerminates(version) |
| 29 | + } |
| 30 | +} finally { |
| 31 | + await rm(root, { recursive: true, force: true }) |
| 32 | +} |
| 33 | + |
| 34 | +async function assertWorkerTerminates(version: string): Promise<void> { |
| 35 | + const workerPath = resolve(`node_modules/@wp-playground/cli/worker-thread-${version}.js`) |
| 36 | + const worker = new Worker(pathToFileURL(workerPath), { |
| 37 | + execArgv: ["--import", pathToFileURL(preloadPath).href], |
| 38 | + }) |
| 39 | + |
| 40 | + let workerError: Error | undefined |
| 41 | + let initialized = false |
| 42 | + let survivedOrdinaryRejection = false |
| 43 | + const exitCode = await Promise.race([ |
| 44 | + new Promise<number>((resolveExit) => { |
| 45 | + worker.once("message", (message: { command?: unknown; phpPort?: { close?: () => void } }) => { |
| 46 | + if (message?.command !== "worker-script-initialized") return |
| 47 | + initialized = true |
| 48 | + message.phpPort?.close?.() |
| 49 | + worker.postMessage("wp-codebox-trigger-non-wasm-rejection") |
| 50 | + }) |
| 51 | + worker.on("message", (message: unknown) => { |
| 52 | + if (message !== "wp-codebox-worker-survived") return |
| 53 | + survivedOrdinaryRejection = true |
| 54 | + worker.postMessage("wp-codebox-trigger-php-wasm-rejection") |
| 55 | + }) |
| 56 | + worker.once("error", (error) => { |
| 57 | + workerError = error |
| 58 | + }) |
| 59 | + worker.once("exit", resolveExit) |
| 60 | + }), |
| 61 | + new Promise<never>((_resolve, reject) => { |
| 62 | + setTimeout(() => reject(new Error(`Playground ${version} worker remained alive after a fatal PHP-WASM rejection`)), 3_000).unref() |
| 63 | + }), |
| 64 | + ]).finally(() => worker.terminate()) |
| 65 | + |
| 66 | + assert.ok(initialized, `Playground ${version} worker must initialize before the rejection checks`) |
| 67 | + assert.ok(survivedOrdinaryRejection, `Playground ${version} worker must preserve ordinary unhandled rejection behavior`) |
| 68 | + assert.notEqual(exitCode, 0, `Playground ${version} worker must exit nonzero after a fatal PHP-WASM rejection`) |
| 69 | + assert.ok(workerError, `Playground ${version} worker must propagate its fatal PHP-WASM rejection`) |
| 70 | + assert.match(workerError.message, /null function or function signature mismatch/) |
| 71 | +} |
| 72 | + |
| 73 | +console.log("playground PHP-WASM worker rejection terminalization ok") |
0 commit comments