Files
FFF_vtt_frozen/server/faults.nim
T
SirStone 2727617db9 feat: session cache, explored bitmap and lock-free tile gate (#65)
The tile gate no longer takes dbLock or runs a SQL session JOIN per tile: a published
in-memory view (server/sessions.nim) holds token hashes -> player/role and one explored
bitmap per player. Sessions load from the DB at startup and are updated by join, key
replacement and player removal in the same lock as the write; refreshed cells publish
before the ack. The gate is one snapshot read (parse + session + bit test + redirect)
over scalar data only, so a tile burst is answered while a mutation holds dbLock.

Evidence: tests/tsessions.nim pins the cache/bitmap lifecycle and gate rule; tiles.test
adds the lock-held burst and the revocation/restart seam; auth.test adds the
LEAK_FAULT=sessioncache negative control (a stale entry makes the auth group fail);
mutation c5 keeps the cache drop load-bearing. Explored wire and persistence untouched.

Bench (devbox run bench, in-process): tile gate old 0.006 ms -> new 0.001 ms per
request; incremental explored mark 0.001 ms; snapshot build 2.0 ms at 262144 cells
(startup only).
2026-11-21 00:10:39 +01:00

61 lines
2.4 KiB
Nim

## Test-only fault injection for the negative controls of the leak harness (DESIGN 10.1, 10.3).
## The hook is compiled in only with `-d:leakfault`, and every fault checks
## `LEAK_FAULT=<name>` from the environment: a normal build cannot fault, it contains no `LEAK_FAULT`
## string, and tools/fault.test.mjs asserts that the shipped binary's bytes lack it.
when defined(leakfault) or defined(dispfault):
import std/os
proc faultActive*(name: string): bool =
## True only in a `-d:leakfault` build whose LEAK_FAULT equals `name`. Without the define this is
## the constant false, so the shipped server has no fault path at all.
when defined(leakfault):
getEnv("LEAK_FAULT") == name
else:
false
proc crashNow*() =
## `-d:leakfault` only: die at a chosen point with no shutdown, modelling a hard kill between two
## transactions (the `markerCrash` hook, TESTING 10.3, #60). A shipped build has no death path.
when defined(leakfault):
quit(70)
when defined(leakfault):
var lockHoldFired = false
proc faultHoldLock*() =
## `-d:leakfault` only: hold the global lock for a moment at the start of `token.move`, so a test
## can prove a tile burst is answered while a mutation is still in flight (#65). Fires once per
## process; the caller already holds `dbLock`, which serializes the flag.
when defined(leakfault):
if not lockHoldFired and getEnv("LEAK_FAULT") == "lockhold":
lockHoldFired = true
sleep(1200)
when defined(dispfault):
var txnFaultFired = false
var ackFaultFired = false
proc injectTxn*() =
## #32 only: raise from inside a mutation that holds the database transaction, once per process.
## The build define and the environment variable are both required, so a shipped binary has no
## raise site at all.
when defined(dispfault):
if txnFaultFired: return
case getEnv("DISP_FAULT")
of "txn-catchable":
txnFaultFired = true
raise newException(ValueError, "injected fault")
of "txn-defect":
txnFaultFired = true
raise newException(Defect, "injected fault")
else: discard
proc injectAfterAck*() =
## #32 only: fail after the handler has already enqueued its ack, once per process, to prove the
## guard never doubles an existing ack. A shipped binary has no raise site.
when defined(dispfault):
if ackFaultFired: return
if getEnv("DISP_FAULT") == "after-ack":
ackFaultFired = true
raise newException(Defect, "injected fault")