fix(pointcloud,geometry): settle worker RPC requests when the worker dies or a post throws

Merged
LO
22d ago
+578-84

Defect

Worker RPC clients whose pending requests never settle when the worker dies or a post fails (implicit-state-machines audit, P3 + P5).

  • P3 @ifc-lite/pointcloud — WorkerSession listened only to message. No error/messageerror handler and no dead latch, while getSharedSession cached the session for the whole page lifetime (evicted only on spawn failure).
  • P5 @ifc-lite/geometry — RemeshClient.start(): a messageerror during start called kill(), which rejects only pending. That map is empty during init, so create() never settled. Init also had no deadline. In request(), a throwing post() (e.g. DataCloneError) left the pending entry and its 30 s timer alive. That timer later fired kill() on the whole client and rejected unrelated requests.

Scenario

  • A decode worker OOMs mid-LAZ decode → the open/next promises hang forever. Every later point-cloud load in the tab then reuses the dead worker and also hangs, until the page is reloaded.
  • The re-mesh worker sends an unreadable ready → the Model workspace's RemeshClient.create() never settles.
  • A remesh request fails to clone → 30 s later, a healthy client is killed under whatever request is in flight then.

Fix

Both clients get the same semantics as the existing reference patterns (RemeshClient.kill, GeometryCompressionSession.fail): the first error wins, the worker is terminated, everything pending is rejected, and later requests reject at once. A throwing post fails only its own request and leaves nothing behind.

  • WorkerSession (moved to streaming/worker-session.ts so worker-client.ts stays under the 400-line rule) adds error/messageerror listeners, a fail() dead latch, alive, onDeath, and post-throw cleanup. notify is a no-op once the session is dead.
  • getSharedSession evicts a dead session (onDeath), so the next load spawns a fresh worker. A never-opened source that retries open() after a crash also moves to a fresh worker. A source that was already opened stays bound to its dead session, because its worker-side state died with it, so its requests reject.
  • RemeshClient.start() routes every init failure through one fail that rejects init and kills: init-error, error, messageerror, a throwing post, and no ready within the new optional startTimeoutMs (default 120 s, covering the wasm fetch and compile). It is separate from requestTimeoutMs, so a fail-fast request deadline does not cut off a slow engine start (review). request() deletes the entry and cancels the deadline when the post throws.
  • Both deadlines are armed by one helper, remesh/deadline.ts armDeadline. Positive finite values are clamped to 2³¹−1 ms, because setTimeout reads a larger value, or Infinity, as about 1 ms. 0, a negative number, NaN or Infinity mean no deadline. Before this, 0 fired at once.
  • docs/guide/geometry.md and the changeset document all of the above. @ifc-lite/geometry is bumped minor because of the new option; @ifc-lite/pointcloud is a patch.

