Mummy runs websocket handlers on worker threads. Intents are parsed outside the one global lock, while the world is walked under it; ORC refcounts are not atomic, so any JsonNode or string payload reachable from both an intent tree and the world can be freed or leaked by two threads at once. - Add server/jsonbound.nim: detachedJson/detachedString/detachedProps copies that share neither nodes nor string payloads (serialise+parse; std/json's copy() would still alias JString payloads). - Detach every intent value that enters world state: wall.add/wall.update flags; token.create flags/props/names/kind/character_id; token.update patch fields; prop.set key and value (token and character); character.create props/names; scene.set name/extra. Use the world's own id in name.reveal. - Build wall effects and create acks inside the mutation's critical section (settleLocked/settleWorldLocked). Wall/token/character record copies and frames no longer outlive the lock; only fresh scalars cross it. - Move player.remove's in-memory follow-up under the lock (it mutated tokens, characters, visions, reveals and encountered outside it). - Cap mummy at 4 worker threads (default was 10x cores) for a seven-person table. - Add tests/tdetach.nim: identity and string-payload checks for the boundary helpers, plus the parseFlags-returns-the-caller's-node premise. A unit test can prove the copy semantics, not the absence of a race.
69 lines
2.8 KiB
Nim
69 lines
2.8 KiB
Nim
## The intent -> world boundary copies (ANALYSIS 4.1): a regression guard for the *shape* of the
|
|
## fix, not for the race itself. A unit test can prove that the helper returns memory that aliases
|
|
## nothing the caller passed in (identity and string-payload checks); it cannot prove that every
|
|
## handler calls it, and it cannot reproduce a non-atomic refcount race. The handler audit and the
|
|
## "frames are built under the lock" rule are verified by reading, not by this file.
|
|
import std/[json, options, unittest]
|
|
import jsonbound, props, world
|
|
|
|
proc ptrOf(n: JsonNode): pointer = cast[pointer](n)
|
|
|
|
proc payload(s: string): pointer =
|
|
## The address of a string's first character: two strings that share a payload answer the same.
|
|
if s.len == 0: nil else: cast[pointer](unsafeAddr s[0])
|
|
|
|
suite "detachedJson":
|
|
test "rebuilds nodes and string payloads":
|
|
let src = parseJson("""{"vision":true,"name":"scroll","nested":{"k":[1,2,3]}}""")
|
|
let dst = detachedJson(src)
|
|
check ptrOf(dst) != ptrOf(src)
|
|
check $dst == $src
|
|
check payload(dst["name"].getStr) != payload(src["name"].getStr)
|
|
check ptrOf(dst["nested"]) != ptrOf(src["nested"])
|
|
check ptrOf(dst["nested"]["k"]) != ptrOf(src["nested"]["k"])
|
|
|
|
test "mutating the source leaves the copy alone":
|
|
let src = parseJson("""{"vision":true}""")
|
|
let dst = detachedJson(src)
|
|
src["vision"] = %false
|
|
check dst["vision"].getBool
|
|
check not src["vision"].getBool
|
|
|
|
test "absent (nil) stays nil and null stays null":
|
|
check detachedJson(nil).isNil
|
|
check detachedJson(newJNull()).kind == JNull
|
|
|
|
suite "detachedString":
|
|
test "does not share the payload":
|
|
let src = "vision"
|
|
let dst = detachedString(src)
|
|
check dst == src
|
|
check payload(dst) != payload(src)
|
|
|
|
test "empty strings stay empty":
|
|
check detachedString("") == ""
|
|
check payload(detachedString("")) == nil
|
|
|
|
suite "detachedProps":
|
|
test "three independent buckets, keys and values included":
|
|
let src = parseJson("""{"public":{"a":1},"owner":{"b":{"c":2}},"gm":{}}""")
|
|
var parsed: Props
|
|
for name, bucket in src:
|
|
parsed[parseBucket(name).get] = bucket
|
|
let dst = detachedProps(parsed)
|
|
for b in PropBucket:
|
|
check ptrOf(dst[b]) != ptrOf(parsed[b])
|
|
check $dst[b] == $parsed[b]
|
|
var srcKey, dstKey: string
|
|
for k in parsed[pbOwner].keys: srcKey = k
|
|
for k in dst[pbOwner].keys: dstKey = k
|
|
check payload(dstKey) != payload(srcKey)
|
|
|
|
suite "the premise this guards (world.parseFlags)":
|
|
test "parseFlags returns the caller's node by reference, not a copy":
|
|
# If this ever becomes a copy, the boundary still works; the test records why it exists.
|
|
let env = parseJson("""{"flags":{"vision":true}}""")
|
|
let flags = parseFlags(env{"flags"})
|
|
check flags.isSome
|
|
check ptrOf(flags.get) == ptrOf(env{"flags"})
|