app.scene is read outside dbLock while scene.set reassigns it #31

Closed
opened 2026-11-15 13:18:29 +01:00 by SirStone · 1 comment
Owner

Summary

app.scene is a mutable global that scene.set replaces wholesale under dbLock, while the tile and scene-check readers read it with no lock and no stable snapshot. Readers take it by value at several points, so a scene.set landing between the read and the last use can hand a reader a SceneRec whose walls seq was already freed by the writer (ORC frees the old seq when the new scene is assigned). The observed window is in the tile path.

Where

  • server/main.nim:598-599 — tileGate passes app.scene to parseTileRequest, which copies the walls.
  • server/main.nim:624 — tilePath(app.scene, ...) re-reads app.scene for the redirect.
  • server/main.nim:430-432 — checkScene compares against app.scene.id on every intent.
  • server/main.nim:1797 — app.scene = candidate inside scene.set, under the lock.
  • HTTP handlers (tileGate) run on their own thread pool, so they are genuinely concurrent with a WS scene.set.

Failure scenario

A GM saves scene edits (new walls) while a player's browser fetches tiles. tileGate reads app.scene before the swap for the parse and after the swap for tilePath; if the swap happens in between, the second read can observe the new record while parsed, derived from the old one, still points into the old geometry, or a copy taken across the free can touch released memory. In practice this is a rare use-after-free / mismatched-answer crash on the tile path, and a wrong tile for one request in the milder interleaving. Suspected rather than reproduced: the window is a few instructions wide.

Confidence

Medium — the unsynchronised read of a reassigned global is certain; the crash itself is not reproduced.

Fix direction

Make the scene read once per request and never re-read it: load app.scene into a local at the top of tileGate, pass that local to both the parse and the path build, and do the same in checkScene. Longer term, treat the scene as an immutable value behind an atomic/guarded pointer that is swapped, not mutated, so a reader that took it once keeps a consistent record for the whole request.

Acceptance

  • No handler reads app.scene more than once per request; each request's decisions all come from the same scene record.
  • A test that interleaves repeated scene.set (changing walls and art version) with concurrent tile requests sees no crash and only whole-scene answers: either the old or the new tile, never a mixed response.
  • Address/thread sanitiser or a targeted stress test (thousands of tile fetches across dozens of scene.set calls) completes clean.

Related

  • #24 (slice 9: map tiles), which owns the tile gate.
  • #25 (slice 10: finish line).
  • #2 (M3 planning map).
## Summary `app.scene` is a mutable global that `scene.set` replaces wholesale under `dbLock`, while the tile and scene-check readers read it with no lock and no stable snapshot. Readers take it by value at several points, so a `scene.set` landing between the read and the last use can hand a reader a `SceneRec` whose `walls` seq was already freed by the writer (ORC frees the old seq when the new scene is assigned). The observed window is in the tile path. ## Where - `server/main.nim:598-599` — `tileGate` passes `app.scene` to `parseTileRequest`, which copies the walls. - `server/main.nim:624` — `tilePath(app.scene, ...)` re-reads `app.scene` for the redirect. - `server/main.nim:430-432` — `checkScene` compares against `app.scene.id` on every intent. - `server/main.nim:1797` — `app.scene = candidate` inside `scene.set`, under the lock. - HTTP handlers (`tileGate`) run on their own thread pool, so they are genuinely concurrent with a WS `scene.set`. ## Failure scenario A GM saves scene edits (new walls) while a player's browser fetches tiles. `tileGate` reads `app.scene` before the swap for the parse and after the swap for `tilePath`; if the swap happens in between, the second read can observe the new record while `parsed`, derived from the old one, still points into the old geometry, or a copy taken across the free can touch released memory. In practice this is a rare use-after-free / mismatched-answer crash on the tile path, and a wrong tile for one request in the milder interleaving. Suspected rather than reproduced: the window is a few instructions wide. ## Confidence Medium — the unsynchronised read of a reassigned global is certain; the crash itself is not reproduced. ## Fix direction Make the scene read once per request and never re-read it: load `app.scene` into a local at the top of `tileGate`, pass that local to both the parse and the path build, and do the same in `checkScene`. Longer term, treat the scene as an immutable value behind an atomic/guarded pointer that is swapped, not mutated, so a reader that took it once keeps a consistent record for the whole request. ## Acceptance - No handler reads `app.scene` more than once per request; each request's decisions all come from the same scene record. - A test that interleaves repeated `scene.set` (changing walls and art version) with concurrent tile requests sees no crash and only whole-scene answers: either the old or the new tile, never a mixed response. - Address/thread sanitiser or a targeted stress test (thousands of tile fetches across dozens of `scene.set` calls) completes clean. ## Related - #24 (slice 9: map tiles), which owns the tile gate. - #25 (slice 10: finish line). - #2 (M3 planning map).
SirStone added the ready-for-agent label 2026-11-15 13:18:30 +01:00
Author
Owner

