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:
@@ -358,6 +358,80 @@ test('tile gate: refusals, direct /_tiles, malformed and out-of-range paths', as
|
||||
})
|
||||
})
|
||||
|
||||
// #31: `app.scene` is swapped (not mutated) by `scene.set` under the global lock. A handler that
|
||||
// reads it twice can see two different records in one request; a read outside the lock races the
|
||||
// assignment itself. This pins the shape of the two HTTP handlers that touch it (the WS handlers all
|
||||
// run under the lock), because a data race is otherwise only visible to a sanitiser.
|
||||
test('every handler reads app.scene at most once, under the lock (#31)', async () => {
|
||||
const source = fs.readFileSync(path.join(projectRoot, 'server', 'main.nim'), 'utf8')
|
||||
// The body of a proc: from its `proc name` line to the next top-level `proc`.
|
||||
const bodyOf = name => {
|
||||
const start = source.indexOf(`proc ${name}(`)
|
||||
assert.ok(start > 0, `proc ${name} not found`)
|
||||
const rest = source.slice(start + 1)
|
||||
const next = rest.indexOf('\nproc ')
|
||||
return rest.slice(0, next < 0 ? undefined : next)
|
||||
}
|
||||
for (const handler of ['tileGate', 'checkScene']) {
|
||||
// Code only: the comments explain the rule and would count as reads.
|
||||
const body = bodyOf(handler).split('\n').filter(l => !l.trim().startsWith('#')).join('\n')
|
||||
const reads = body.split('app.scene').length - 1
|
||||
assert.equal(reads, 1, `${handler} must read app.scene once (found ${reads})`)
|
||||
const readAt = body.indexOf('app.scene')
|
||||
const lockAt = body.indexOf('withLock app.dbLock')
|
||||
assert.ok(lockAt >= 0 && lockAt < readAt,
|
||||
`${handler} must take the lock before it reads app.scene`)
|
||||
}
|
||||
})
|
||||
|
||||
test('concurrent scene edits and tile requests answer whole tiles, never a mixture (#31)', async () => {
|
||||
await withTileStack(async ({ s, gmKey, ngx, stateDir }) => {
|
||||
const ctx = await sceneWithVision(s, gmKey, ngx)
|
||||
const scene = ctx.gm.ws.messages.find(m => m.t === 'hello' && m.scene).scene
|
||||
const url = tileUrl(scene.id, ctx.artVersion, 9, 9)
|
||||
const want = writeTiles(stateDir, { scene: scene.id, artVersion: ctx.artVersion,
|
||||
tiles: [[9, 9]] })
|
||||
const expected = fs.readFileSync(want[0])
|
||||
// The explored tile Alice may fetch; the gate may refuse it while a wall edit hides the cell,
|
||||
// so both 200 and 403 are whole answers, and a 200 must be exactly the fixture bytes.
|
||||
let allowed = 0
|
||||
let refused = 0
|
||||
const fetchTile = async () => {
|
||||
const r = await getTile(ngx.origin, url, ctx.seenCtx.cookie)
|
||||
assert.ok([200, 403].includes(r.status), `unexpected status ${r.status}`)
|
||||
if (r.status === 200) {
|
||||
allowed++
|
||||
assert.ok(Buffer.from(await r.arrayBuffer()).equals(expected),
|
||||
'a 200 must be one whole tile, not a mix of two scene records')
|
||||
} else {
|
||||
refused++
|
||||
}
|
||||
}
|
||||
// Editors: `scene.set` (a full resync) and wall edits that change the explored grid, while the
|
||||
// fetches run. Every edit is acked before the next one: they are serialised by the global lock.
|
||||
const edits = (async () => {
|
||||
for (let i = 0; i < 20; i++) {
|
||||
assert.equal((await ctx.gm.ws.request({ t: 'scene.set', scene: scene.id,
|
||||
name: `Cave ${i}` })).ok, true)
|
||||
const w = await ctx.gm.ws.request({ t: 'wall.add', scene: scene.id,
|
||||
a: [380, 200], b: [380, 420], kind: 'wall' })
|
||||
assert.equal(w.ok, true, JSON.stringify(w))
|
||||
assert.equal((await ctx.gm.ws.request({ t: 'wall.remove', scene: scene.id,
|
||||
id: w.id })).ok, true)
|
||||
}
|
||||
})()
|
||||
const fetches = (async () => {
|
||||
for (let i = 0; i < 200; i++) await fetchTile()
|
||||
})()
|
||||
await Promise.all([edits, fetches])
|
||||
assert.ok(allowed + refused === 200, `every request answered (${allowed} + ${refused})`)
|
||||
assert.ok(allowed > 0, 'the request shape was fetchable at least once')
|
||||
// The server is alive and still answers its own health after the interleaving.
|
||||
assert.equal((await fetch(`${s.origin}/healthz`)).status, 200)
|
||||
ctx.gm.ws.close(); ctx.seenCtx.ws.close()
|
||||
})
|
||||
})
|
||||
|
||||
test('fixture tiles are generated by the helper and none is committed', async () => {
|
||||
// The repo owns no PNG: tiles are generated per test into a state dir (DESIGN 13 M3).
|
||||
const tracked = spawnSync('git', ['ls-files', '*.png'], { cwd: projectRoot, encoding: 'utf8' })
|
||||
|
||||
Reference in New Issue
Block a user