pcbjam/tests/jspi/suspend-races.spec.ts
Viktor Vaczi 9c475a804e jspi cleanup: remove the asyncify-era residue — dead code, conditionals, pipeline scaffolding, stale prose
The runtime is JSPI-only; this removes everything that still pretended
otherwise. Three exhaustive sweeps (C++/JS+build+CI/tests+docs) drove
the inventory; every deletion verified by grep closure + full gates.

Broken-right-now fixes:
- deploy-staging.yml passed the retired opt_level input — the workflow
  could not even start. Removed.
- env.sh carried dead exports with a live -sASYNCIFY=1 inside
  (WASM_LDFLAGS/PTHREAD_LDFLAGS, zero consumers). Removed; the
  WASM_LEGACY_EXCEPTIONS rationale rewritten to the real reason.
- docker/build.sh exported PCBJAM_ASYNC_BACKEND (read nowhere). Gone.

Dead weight removed:
- binaryen submodule (nothing builds or invokes it), wasm-opt-bench
  workflow + scripts/bench/, get-wasm-opt.sh, diagnostics.js (242 lines
  of Asyncify-API-only code), the KICAD_PIPELINE background-postprocess
  scaffolding (existed to parallelize the deleted wasm-opt phase; the
  postprocess is a seconds-long node script and now runs inline),
  build-monitor's dead asyncify rows, sched-context orphan build
  output, dead .gitignore entries, the .jspi-assets spike dir (the two
  wf-result research JSONs moved to docs/features/async/migration-evidence/).
- bindings: fiber_park.h + its 12 embind registrations (broken-if-
  called under JSPI), the kicadOpenFileStart/OPEN_JOB starter route,
  main_stack_runner.h + 5 includes, the always-null context-sleep weak
  hook in nanosleep_yield.c.
- shim: the backend field (installed-flag idempotency instead),
  noteContextWait (dead both sides), the __wxAsyncifyDump alias (+ the
  WasmTool fallback and string-dump normalize branch).
- web: the emscripten-6-ignored mainScriptUrlOrBlob option in boot.ts
  (gerber-demo keeps it: it loads the deployed CDN release, which
  predates emscripten 6 — noted inline).

Conditionals: all 'backend === jspi' checks reduced to scheduler-
presence checks; races_quiescent re-keyed from Asyncify.state (vacuous)
to real backlog quiescence (resumeReady/mutatorQueue — NOT _windowLive,
which is the probing activation's own window by definition).

Renames (identifiers only, no file renames): ASYNC_LINK_FLAGS→
JSPI_LINK_FLAGS and Makefile ASYNC_LDFLAGS→JSPI_LDFLAGS,
kicadCollabFiberBusy→kicadCollabBusy (embind + web + tests),
collab_common.h fiber*→apply*/coroutine naming, asyncifySignatures→
wasmTrapSignatures (lists byte-identical).

Tests: the two remaining vacuous [wx-asyncify]/fiber-resume-refused
asserts re-keyed to live JSPI beacons; eeschema-load's failure message
no longer sends the developer to a deleted script; wait-beacons' dead
families/parser deleted; lane-0 legacy-glue guards removed (lane 0 is
unconstructible); the embind test.fail re-gated with the JSPI reason
(plain embind invokers cannot suspend — verified still failing);
lint-determinism now scans tests/jspi (166 files clean);
eeschema-collab local-move gated to chromium (~50% flaky on FF even
solo; pcbnew twin covers both engines).

Docs: DEBUG.md rewritten as the JSPI debugging guide; build.md
describes the single-phase build; docs/features/async/README.md
banner-marked historical and repointed at the NEW
23-jspi-runtime.md (current architecture: export census, turnstile,
libcontext ownership + refusal contract, embind call shapes, the
em-pthread service-wrapper trick, exception policy, known gaps).

Gates on the cleaned tree: test:e2e 725 passed / 0 failed (after the
quiescence-probe fix; the 3 other reds were verified contention flakes
solo-green or the documented FF gate), web 76/0, jspi 18/18 both
engines, vitest 295/295 + 17/17, all lints green, live-app census
clean.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016X9eh1s5sTx1o9Em9KBuwR
2026-08-14 09:25:32 +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
// asyncify-era Fibers.* probes retired with that backend.
//
// 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);
});
});