Not extracted into a shared helper: @ifc-lite/pointcloud has no @ifc-lite/* dependency at all, and @ifc-lite/geometry does not depend on pointcloud. A shared helper would need a new dependency edge, or a new package, for about 20 lines. Both clients are fixed in place with identical semantics, and the shared createWorkerRpc is planned for a later wave.

Behaviour note: an uncaught exception in the decode worker (error event) now kills the shared worker and rejects every source's pending requests. That matches RemeshClient. Before this change, the request that hit the exception hung, and so did everything behind it.

Tests (TDD: written first, run red against base, then green)

New packages/pointcloud/src/streaming/worker-client-death.test.ts (fake worker that can crash, emit messageerror, or throw on post):

  • P3: a worker crash mid-open rejects the pending open instead of hanging — red on base (timeout) → green
  • P3: an unreadable reply (messageerror) rejects every pending request — red → green
  • P3: a crash mid-decode rejects the pending next(), and later calls reject at once — red → green
  • P3: the shared session is evicted on death so the next load spawns a fresh worker — red → green
  • P3: retrying open() on the same never-opened source after a crash uses a fresh worker — red → green
  • P3: a throwing postMessage rejects that request and leaves the session usable — green on base too. The executor throw already rejected the promise, and the only defect was the leaked map entry, which is not observable. The test stays as a pin that the session survives and is not evicted.

packages/geometry/src/remesh/remesh-client.test.ts:

  • P5: an unreadable message during start rejects create() and terminates the worker — red (timeout) → green
  • P5: a worker that never answers init rejects create() at the deadline — red → green
  • P5: a request whose post throws rejects alone and leaves no timer to kill later requests — red (client killed) → green
  • P5: a throwing init post rejects create() and terminates the worker — green on base too (executor throw, then create disposes). Kept as a pin.
  • P5: a short request deadline does not cut off a slow engine start — new (review). requestTimeoutMs: 1_000 with ready arriving at 5 s, so create resolves. It then advances another 120 s and asserts the client is alive, the worker is not terminated, and an in-flight request still resolves. Mutation check: deleting the start-deadline disarm on ready fails this test (1 of 46 failed).
  • P5: a late reply for a request whose post threw is ignored and does not disturb the client — new (review). Replies for the refused id and for a stray id are dropped; the client stays alive and the next request resolves.
  • P5: a Infinity timeout means no deadline, not an immediate one and P5: a 0 timeout means no deadline, not an immediate one — new.
  • New packages/geometry/src/remesh/deadline.test.ts: pass-through, clamp to 2³¹−1, 0/-1/NaN/±Infinity read as no deadline, fires once and not after cancel, an overflowing delay does not fire early, and no timer is armed for no deadline. Mutation check: removing the clamp and no-deadline branch fails 10 of 46 tests.

Existing pointcloud test fakes (worker-client.test.ts, copc-worker-client.test.ts, decode-worker.test.ts) delivered every addEventListener type to the message listener. They now honour the event type, as a real Worker does.

Local results:

  • packages/pointcloud vitest: 32 files passed and 1 skipped; 420 tests passed and 5 skipped.
  • packages/geometry src/remesh vitest (head c467b72e37, run under systemd-run --user --scope -p MemoryMax=8G -p MemorySwapMax=0): 5 files and 46 tests passed.
  • turbo run typecheck --filter=@ifc-lite/pointcloud: OK.
  • oxlint on the touched dirs: clean.
  • check-module-size: OK.

Perf verdict (packages/geometry is perf-sensitive)

No hot-path change, so no benchmark was run. The diff touches only RemeshClient error and lifecycle handling: one extra setTimeout/clearTimeout per client start (once per Model workspace session), and a try around the per-request postMessage. That try costs nothing on the non-throwing path. Mesh output, the wasm boundary and the meshing code are untouched, so mesh bytes are identical by construction.

Labels

  • unqueued: self-driven audit work.
  • No ci:native-adapter: that lane exercises the Rust native adapter and retained or geometry kernel output. This PR changes only the TypeScript re-mesh worker client's error paths, which the native adapter never loads.

Fleet validation

  • Commit c467b72e37, job 6c8487ba: pnpm install && pnpm exec turbo run build typecheck test --filter=@ifc-lite/geometry --filter=@ifc-lite/pointcloud passed 12/12 tasks. geometry: 85 files and 666 tests passed. This covers the startTimeoutMs and deadline.ts changes, including typechecking their test files.
  • Commit e64867b2ee (head) changes only tests (the double-fail latch pin, and an observable no-deadline timer test). Locally under the 8 GiB cap, the remesh suite passed 50/50. Mutation checks: removing the kill() dead latch fails the new double-fail test; dropping Number.isFinite fails 4 tests. Typechecking those test files at this head is left to CI.
  • A stale request, auto-07370cbb, was submitted without pnpm install and returned exit 254. It is not evidence, and it was not replayed.

The first commit's run follows.

Commit 8ae73ee5bd, job 903b05fc

  • pnpm exec turbo run build typecheck test --filter=@ifc-lite/pointcloud --filter=@ifc-lite/geometry: 12/12 tasks successful. pointcloud: 33 test files passed. geometry: 84 files and 652 tests passed.
  • check-api-surface in the same job exited early, because it reads every package's built .d.ts and the job built only the two filtered packages. That was a gap in my command, not a code failure. The published exports are unchanged: WorkerSession is not exported from the package index, and RemeshClient's public members are unchanged. CI's api-surface gate is authoritative.
  • node scripts/pre-handoff.mjs (run locally): every gate PASS, except retained-free viewer, which was SKIPPED because apps/viewer/dist is absent and this PR does not touch the viewer.

Unverified

  • No browser repro of a real worker OOM. The death paths are proven with scripted workers only.
  • The full viewer suite was not run. The viewer imports only types from @ifc-lite/geometry/remesh, and no viewer test fakes the pointcloud worker.

🤖 Generated with Claude Code