Skip to content

fix(voice): an older occupancy snapshot never undoes a newer join or leave - #384

Open
Aizen93 wants to merge 2 commits into
developfrom
fix/voice-occupancy-version
Open

Aizen93 wants to merge 2 commits into
developfrom
fix/voice-occupancy-version

Conversation

@Aizen93

@Aizen93 Aizen93 commented Nov 29, 2026

Copy link
Copy Markdown
Owner

What was wrong

A client that connects or reconnects receives each visible voice channel's occupants as a snapshot (voice:channel_users, source: 'connect'). The server reads it from Redis and sends it after a database read, and the socket is already in its channel rooms by then: a voice:user_joined or voice:user_left sent in that window reached the client first and was then overwritten by the older snapshot. The sidebar showed someone who had left, or missed someone who had joined, until the next event about that person or the next reconnect. The same held for the replay after a space join and for the list a joiner receives.

Two more things of the same family:

  • A join whose session ended during the reads that follow its Redis write was still announced as an arrival, after its departure had gone out, and was still sent the occupants' list.
  • The list a joiner receives could name someone who had left while it was being built, with default flags.

What the code does now

Server

  • Every join, leave and reap of a voice channel's Redis mirror runs one Lua script as the last command of the MULTI that writes it: the channel's occupancy version becomes Redis's clock in microseconds (TIME), forced above the value stored for the channel. The key voice:channel:version:{channelId} is not deleted with the channel's state and expires a week after the channel's last change, so a channel that empties and fills again, a node restart and an owner takeover never bring the version back down.
  • voice:user_joined, voice:user_left and voice:channel_users carry it as the optional occupancyVersion. A snapshot reads every channel's occupants and then every channel's version in one MULTI (still three Redis round trips whatever the number of spaces) and is sent with the version it was read at. The list a joiner receives is built on the channel's owner node after the join's write answered and carries the version just below the join's; an occupant who left while it was built is left out of it.
  • Timing: a departure is announced once its Redis write answered, one round trip later than before. While Redis does not answer, it goes out without a version at most 2 s after the write was issued. A join's events wait for their write the same way. State, speaking and screen-share events go out at once.
  • A write whose version script alone failed stands, and its events go out without a version, with one warning. The dead-node reaper and the boot cleanup then go on to the next channel. Any other failure is handled as before. A leave or a reap on a Redis over its memory limit is that case: the deletes go through, the script's write is refused, and the departure is still announced.
  • A join whose session ended during one of the reads that follow its write is sent nothing more from that read on (not the occupants' list, not the annotation scene, not its transports) and is not announced.
  • A space's deletion clears its channels' mirrors without a version and sends no event, as before.

Desktop

  • Per channel, the voice store keeps the version of the last list applied and each user's latest event since, outside anything that renders (stores/voiceOccupancy.ts, at most 500 users a channel).
    • An event at or below what the list reflects for that user changes nothing.
    • A list older than the one shown changes no row.
    • A newer list replaces the rows, except for the users whose latest event is newer than it, who stay as they are.
  • A list or an event without a version applies as it did before. The bookkeeping is forgotten where the lists are cleared: the reconnect, a deleted space's channels, the logout. The active channel's connect snapshot stays ignored.
  • Sounds and the answer of removeUserFromChannel follow the events as before, and so do the departures the secure-voice key exchange is handed. What it is handed as present follows the list:
    • an arrival the list refused by its version is not handed over;
    • for the names a list carries, it is handed the rows the store shows once the list is merged: nothing for a list refused as older than the one shown, nothing for someone whose newer event is a departure, and the current session of someone whose newer event is an arrival;
    • an arrival or a list without a version is handed over as before.
  • The three handlers MainLayout registered inline live in services/voiceOccupancySocketHandlers.ts.

Compatibility. The field is optional in both directions: a 2.1.x client ignores it, and this client with a server that does not send it behaves as before.

Key files

  • Server: apps/server/src/utils/voiceMirror.ts, apps/server/src/websocket/voiceHandler.ts
  • Shared: packages/shared/src/types.ts, packages/shared/src/constants.ts
  • Desktop: apps/desktop/src/stores/voiceOccupancy.ts, apps/desktop/src/stores/voiceStore.ts (setChannelUsers, addUserToChannel, removeUserFromChannel), apps/desktop/src/services/voiceOccupancySocketHandlers.ts
  • scripts/test-multi-node.ts, Architecture.md (the voice occupancy paragraph)

Migrations

None. Redis gains one small key per voice channel that has had an occupant, which expires a week after the channel's last change.

Tests

  • Server: the script's text, and the version written in each join, leave, forced leave, eviction, reap and takeover MULTI; the snapshot read (occupants and version in one MULTI, a fixed number of round trips) and every emitter carrying the field; the script-alone failure in the helper, a reap, the reaper's tick, the boot cleanup, a takeover, a join and a leave; the ended-join guards after both reads (a leave, and a re-join of the same socket); an occupant leaving while the joiner's list is built; changes written between a join's write and its list.
  • Desktop, through the real store and the real handlers: a join or a leave that lands during a replay; newer, older and out-of-order lists; a server without versions, and unversioned events beside versioned ones; the reconnect's clear, the active channel's connect rule, the sounds and the screen-share reference; what the key exchange is handed in each order of a list and the events around it. The key exchange itself: an announcement of the session it already holds changes nothing.
  • scripts/test-multi-node.ts: versions grow across two nodes, across an emptied channel and across an owner takeover.
  • Results: server suite 205 files (7379 passed, 3 skipped), desktop suite 312 files (5941 passed, 1 skipped), typecheck and lint clean. The version script was also run on a Redis 7 through the server's own client: the reply is a number JavaScript holds exactly, successive changes are strictly increasing (inside one MULTI and with a stored value ahead of the clock too), the key expires after one week, and a MULTI whose script alone fails comes back as the case the code handles (also for a leave over the memory limit; a join there is rejected whole, as before).

To check by hand

  1. The "Multi-node and permission scripts" workflow on this pull request (two nodes, the version assertions).
  2. Two clients in one space: cut and restore one client's network while the other joins or leaves a voice channel. After the reconnect the sidebar shows who is really there.
  3. A secure voice channel with three members: one leaves and joins again quickly while another is joining. Everyone hears everyone afterwards.
  4. A 2.1.x client against this server: voice lists behave as today.

Every join, every leave and every reap of a voice channel's mirror runs
one Lua script as the last command of the MULTI that writes it: the
channel's occupancy version becomes Redis's clock in microseconds (TIME),
forced above the last value stored for the channel, and the MULTI answers
it. The key voice:channel:version:{id} is never deleted with the channel's
state; it expires a week after its last change. A space's deletion clears
its channels' mirrors without a version and sends no event.

voice:user_joined and voice:user_left carry that version as
occupancyVersion. A departure goes out once the leave's write answered, or
without a version OCCUPANCY_VERSION_WAIT_MS after the write was issued; a
join's events wait for theirs the same way. Only occupancy events wait:
state, speaking and screen-share events go out at once and can precede a
departure.

The joiner's list of those already there is built on the owner node after
the join's write answered: it holds at least every change up to the join
and carries the version just below the join's. Each row is read again once
the user rows are fetched, so an occupant who left meanwhile is left out
of it; such a row used to be sent with default flags and no session.

A snapshot (getVoiceStateForServers) reads every channel's occupants and
then every channel's version in its third MULTI, still three round trips,
and replayVoiceState sends each list with the version it was read at.

When the script's reply is the only error of a write's MULTI (the client
rejects exec() after Redis applied the other commands), the join, the
leave or the reap stands and its events go out without a version, with
one warning; any other failure is handled as before. The dead-node
reaper's tick and the boot cleanup then go on to the next channel. A
leave or a reap on a Redis over its memory limit is that case: its
deletes go through and the script's write is refused.

A join whose session ended during one of the reads that follow its write
(the occupants' user info, the annotation hydration) is sent nothing more
from that read on: not the occupants' list, not the scene, not its
transports, and it is not announced.

The field is optional on the wire: a client that does not know it ignores
it, and an event or a list without it applies as before.

Refs #195
…event

The voice store judges every occupancy list and event by the server's
occupancyVersion (stores/voiceOccupancy.ts). Per channel it keeps, outside
anything that renders, the version of the last snapshot applied and, per
user, the version of the latest event applied since. An event at or below
what the list reflects for that user leaves the list as it is. A snapshot
older than the last one applied changes no row. A newer snapshot replaces
the list except for the users whose latest event is newer than it, who stay
as the list shows them: a join or a leave that lands while a replay is on
its way survives the snapshot.

The sounds and the answer of removeUserFromChannel follow the events as
before, and so do the departures the secure-voice keying is handed,
whatever their version. What the keying is handed as present follows the
list:
- an arrival the list refused by its version is not handed over: it is no
  newer than what the list already reflects for that user, and the keying
  learns their current session from that later event or from the list of
  its own join;
- for the names a list carries, the keying is handed the rows the store
  shows once the list is merged: nothing for a list refused as older than
  the one shown, nothing for a name whose newer event is a departure, and
  the current session of a name whose newer event is an arrival (a repeat
  the keying ignores when that arrival was handed to it);
- an arrival or a list without a version is handed over as before.

A departure already reflected leaves the screen-share reference, which
follows the row, as it is. A list or an event without a version applies as
before and leaves the bookkeeping alone, and the active channel's connect
snapshot stays ignored.

The bookkeeping is forgotten where the lists are cleared: the reconnect's
synchronous clear, a deleted space's channels and the logout. The three
handlers MainLayout registers live in
services/voiceOccupancySocketHandlers.ts. Architecture.md describes the
version and the merge.

Closes #195
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant