fix: player.setname acks before its effects (#33)

The durable write and registry update stay put; the ack is enqueued
between the commit and the first effect, then hello, the full resync and
roster follow in their existing order. The failure paths are unchanged.
Frame-order test pins ack first on the renaming connection.
This commit is contained in:
2026-11-20 21:36:47 +01:00
parent a5c0ee5d4c
commit 41c2fe695b
2 changed files with 50 additions and 7 deletions
+15 -7
View File
@@ -398,6 +398,18 @@ proc resyncEveryone() =
sendHello(c.id, p.get)
resync(c.id, c.named, p.get)
proc settleSetNameLocked(conn: Conn, reqId: int, named: Player, ackFirst = true) =
## `player.setname`'s effects (6.1, 6.3): every connection of that player gets `hello` with
## `me.name`, then the full resync; `roster` to all. The ack is enqueued first, after the durable
## write and before any effect (#33); the effects keep their own order. Caller holds the lock and
## has already committed the name.
if ackFirst: ackOk(conn, reqId)
for c in app.conns.connectionsOf(named.id):
sendHello(c.id, named)
resync(c.id, true, named)
sendRoster()
if not ackFirst: ackOk(conn, reqId)
proc settleLocked(conn: Conn, reqId: int, extra: JsonNode = nil, roster = false) =
## 4.2 step 7 for a mutation: the ack and then every recipient's diff are enqueued under the global
## lock, so no two threads touch a connection's `known` table at once and the ack always precedes
@@ -933,13 +945,9 @@ proc dispatch(conn: Conn, env: Envelope) {.gcsafe.} =
let named = playerById(app.db, conn.playerId)
outcome = snOk
app.conns.markNamed(conn.playerId)
# 6.1: every connection of that player gets `hello` with `me.name` set, then the full resync
# (6.3); `roster` to all (Q9). The ack follows, so nothing is acked before it is durable.
for c in app.conns.connectionsOf(conn.playerId):
sendHello(c.id, named.get)
resync(c.id, true, named.get)
sendRoster()
ackOk(conn, env.reqId)
# 6.1: the name is durable here; the ack is enqueued first, then every connection of that
# player gets `hello` with `me.name` set and the full resync (6.3), then `roster` to all.
settleSetNameLocked(conn, env.reqId, named.get)
else: outcome = snNotAllowed
if outcome == snTaken:
ackErr(conn, env.reqId, CodeNameTaken)