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.
## 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).
Fix — one scene record per request, read under the lock:
tileGate reads app.sceneonce 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.sceneinside 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.
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.
Summary
app.sceneis a mutable global thatscene.setreplaces wholesale underdbLock, 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 ascene.setlanding between the read and the last use can hand a reader aSceneRecwhosewallsseq 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—tileGatepassesapp.scenetoparseTileRequest, which copies the walls.server/main.nim:624—tilePath(app.scene, ...)re-readsapp.scenefor the redirect.server/main.nim:430-432—checkScenecompares againstapp.scene.idon every intent.server/main.nim:1797—app.scene = candidateinsidescene.set, under the lock.tileGate) run on their own thread pool, so they are genuinely concurrent with a WSscene.set.Failure scenario
A GM saves scene edits (new walls) while a player's browser fetches tiles.
tileGatereadsapp.scenebefore the swap for the parse and after the swap fortilePath; if the swap happens in between, the second read can observe the new record whileparsed, 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.sceneinto a local at the top oftileGate, pass that local to both the parse and the path build, and do the same incheckScene. 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
app.scenemore than once per request; each request's decisions all come from the same scene record.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.scene.setcalls) completes clean.Related
Fixed and pushed on
main.Fix — one scene record per request, read under the lock:
tileGatereadsapp.sceneonce insidewithLock app.dbLockand uses that local for the URL parse, the explored-grid check and theX-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).checkScenereads the scene id under the lock.scene.setnow computes itscandidatefromapp.sceneinside the lock (it used to patch an unlocked copy).sceneIdinside their lock for the post-lock log line and effect frame.Test evidence —
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.mjsalso interleaves 20scene.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. Noteart_versionis 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.tests/ttiles.nim): a request built from one record stays internally consistent after ascene.setswaps art version/width (old record still namesv1, the new record seesv1as stale) — the deterministic model of the interleaving, since the data race itself needs a sanitiser to observe.Commits —
f963e8fmain: read the scene once per request, under the lock (#31);b4bddf1design (4.3: the scene is a value swapped, never mutated, and each request reads it once; plus "borrow, do not copy");e13fa90results.Gates —
devbox run test: 107/107 node tests + all Nim suites;devbox run mutation: exit 0, every mutation passes.