Fix review: H-L4-01 (leaving a group conversation ends the leaver's seat in its Talk)
From mnema-talk v3.3.13, rendered on this site.
Branch msch128/fix-h-l4-01, commit 9a9b378bc on top of 59814670d (the lane 4 test matrix, not reviewed here). Read-only review in a detached temp worktree, removed afterwards. Nothing pushed or committed.
Verdict: MERGE
Findings: no BLOCKER, no SHOULD-FIX. Five NOTEs (N1 to N5 below).
1. Does it remove the cause
The finding and the refuter put the cause in one place: ServeLeaveDM called only Perms.Invalidate(). That dropped the cached snapshot, so new decisions already refused the leaver, but the hub was never told to re-check live seats. That re-check runs only from perm.Notifier.PermissionsChanged (internal/ws/perms.go:156-170). The fix (internal/chat/dm/dm_handler.go:313-315) calls Perms.Changed([]uuid.UUID{user.ID}).
What Invalidate did is still done. Changed is s.Invalidate() followed by the notifier
(internal/perm/service.go:210-221), and it is nil-safe in the same way, so nothing is lost. The extra work is
the notifier call: permissions_changed goes to the leaver's sessions (debounced), the snapshot is reloaded,
then sweepStages and enforceVoiceAccess run for the leaver alone.
Order:
- LeaveDM commits its transaction before it returns (dm_members.go:80-95), so the reload inside Changed sees the membership row gone. A loader that started before the commit cannot keep a stale snapshot: the generation check in reload (service.go:163-170) drops it.
- Changed runs before the DMMemberRemoved event. The leaver therefore gets voice_kicked (missing_permission) before dm_member_removed, and the remaining members get the voice_state_update leave before the member list change. Both orders are sensible. I measured the seat gone about 1 µs after the 204, so the enforcement runs synchronously before the response.
- A voice_join the leaver sends between the commit and Changed may be allowed on the old snapshot. The enforcement after it removes the seat again, so the race closes.
Every variant listed by the finding and the refuter, checked on the fix:
- Seat, room events (talk_reaction), key messages (media_handshake), told (the others see the leave): the fix's matrix covers them, and all stop.
- SFU peer: kickUserFromChannel goes through leaveVoiceLocked (internal/ws/voice_kick.go:31-47), which is the path that removes the peer. Read from code only, as in phase 2.
- MLS removal by the remaining clients: their roster is the server's voice list, and that list now drops the leaver (told cell). Removing the leaver from the media group then becomes valid by the roster rule the refuter cites. Read from code, not run.
- P3/P4 (the non-member keeps an otherwise empty Talk alive): the kick goes through the normal presence removal, so an empty temporary Talk follows its usual lifecycle.
- Grace and resume: see R1 below.
Situations of my own, not in the fix's tests (temp probe file, not kept). All pass on 9a9b378bc:
- R1, leaving during the grace period. Carol's socket is closed first, so she is still listed during the 30 s grace. Then she leaves over REST. The grace seat is gone at once. A resume reconnect then gets voice_kicked missing_permission and gives no seat.
- R2, two Talks in one conversation. Alice and carol sit in Talk 1, then bob starts Talk 2. GET /api/dms/{id} then shows call.talk_id = Talk 2, because DMCalls tracks one call per conversation and supersedes the old one. Carol leaves and her seat in Talk 1 ends at once: enforceVoiceAccess checks every seat of the leaver, not only "the" Talk.
- R3, the last member leaves while seated. Alice leaves the call, alice and bob leave the conversation, and carol, the last member, sits alone in the Talk and leaves. She gets exactly one voice_kicked (closeVoiceRooms, reason empty). There is no second kick from Changed, because the seat is already gone when enforceVoiceAccess scans. The answer is 204, nothing panics, and the conversation is then 404. So there is no double end. This case also passes with the fix reverted (closeVoiceRooms already covers it), as the finding says.
- R4, leave, re-add, rejoin, leave again, two rounds. Each leave ends the seat, each re-add (200) lets carol rejoin, and alice's seat survives every round.
Side effect worth stating: Changed also closes a small existing gap in the last-member case. dmAndTalkIDs runs
before the LeaveDM transaction, so a Talk created between the two would not be in talks and its room would not
be closed. The enforcement for the leaver now removes the leaver's seat in such a Talk too. I did not test this
race.
2. Tests run as normal tests and fail when the fix is reverted
- TestLeavingAGroupConversationEndsItsCall and TestConversationRevocationMatrix (integration tag, both normal tests, neither a known failure) pass on the fix. The matrix logged seat 0 s, told 0 s; room events and key messages each wait out their own 1 s window.
- I reverted the server line (Changed back to Invalidate). Both tests fail on all four cells (seat, told, room events, key messages). My probes R1, R2 and R4 fail too. R3 passes either way, as expected.
- useDmCall.test.ts passes (4 tests). I reverted the client order (leaveConversation calls only leaveDm), and 2 of the 4 tests fail ("leaves the call first ...", "knows the Talk from the conversation list ..."). The other two are negative cases and pass either way, which is correct.
3. New defects, cost, abuse
Cost of Changed compared with Invalidate:
- One snapshot reload, run eagerly on the request goroutine, where Invalidate left it to the next question, which in practice comes at once on a live server.
- One permissions_changed to the leaver's own sessions (debounced; the client refetches channels, permissions and roles).
- sweepStages and enforceVoiceAccess: an O(seats) scan under the hub read lock, limited to one user.
- This is the same work every role, overwrite and timeout change already does.
Abuse by a leave/re-add loop: a member can only remove themselves (dm_handler.go:299-301), so every leave needs membership first. Membership comes from createDM or addDMMember, and both sit behind DMLimit, 3 per minute per user (dm_handler.go:21-26, dm.go:35). So each account can cause about 3 extra reloads a minute, plus the general write limit. A colluding pair cannot make the hub do meaningfully more work than a member who edits their own self-assignable roles (also Changed([user])). Not a concern.
Fail-open check: Changed runs the enforcement through enforceFailClosed (internal/ws/voice_kick.go:183). A snapshot load failure is retried with backoff (permRetry). A panic disconnects the leaver and ends their seats. Neither path leaves the seat in place. The request is not turned into an error after the commit.
Scope: only the leaver is passed. The remaining members' rights do not change, so Changed(nil) would only add work.
Public API (doc/api-stability.md): no route, field or status changes. The endpoint description now names voice_kicked and the voice leave. Those are existing events, and openapi-check passes.
N1 (NOTE), web client, which Talk is found: leaveConversation (web/src/dm/composables/useDmCall.ts:66-70) finds the conversation's Talk only through the tracked call (calls.active[dm.id].talkId) or dm.call.talk_id. Both name at most one Talk per conversation, the latest one rung (R2 shows dm.call moving to the second Talk). A member seated in an older Talk of the same conversation, or one whose call record is gone, does not leave the call first. The server kick still ends the seat in those cases, so nothing leaks. The client handles the kick in handleKicked: the call ends and a "voice.noPermission" toast appears instead of a quiet leave. The client cannot do better without a dm link on channel objects, which the API does not expose (by design, for privacy). I do not ask for a change. The doc comment's "the server ends the seat as well, for clients that do not" already covers it.
N2 (NOTE), the API call fails after the call was left: leaveVoiceChannel is synchronous (session.ts:241-244;
the await is on a void). If DELETE /members/@me then fails (network, 429, 500), the member is out of the call
but still in the conversation, sees the "dm.leaveFailed" toast and can rejoin. This is the safe failure
direction. The reverse order would recreate the defect for a failed leave. Acceptable.
N3 (NOTE), other UI paths: grep leaveDm|members/@me over web/src finds a single caller, DmInfoDialog's Leave
button (web/src/dm/components/DmInfoDialog.vue:49-56), now routed through leaveConversation. I found no context
menu, keyboard shortcut, mobile-specific component or store path that leaves a conversation. The button is
shown only for groups (v-if="dm.group"), so a 1:1 conversation never drops the call for a 400. The desktop
client lives in a separate repository and was not examined. Any client that does not leave the call first is
covered by the server kick.
N4 (NOTE), the leave confirmation copy (web/src/i18n/en.json:4266, "You lose access to its messages ...") does not mention the call. The behaviour is now self-evident (the call ends), so this is optional.
N5 (NOTE), size: internal/chat/dm/dm_handler.go is now 392 lines, under the 400 cap but close to it. The next change there will probably have to split the file.
4. Independent sibling sweep (paths that take access away)
The snapshot reads (internal/perm/snapshot_load.go:134-147): roles, user_roles, users.role = 'admin', instance_settings (rules version, direct_messages_enabled, minimum ages, upload size), users.timeout_until, users.rules_accepted_version, bots, categories, channels (category_id, is_stage), channels.dm_id, dm_members, permission_overwrites. I grepped every production writer that can take access away and matched it to enforcement:
- dm_members delete:
- ServeLeaveDM: now Changed (the fix).
- Account erasure (internal/chat/account/account_erase.go:52): the erased account's sessions are ended. I did not check whether a grace seat survives an erasure (not in scope).
- user_roles delete, roles delete or edit, overwrites set or delete: all go through perm.Service.mutate, which calls Changed(affected) (internal/perm/store.go:177-191). Self-assign calls Changed([user]) (store_self_assign.go:132).
- Bot removal (bot/store.go:253-265): bot handler Changed(nil) (internal/bot/handler.go:211/301/349).
- Channel or category move (layout, channel_layout.go:110): ServeApplyLayout calls structureChanged, which is Changed(nil) (handler_channels.go:324). Channel and category create, delete and duplicate also call structureChanged. is_stage edits call structureChanged (handler_channels.go:237-241).
- ChannelsChanged (Invalidate only) callers: category create, channel edit without is_stage, category rename, auto-delete, Talk rename/limit (talks_handler.go:107). None of them changes an input that removes access. A new, empty category takes nothing away. I agree with the author's table.
- Timeouts (TimeoutMask includes Connect):
- Moderator: Changed([id]) (memberadmin/moderation.go:134,170).
- Automod hits and raid restriction: announceTimeouts calls Changed(ids) (automod/hits.go:143; raid.go:42 goes through it).
- Rules version, direct_messages_enabled, minimum ages: onboarding settings Changed(nil) (settings.go:146). Upload settings: Changed(nil) (perm/service.go:132). Rules acceptance only grants access.
- Time-based changes (timeout end, wait end): the service itself calls Changed (service.go:354,370).
- Owner (users.role 'admin'): I found no runtime writer. It is set from ADMIN_USERNAME (perm.go:3).
- Blocks: not a snapshot input (snapshot_channels.go:76-87 checks DM membership only). I agree with the refuter: documented as private, not an access change.
- dm_start.go:42 and addDMMember (dm_handler.go:251) call Invalidate only. Both only grant access, so live enforcement is not needed.
- automod Engine.Invalidate (automod_rules.go:140/178/208) is the rule cache, not permissions.
Result: after the fix I found no production path that takes away a snapshot input without calling Changed.
5. Gates
All of these were run on 9a9b378bc and pass:
- gofmt; go vet on internal/chat/dm and, with the integration tag, on internal/server.
- make complexity-check (exit 0).
- make openapi-check (exit 0; api/openapi.json matches the annotation).
- go test ./internal/repo/ (file and function size caps).
- node scripts/check-english-only.mjs.
- ESLint on the three touched web files.
- vue-tsc with tsconfig.app.json and tsconfig.tests.json.
Nothing weakened: no allowlist or script change, no limit raised, no test skipped. The diff touches no workflow files.
Commit type: fix (correct). The commit is English only and has no attribution trailer.
Docs match the new behaviour:
- doc/talks.md:69-71 says a leaver loses the Talk at once and the others see them leave the call.
- The endpoint description says the same.
What I ran
Every command ran in a detached temp worktree of 9a9b378bc, under nice -n 10, with go -p 2 and at most 2 vitest workers:
- go test -tags=integration -count=1 -run
'TestLeavingAGroupConversationEndsItsCall|TestConversationRevocationMatrix|TestFixReview' ./internal/server/
- on the fix: all pass;
- with the server line reverted to Invalidate: the two fix tests and probes R1, R2 and R4 fail, R3 passes;
- with my R4 probe corrected: pass.
- npx vitest run src/dm/composables/useDmCall.test.ts
- on the fix: 4 pass;
- with the client order reverted: 2 fail.
- The gates listed in section 5.
What I did not check
- RTP actually forwarded or stopped at the SFU (no WebRTC peer; read from code).
- The remaining clients' MLS removal of the leaver, end to end (read from code).
- The desktop client repository.
- Account erasure's handling of a grace seat.
- The dmAndTalkIDs-before-commit race in the last-member case.
- The full integration suite: I ran targeted tests only.