A platform for community discussion. Free, open, simple.
FIX: LiveKit token refresh bypasses the room participant cap (#43922)
Penar Musaraj2008f579c5f2
diff --git a/plugins/voice/app/controllers/voice/rooms_controller.rb b/plugins/voice/app/controllers/voice/rooms_controller.rbindex 006c4214c9e..437b95232cc 100644--- a/plugins/voice/app/controllers/voice/rooms_controller.rb+++ b/plugins/voice/app/controllers/voice/rooms_controller.rb@@ -311,10 +311,19 @@ module Voicereturn(render_json_error(I18n.t("voice.errors.livekit_room_instance_ended"), status: 410))end+ # The reconnect ladder may have outlived presence, but it must still+ # claim a slot atomically before receiving a new LiveKit join token.+ admission =+ Voice::ParticipantTracker.add_within_capacity(+ @room.id,+ current_user.id,+ @room.effective_max_participants,+ )+ return render_json_error(I18n.t("voice.errors.room_full"), status: 422) if admission == :full+# The reconnect ladder only runs while the client considers itself in the- # call, so an explicit token request voids any leave/kick tombstone.+ # call, so an admitted explicit token request voids any leave/kick tombstone.Voice::ParticipantTracker.clear_left(@room.id, current_user.id)- Voice::ParticipantTracker.add(@room.id, current_user.id)# The session may have expired along with the lapsed presence — mint a# fresh one so heartbeat/state keep working after the reconnect.participant_session_id =diff --git a/plugins/voice/spec/requests/voice/rooms_controller_livekit_spec.rb b/plugins/voice/spec/requests/voice/rooms_controller_livekit_spec.rbindex b7532ffcfa9..b2a5061f25c 100644--- a/plugins/voice/spec/requests/voice/rooms_controller_livekit_spec.rb+++ b/plugins/voice/spec/requests/voice/rooms_controller_livekit_spec.rb@@ -197,6 +197,25 @@ RSpec.describe Voice::RoomsController doexpect(response.status).to eq(403)end+ it "refuses a token for a new participant when the room is full" do+ full_room_participant = Fabricate(:user)+ room.update!(max_participants: 2)+ Voice::ParticipantTracker.pin_transport!(room.id, "livekit")+ Voice::ParticipantTracker.add(room.id, other_user.id)+ Voice::ParticipantTracker.add(room.id, full_room_participant.id)+ sign_in(user)++ post "/voice/rooms/#{room.id}/livekit_token.json"++ expect(response.status).to eq(422)+ expect(response.parsed_body["errors"]).to include(I18n.t("voice.errors.room_full"))+ expect(response.parsed_body).not_to have_key("token")+ expect(Voice::ParticipantTracker.user_ids(room.id)).to contain_exactly(+ other_user.id,+ full_room_participant.id,+ )+ end+it "reissues a token, re-adds lapsed presence, and mints a fresh participant session" dosign_in(user)Voice::ParticipantTracker.pin_transport!(room.id, "livekit")