Fixed and pushed on main.

Fix — one scene record per request, read under the lock:

  • tileGate reads app.scene once inside withLock app.dbLock and uses that local for the URL parse, the explored-grid check and the X-Accel-Redirect (the old code read it three times: once for the parse, once for the explored check, once for the path, two of those outside the lock).
  • checkScene reads the scene id under the lock.
  • scene.set now computes its candidate from app.scene inside the lock (it used to patch an unlocked copy).
  • the wall/door handlers capture sceneId inside their lock for the post-lock log line and effect frame.

Test evidence —

  • red (HEAD source, same scan): tileGate reads= 3 lockBeforeRead= false, checkScene reads= 1 lockBeforeRead= false; green: both read once, after the lock (tools/tiles.test.mjs, "every handler reads app.scene at most once, under the lock (#31)").
  • tools/tiles.test.mjs also interleaves 20 scene.set + 20 wall add/remove pairs with 200 tile fetches over the real nginx stack: every request is 200 or 403, every 200 is byte-identical to the fixture tile (never a mixed answer), the server still answers /healthz. Note art_version is not a settable scene field in v1 (6.1), so the stress cannot flip the redirect target; the visible mix is explored-vs-record, which the single read removes.
  • Nim level (tests/ttiles.nim): a request built from one record stays internally consistent after a scene.set swaps art version/width (old record still names v1, the new record sees v1 as stale) — the deterministic model of the interleaving, since the data race itself needs a sanitiser to observe.

Commits — f963e8f main: read the scene once per request, under the lock (#31); b4bddf1 design (4.3: the scene is a value swapped, never mutated, and each request reads it once; plus "borrow, do not copy"); e13fa90 results.

Gates — devbox run test: 107/107 node tests + all Nim suites; devbox run mutation: exit 0, every mutation passes.

Fixed and pushed on `main`. **Fix** — one scene record per request, read under the lock: - `tileGate` reads `app.scene` **once** inside `withLock app.dbLock` and uses that local for the URL parse, the explored-grid check and the `X-Accel-Redirect` (the old code read it three times: once for the parse, once for the explored check, once for the path, two of those outside the lock). - `checkScene` reads the scene id under the lock. - `scene.set` now computes its `candidate` from `app.scene` **inside** the lock (it used to patch an unlocked copy). - the wall/door handlers capture `sceneId` inside their lock for the post-lock log line and effect frame. **Test evidence** — - red (HEAD source, same scan): `tileGate reads= 3 lockBeforeRead= false`, `checkScene reads= 1 lockBeforeRead= false`; green: both read once, after the lock (`tools/tiles.test.mjs`, "every handler reads app.scene at most once, under the lock (#31)"). - `tools/tiles.test.mjs` also interleaves 20 `scene.set` + 20 wall add/remove pairs with 200 tile fetches over the real nginx stack: every request is 200 or 403, every 200 is byte-identical to the fixture tile (never a mixed answer), the server still answers `/healthz`. Note `art_version` is not a settable scene field in v1 (6.1), so the stress cannot flip the redirect target; the visible mix is explored-vs-record, which the single read removes. - Nim level (`tests/ttiles.nim`): a request built from one record stays internally consistent after a `scene.set` swaps art version/width (old record still names `v1`, the new record sees `v1` as stale) — the deterministic model of the interleaving, since the data race itself needs a sanitiser to observe. **Commits** — `f963e8f` main: read the scene once per request, under the lock (#31); `b4bddf1` design (4.3: the scene is a value swapped, never mutated, and each request reads it once; plus "borrow, do not copy"); `e13fa90` results. **Gates** — `devbox run test`: 107/107 node tests + all Nim suites; `devbox run mutation`: exit 0, every mutation passes.
Sign in to join this conversation.