main: read the scene once per request, under the lock (#31)

scene.set reassigns app.scene under dbLock while the HTTP handlers read it with
no lock and no stable snapshot: tileGate read it three times (twice outside the
lock) and checkScene read the id unlocked, so a swap could hand a request two
records, or race ORC freeing the old walls seq. tileGate now takes one snapshot
under the lock and uses it for the URL parse, the explored check and the
X-Accel-Redirect; checkScene and scene.set read under the lock; the wall/door
handlers capture sceneId inside their lock for the post-lock log and effect
frame.

Tests: ttiles pins that one record gives one whole answer after a swap;
tiles.test.mjs pins the handler shape (one code read, after the lock) and
interleaves 20 scene.set plus wall edits with 200 tile fetches -- only whole
tiles, no crash.
This commit is contained in:
2026-11-15 14:26:15 +01:00
parent e9d5f6815a
commit f963e8f4d6
3 changed files with 144 additions and 31 deletions
+19
View File
@@ -115,3 +115,22 @@ suite "tile access rule":
check tileAllowed(sc, cells(sc, [(128, 0)]), 128, 0, false)
# a cell beyond the scene is never consulted even if a stray index says so
check not tileAllowed(sc, cells(sc, [(129, 0)]), 128, 0, false)
suite "one scene record per request (#31)":
test "a request built from one record stays internally consistent when the scene is swapped":
# What main.tileGate does: read the scene once under the lock, then parse, gate and build the
# redirect from that one local. A `scene.set` landing afterwards must not change the answer.
var live = testScene()
let sc = live # the one read (`withLock` in main)
let parsed = parseOk(sc, "1", "v1", "9_9.png")
var explored = initHashSet[int]()
explored.incl sc.cellIndex(9, 9)
let allowed = tileCoveredByExplored(sc, explored, parsed.x, parsed.y)
# the writer swaps the scene between the read and the last use of the local
live.artVersion = 2
live.w = 8192.0
check allowed
check tilePath(sc, parsed.x, parsed.y) == "/_tiles/1/v1/9_9.png"
# the same URL is stale for the new record, never a mixture of the two
check parseTileRequest("1", "v1", "9_9.png", live).outcome == toTileWrongVersion
check tilePath(live, parsed.x, parsed.y) == "/_tiles/1/v2/9_9.png"