fix: dispatch guard and Defect-safe rollback (#32)

A readable intent that fails in handling now always gets exactly one ack:
the new static internal code, unless the handler's own ack was already
enqueued. Both transaction helpers roll back on every unwind path,
Defect included, so the shared SQLite connection never keeps an open
transaction. Test-only dispfault build covers catchable/defect/after-ack;
DESIGN 6.5 records internal as an owner decision and 6.2 names refusal
codes as additive.
This commit is contained in:
2026-11-20 21:36:44 +01:00
parent 38941da003
commit a5c0ee5d4c
9 changed files with 246 additions and 12 deletions
+21 -1
View File
@@ -92,6 +92,11 @@ var pingCounter: Atomic[int64]
## Payload of the last server Ping; the client echoes it in its Pong (4.3). Atomic because the
## heartbeat thread and the upgrade handler (a worker thread) both send pings.
var ackedReqId {.threadvar.}: int
## #32: the reqId whose ack this handler invocation already enqueued, or -1. A threadvar is enough
## because it is written and read entirely inside one `handleText` call on the thread that serves
## the frame, even when that call unwinds through the dispatch guard.
proc nextPingSeq(): int64 =
pingCounter.fetchAdd(1) + 1
@@ -105,10 +110,12 @@ proc nextN(conn: Conn): int =
app.conns.nextFrameNo(conn.id)
proc ackOk(conn: Conn, reqId: int, extra: JsonNode = nil) =
ackedReqId = reqId
let n = nextN(conn)
if n > 0: conn.ws.send($ackFrame(reqId, n, extra))
proc ackErr(conn: Conn, reqId: int, code: string) =
ackedReqId = reqId
let n = nextN(conn)
if n > 0: conn.ws.send($nackFrame(reqId, n, code))
@@ -403,6 +410,7 @@ proc settleLocked(conn: Conn, reqId: int, extra: JsonNode = nil, roster = false)
## Caller holds `app.dbLock` (ANALYSIS 4.1: every frame that references world state is built here).
recordEncounters()
ackOk(conn, reqId, extra)
faults.injectAfterAck()
if roster: sendRoster()
egressAll()
@@ -1387,6 +1395,7 @@ proc dispatch(conn: Conn, env: Envelope) {.gcsafe.} =
app.tokens[idx.get] = rec
refreshPlayer(app.tokens[idx.get].owner)
app.db.withTransaction:
faults.injectTxn()
discard writePosition(app.db, app.tokens[idx.get])
writeStatuses(app.db, app.tokens[idx.get])
persistDerived(@[app.tokens[idx.get].owner])
@@ -2244,7 +2253,18 @@ proc handleText(conn: Conn, text: string) {.gcsafe.} =
if not env.ok:
malformedFrame(conn, nowMs)
return
dispatch(conn, env)
# #32: one guard covers the whole handling of a readable envelope, so an escaping handler failure
# is still exactly one ack. A failure after the handler's own ack is not answered twice; anything
# else gets the static `internal` refusal. The transaction helpers roll back on every unwind path,
# including `Defect`, before this runs.
ackedReqId = -1
try:
dispatch(conn, env)
except Exception as e:
if ackedReqId != env.reqId:
ackErr(conn, env.reqId, CodeInternal)
app.lg.log(lvError, "intent_failed", [("intent", env.t), ("reqId", $env.reqId),
("class", $e.name), ("message", "unhandled")])
proc websocketEvent(ws: WebSocket, event: WebSocketEvent, message: Message) {.gcsafe.} =
case event