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:
+15
-7
@@ -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)
|
||||
|
||||
@@ -618,3 +618,38 @@ async function openAs_(origin, key) {
|
||||
const ws = await openWs(origin, cookie)
|
||||
return { ws, cookie }
|
||||
}
|
||||
|
||||
// #33: the ack is the first frame a client can observe for its `reqId` (DESIGN 6.1 Ack, 4.2).
|
||||
// `player.setname` used to push `hello`, the resync and `roster` before its ack.
|
||||
test('player.setname: the ack precedes hello, the resync and roster', async () => {
|
||||
await withGmServer(async (s, gmKey) => {
|
||||
const gm = await gmConnect(s.origin, gmKey)
|
||||
const alice = await createPlayer(gm.ws)
|
||||
const { cookie } = await signIn(s.origin, alice.key)
|
||||
const ws = await openWs(s.origin, cookie)
|
||||
await ws.waitText('hello')
|
||||
const mark = ws.messages.length
|
||||
const ack = await ws.request({ t: 'player.setname', name: 'Alice' })
|
||||
assert.equal(ack.ok, true, JSON.stringify(ack))
|
||||
await ws.waitFor((m, i) => i >= mark && m.t === 'roster')
|
||||
const seq = ws.messages.slice(mark).map(m => m.t)
|
||||
assert.equal(seq[0], 'ack', `the ack must come first for this reqId: ${JSON.stringify(seq)}`)
|
||||
const at = t => seq.indexOf(t)
|
||||
assert.ok(at('ack') < at('hello'), 'ack before the second hello')
|
||||
assert.ok(at('hello') < at('roster'), 'the effects keep their own order: hello, then roster')
|
||||
assert.ok(at('ack') < at('vision'), 'ack before the resync frames')
|
||||
|
||||
// The failure path is unchanged: one error ack, no hello and no resync.
|
||||
const bob = await createPlayer(gm.ws)
|
||||
const { cookie: bobCookie } = await signIn(s.origin, bob.key)
|
||||
const b = await openWs(s.origin, bobCookie)
|
||||
await b.waitText('hello')
|
||||
const mark2 = b.messages.length
|
||||
const taken = await b.request({ t: 'player.setname', name: 'alice' })
|
||||
assert.equal(taken.code, 'name-taken')
|
||||
await sleep(50)
|
||||
const after = b.messages.slice(mark2).map(m => m.t)
|
||||
assert.deepEqual(after, ['ack'], `a refused rename has no effects: ${JSON.stringify(after)}`)
|
||||
ws.close(); b.close(); gm.ws.close()
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user