pcbjam/tests/jspi/suspend-races.spec.ts
Viktor Vaczi 3f09a46ff5 jspi: migration phases 0-7 — build knob, scheduler shim, test successor suite
Toolchain: emsdk 6.0.6 (versions.sh; cache-hash keys on it). Build knob
PCBJAM_ASYNC_BACKEND=jspi|asyncify: build-kicad-target.sh links editors with
-sJSPI + -sJSPI_EXPORTS=@scripts/common/jspi-exports.txt + --pre-js
jspi-scheduler.js (no DYNCALLS, no post-link asyncify pipeline); wx build
stamps the backend and forces clean on flip or unknown provenance;
docker/build.sh passes the knob, seeds the emscripten ports cache from the
volume every launch, jspi postprocess = patch-env-shim only.

scripts/common/shims/jspi-scheduler.js: the JSPI successor scheduler —
token-wait registry, resume turnstile (one armed resume between engine
re-entries, SP swaps only at microtask boundaries), green-region spill
stacks (16-aligned tops), S1 embind mutator FIFO lane + parker wraps, S6
shutdown, libctx integration hooks (suspend/end/quarantine + g_current
arm/clear), SuspendError attributor, lost-wake + stuck-window watchdogs,
__wxWaitDump observability.

Embind: PARKER registrations get emscripten::async() under PCBJAM_JSPI
(wasm/bindings/pcbjam_async_policy.h). nanosleep yields route via the shim.

Tests: tests/asyncify -> tests/jspi successor suite (jspi-stack red/green
shadow-stack battery, jspi-coroutine MiniCoro harness, suspend-races
semantic scenarios + __wxWaitDump books coherence); projects jspi-firefox/
jspi-chrome (asyncify-webkit retired — no JSPI in WebKit); unconditional
Firefox JSPI pref; guard-beacons -> wait-beacons (+wxScheduler/libctxJspi
families); Makefile.wasm links test apps against JSPI with the shim as a
tracked link prerequisite.

Web: WasmTool setRo await + __wxWaitDump forensics, open-flow contained
promise, scheduler-shim.test.ts retargeted (8 green).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NDeBaKKhQztd8KiVtHuyXr
2026-08-13 07:06:24 +02:00

245 lines
9.2 KiB
TypeScript

