Files
FFF_vtt_frozen/server/jsonbound.nim
T
SirStone 08363ef086 fix: stop sharing intent JSON refs with the world across worker threads
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.
2026-11-17 23:02:32 +01:00

32 lines
1.8 KiB
Nim

## Copies that cross the intent -> world boundary (ANALYSIS 4.1, DESIGN 4.3).
##
## Mummy runs the websocket handlers on several worker threads. The world is read and written under
## the one global lock, but the intents are parsed outside it. Nim's ORC reference counts are not
## atomic, so any `JsonNode` or `string` payload that is reachable from both an intent tree and the
## world can be ref-counted by two threads at once: once under the lock (an egress walk) and once
## outside it (the intent's `env` dying at the end of `dispatch`). That is a lost update, i.e. a
## premature free or a leak.
##
## Rule: an intent value is copied *at the boundary* into memory nothing else aliases. `copy()` is
## not enough for a `JsonNode`, because std/json rebuilds the nodes but a `JString`'s `str` field is
## still a string assignment and thus shares the intent's payload. Serialise + parse allocates a
## complete second tree, string payloads included. `detachedString` does the same for a bare string.
import std/json
import props
proc detachedString*(s: string): string =
## A string whose payload the caller does not share. Empty strings have no payload to share.
if s.len == 0: return ""
result = newString(s.len)
copyMem(addr result[0], unsafeAddr s[0], s.len)
proc detachedJson*(node: JsonNode): JsonNode =
## A deep copy of `node` that shares no node and no string payload with it. Serialise/parse is the
## portable way to get that guarantee from std/json; props and flags are small and bounded.
if node.isNil: nil else: parseJson($node)
proc detachedProps*(p: Props): Props =
## The three property buckets, each detached. Keys and values both come from the intent on
## `token.create`/`character.create`, so a shallow bucket copy would still alias them.
for b in PropBucket: result[b] = detachedJson(p[b])