Skip to content

fix(rtc-node): stop routing FFI events to a room the server disconnected - #792

Open
CSantosM wants to merge 2 commits into
livekit:mainfrom
CSantosM:fix/rtc-remove-ffi-listener-on-server-disconnect
Open

CSantosM wants to merge 2 commits into
livekit:mainfrom
CSantosM:fix/rtc-remove-ffi-listener-on-server-disconnect

Conversation

@CSantosM

@CSantosM CSantosM commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Room.disconnect() is the only place that removes the room's onFfiEvent listener from the FfiClient, and it returns early once the room is no longer connected. When the server ends the connection (the room is deleted with RoomServiceClient.deleteRoom, the participant is removed, the room closes), the FFI disconnected event runs cleanupOnDisconnect() and RoomEvent.Disconnected fires, but the listener stays on the FfiClient singleton for the life of the process. Calling room.disconnect() afterwards does not remove it either, since the room is already disconnected.

In a long-running process that joins many rooms and is disconnected by the server when each one ends (a server-side participant or agent, for example):

  • every FFI event of the process is also dispatched to every room disconnected this way, and each of them takes its ffiEventLock before discarding the event, so the cost of each event grows with the number of rooms ever disconnected server-side;
  • the listener keeps each of those Room objects, with their participants and publications, from being garbage collected;
  • calling connect() again on such a Room registers onFfiEvent a second time.

Reproduction

Against a local LiveKit server: three rounds of 50 rooms, each joined by one Room and then deleted with RoomServiceClient.deleteRoom, waiting for RoomEvent.Disconnected each time.

ffi_event listeners after round 1 / 2 / 3 time per round
@livekit/rtc-node 1.2.3 50 / 100 / 150 3.2 s / 4.1 s / 5.4 s
this branch 0 / 0 / 0 2.9 s / 2.8 s / 2.9 s
Script
import { Room, RoomEvent } from '@livekit/rtc-node';
import { AccessToken, RoomServiceClient } from 'livekit-server-sdk';

const [key, secret] = ['devkey', 'secret'];
const svc = new RoomServiceClient('http://localhost:7880', key, secret);
const ffiListeners = () => globalThis._ffiClientInstance.listenerCount('ffi_event');

for (let round = 1; round <= 3; round++) {
  for (let i = 0; i < 50; i++) {
    const name = `ffi-listener-repro-${round}-${i}-${Date.now()}`;
    await svc.createRoom({ name });
    const at = new AccessToken(key, secret, { identity: 'bot' });
    at.addGrant({ roomJoin: true, room: name });
    const room = new Room();
    await room.connect('ws://localhost:7880', await at.toJwt());
    const disconnected = new Promise((resolve) => room.once(RoomEvent.Disconnected, resolve));
    await svc.deleteRoom(name);
    await disconnected;
  }
  console.log(`round ${round}: ffi_event listeners = ${ffiListeners()}`);
}
process.exit(0);

Change

cleanupOnDisconnect(), which both the explicit and the server-initiated path run once per connection, now removes onFfiEvent from the FfiClient, before it emits RoomEvent.Disconnected. The removal that disconnect() did after its callback is gone: when the FFI room event arrives first and a connect() is started from RoomEvent.Disconnected, that trailing removal would take the listener away from the new connection.

Tests

New unit tests in room.test.ts, on the ffi_event registrations of a room:

  • removed when the server disconnects the room (an FFI disconnected room event; fails on main);
  • removed by disconnect();
  • kept, once, for a connect() started from RoomEvent.Disconnected while disconnect() waits for its callback (passes on main, and would fail if disconnect() still removed the listener).

vitest run src (the e2e suites excluded) passes, pnpm lint reports no new warnings, and the reproduction above was rerun on the final version.

  • Changeset: patch for @livekit/rtc-node.

Room.disconnect() is the only place that removed onFfiEvent from the
FfiClient, and it returns early when the room is no longer connected. A
room disconnected by the server (room deleted, participant removed, room
closed) therefore kept its listener for the life of the process: every
later FFI event of any room reached it, and the closure kept the Room
from being garbage collected.

cleanupOnDisconnect(), which both disconnect paths run, now removes the
listener.
devin-ai-integration[bot]

This comment was marked as resolved.

disconnect() still removed onFfiEvent after its callback. When the FFI
room event wins the race, cleanupOnDisconnect() has already removed it
and emitted RoomEvent.Disconnected; a connect() started from that event
registers the listener again, and the trailing removal then took it away
from the new connection. cleanupOnDisconnect() now owns the removal, and
a test covers a connect() started from RoomEvent.Disconnected.
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