import { test, expect, tryLoadApp } from '../e2e/utils/fixtures';
// Successor of tests/asyncify/asyncify-races.spec.ts (retired with the
// asyncify backend): the SEMANTIC suspension-race scenarios, run against the
// same races harness (tests/apps/standalone/asyncify-races/races_test.cpp)
// built for JSPI. The scenarios express through public wx + coroutine APIs —
// nested modal LIFO, out-of-order wake resolution, no-lost-wakes, nested-loop
// teardown-on-error — so they are exactly as meaningful under JSPI as under
// asyncify; only the failure MODES they'd catch differ (activation misnesting
// or a lost wait token instead of a clobbered rewind buffer). The harness's
// Fibers.* probes self-disable on non-asyncify glue ('n/a').
//
// Retired asyncify-mechanism gates, deliberately NOT ported: the
// Asyncify.currData single-writer tripwire (N1) and the deferred-wake books
// (readyWakes/drainedWakes) — those states are unrepresentable under JSPI.
// Their intent (coherent books at settle) lives on below via __wxWaitDump.
const BATTERY = [
'post_park_fiber_swap',
'sleep_inside_fiber_inside_modal',
'out_of_order_sleep_resolution',
'long_parked_sleep_clobbered_by_swap',
];
const CRASH_SIGNATURES = [
// backend-agnostic trouble
'index out of bounds',
'indirect call to null',
'invalid state',
'is not a function',
// the JSPI-specific loud failure: an un-promised export tried to suspend
'suspenderror',
'trying to suspend',
];
type WaitDump = {
pendingWaits: number;
runningActivations: number;
suspendedActivations: unknown[];
mutatorQueueDepth: number;
waitsBegun: number;
waitsResolved: number;
};
function findSummary(logs: string[]) {
return logs.find((log) => log.includes('[ASYNCIFY_RACES] SUMMARY'));
}
function parseSummary(summary: string) {
const match = summary.match(/total=(\d+)\s+passed=(\d+)\s+failed=(\d+)/);
expect(match, 'summary line should be parseable').not.toBeNull();
return { total: Number(match![1]), passed: Number(match![2]), failed: Number(match![3]) };
}
function crashLines(testLogger: { consoleLogs: string[]; errors: string[] }) {
const all = [...testLogger.consoleLogs, ...testLogger.errors];
return all.filter(
(line) =>
CRASH_SIGNATURES.some((sig) => line.toLowerCase().includes(sig)) &&
// The harness's own meta-output mentions these words legitimately.
!line.includes('[ASYNCIFY_RACES]')
);
}
function realErrors(testLogger: { errors: string[] }) {
return testLogger.errors.filter((e) => !e.includes('favicon'));
}
test.describe('Suspension races — green targets (jspi backend)', () => {
test('battery: all chained scenarios pass with a clean console', async ({
page,
testLogger,
}) => {
await page.goto('/standalone/asyncify-races/races_test.html');
const loaded = await tryLoadApp(page, 30000);
expect(loaded, 'races harness should load').toBe(true);
await expect
.poll(() => findSummary(testLogger.consoleLogs) ?? null, {
timeout: 60000,
message: 'battery should emit a final SUMMARY line (a missing one means a wedge/hang)',
})
.not.toBeNull();
const { total, passed, failed } = parseSummary(findSummary(testLogger.consoleLogs)!);
const failLogs = testLogger.consoleLogs.filter((l) => l.includes('[ASYNCIFY_RACES] FAIL '));
const passLogs = testLogger.consoleLogs.filter((l) => l.includes('[ASYNCIFY_RACES] PASS '));
expect(total).toBe(BATTERY.length);
expect(passed).toBe(BATTERY.length);
expect(failed).toBe(0);
expect(failLogs, `FAIL lines: ${failLogs.join(' || ')}`).toHaveLength(0);
expect(passLogs).toHaveLength(BATTERY.length);
for (const name of BATTERY) {
expect.soft(
testLogger.consoleLogs.some((l) => l.includes(`[ASYNCIFY_RACES] PASS ${name}`)),
`scenario ${name} should PASS`
).toBe(true);
}
expect(crashLines(testLogger), 'no crash signatures in console').toHaveLength(0);
expect(realErrors(testLogger), 'no page errors').toHaveLength(0);
});
test('battery leaves coherent books — no pending waits, no stuck activations', async ({
page,
testLogger,
}) => {
// Successor of the retired N4 gate: after the battery settles, the
// scheduler's books must be clean — every begun wait resolved or
// consumed, no activation left suspended, no queued mutators.
test.setTimeout(180000);
await page.goto('/standalone/asyncify-races/races_test.html');
const loaded = await tryLoadApp(page, 30000);
expect(loaded, 'races harness should load').toBe(true);
await expect
.poll(() => findSummary(testLogger.consoleLogs) ?? null, { timeout: 60000 })
.not.toBeNull();
const hasDump = await page.evaluate(
() => typeof (globalThis as unknown as { __wxWaitDump?: unknown }).__wxWaitDump === 'function',
);
test.skip(!hasDump, 'stale build — no __wxWaitDump');
// The last scenario's teardown can lag the SUMMARY line by a tick.
// main() legitimately parks on its per-frame yield forever ("frame") —
// the invariant is that no TOKEN wait (modal/nested/sleep/…) and no
// queued mutator survives the battery.
const stuck = (dump: WaitDump) =>
(dump.suspendedActivations as { waitKind: string | null }[]).filter(
(a) => a.waitKind !== 'frame',
).length + dump.mutatorQueueDepth;
await expect
.poll(
() =>
page.evaluate(() => {
const dump = (
globalThis as unknown as { __wxWaitDump: () => WaitDump }
).__wxWaitDump();
return (dump.suspendedActivations as { waitKind: string | null }[]).filter(
(a) => a.waitKind !== 'frame',
).length + dump.mutatorQueueDepth;
}),
{ timeout: 30000, intervals: [250] },
)
.toBe(0);
const books = await page.evaluate(() =>
(globalThis as unknown as { __wxWaitDump: () => WaitDump }).__wxWaitDump(),
);
console.log(`[TEST] scheduler books: ${JSON.stringify(books)}`);
expect(stuck(books), 'no token wait or mutator left stuck').toBe(0);
});
test('modal_in_modal_in_modal: three nested ShowModals resolve LIFO', async ({
page,
testLogger,
}) => {
// Historical red: the pre-scheduler wx dialog.cpp kept the modal resolver
// in a single slot (Module._endModal), so with three nested modals the
// middle EndModal resolved nothing and its ShowModal parked forever.
// Green since the LIFO resolver semantics — under JSPI the wait
// registry's per-kind LIFO stacks (jspi-scheduler.js, contract-identical
// to the asyncify shim's S4).
await page.goto('/standalone/asyncify-races/races_test.html#only=modal_in_modal_in_modal');
await tryLoadApp(page, 30000);
await expect
.poll(() => findSummary(testLogger.consoleLogs) ?? null, {
timeout: 45000,
message: 'triple modal should complete (middle EndModal must not be lost)',
})
.not.toBeNull();
const { passed, failed } = parseSummary(findSummary(testLogger.consoleLogs)!);
expect(passed).toBe(1);
expect(failed).toBe(0);
expect(crashLines(testLogger), 'no crash signatures in console').toHaveLength(0);
});
test('wakeup_during_transition: modal teardown over parked sleeps stays clean', async ({
page,
testLogger,
}) => {
await page.goto('/standalone/asyncify-races/races_test.html#only=wakeup_during_transition');
await tryLoadApp(page, 30000);
await expect
.poll(() => findSummary(testLogger.consoleLogs) ?? null, { timeout: 45000 })
.not.toBeNull();
const { passed, failed } = parseSummary(findSummary(testLogger.consoleLogs)!);
expect(passed).toBe(1);
expect(failed).toBe(0);
expect(crashLines(testLogger), 'no crash signatures in console').toHaveLength(0);
expect(realErrors(testLogger), 'no page errors').toHaveLength(0);
});
test('nested_quasi_modal_pump_error: pump rejection must not leak the parked DoRun', async ({
page,
testLogger,
}) => {
await page.goto(
'/standalone/asyncify-races/races_test.html#only=nested_quasi_modal_pump_error'
);
await tryLoadApp(page, 30000);
await expect
.poll(() => findSummary(testLogger.consoleLogs) ?? null, {
timeout: 45000,
message:
'nested loop must exit after a pump error (silent stall = the c27fe8bf bug, fixed in wx evtloop.cpp)',
})
.not.toBeNull();
const { passed, failed } = parseSummary(findSummary(testLogger.consoleLogs)!);
expect(passed).toBe(1);
expect(failed).toBe(0);
});
test('sleep-park mode: park throw must not escape as an unhandled rejection', async ({
page,
testLogger,
}) => {
await page.goto('/standalone/asyncify-races/races_test.html#mode=sleep-park');
await tryLoadApp(page, 30000);
await expect
.poll(() => findSummary(testLogger.consoleLogs) ?? null, { timeout: 45000 })
.not.toBeNull();
const { passed, failed } = parseSummary(findSummary(testLogger.consoleLogs)!);
expect(passed).toBe(1);
expect(failed).toBe(0);
const rejectionLeaks = testLogger.errors.filter(
(l) => !l.includes('[ASYNCIFY_RACES]') && !l.includes('favicon')
);
expect(
rejectionLeaks,
`park throw escaped: ${rejectionLeaks.join(' || ')}`
).toHaveLength(0);
});
});