Skip to content

fix(rtc-node): stop capturing native logs nothing consumes - #793

Open
CSantosM wants to merge 1 commit into
livekit:mainfrom
CSantosM:fix/rtc-no-ffi-log-capture
Open

CSantosM wants to merge 1 commit into
livekit:mainfrom
CSantosM:fix/rtc-no-ffi-log-capture

Conversation

@CSantosM

Copy link
Copy Markdown
Contributor

FfiClient initializes the FFI server with captureLogs set to true, as it has since the initial commit, but nothing in this SDK handles the logs FFI event. In capture mode the Rust FfiLogger accepts every record down to trace level (enabled() returns true and the max level is Trace), so every native log, including libwebrtc's text_pcap_packet_observer dump of each data channel packet, crosses into JavaScript, is decoded with FfiEvent.fromBinary and is emitted to every ffi_event listener, only to be discarded. RUST_LOG has no effect while capture is on.

In a process that holds several rooms, each of those events also reaches every Room.onFfiEvent, which takes its ffiEventLock before discarding it, so the cost grows with the number of rooms times the traffic of all of them.

With capture off, the native logger falls back to env_logger, so RUST_LOG controls the native logs again (errors only when it is unset).

Measurements

Against a local LiveKit server, one process holding N hidden, subscribe-only agent rooms (autoSubscribe: false), while another process joins and leaves 2 participants per room in two rounds. Node 24, NODE_ENV=production.

Log records forwarded to JavaScript in 17 s with 10 rooms:

log records (one logs event each)
@livekit/rtc-node 1.2.3 50,998: 50,531 trace and 467 debug; 48,539 of them from libwebrtc
this branch 0

Process holding 100 rooms:

FFI events/s CPU of one core, avg / max event loop delay p99 / max slowest connect()
@livekit/rtc-node 1.2.3 ~7,000 30% / 69% 65 ms / 100 ms n/a
this branch ~50 7% / 31% 11 ms / 23 ms 114 ms

With 25 and 50 rooms this branch stays at 6% CPU and 11 ms p99, while 1.2.3 goes from 15 ms to 32 ms p99 as the rooms double.

Notes

  • With RUST_LOG unset, libwebrtc now prints ERROR libwebrtc] (cuda_context.cpp:86): Failed to initialize CUDA. to stderr on hosts without a GPU. It was emitted before as well and dropped in JavaScript.
  • If capturing the logs into the SDK's lk-rtc logger is ever wanted, the Rust side would also have to filter by level in capture mode; otherwise every trace record keeps crossing the boundary.
Script that counts the forwarded logs
import { Room } from '@livekit/rtc-node';
import { AccessToken, RoomServiceClient } from 'livekit-server-sdk';

const [key, secret, N] = ['devkey', 'secret', 10];
const svc = new RoomServiceClient('http://localhost:7880', key, secret);
const token = async (room, identity, grant) => {
  const at = new AccessToken(key, secret, { identity });
  at.addGrant({ roomJoin: true, room, ...grant });
  return at.toJwt();
};

const levels = {};
const rooms = [];
for (let i = 0; i < N; i++) {
  const name = `log-capture-${i}`;
  await svc.createRoom({ name });
  const room = new Room();
  await room.connect('ws://localhost:7880', await token(name, 'saver', { hidden: true, canPublish: false }), {
    autoSubscribe: false,
  });
  rooms.push(room);
}
globalThis._ffiClientInstance.on('ffi_event', (e) => {
  if (e.message.case !== 'logs') return;
  for (const r of e.message.value.records) levels[r.level] = (levels[r.level] ?? 0) + 1;
});

const people = [];
for (let i = 0; i < N; i++) {
  for (let p = 0; p < 2; p++) {
    const room = new Room();
    await room.connect('ws://localhost:7880', await token(`log-capture-${i}`, `p${p}`, { canPublishData: true }));
    people.push(room);
  }
}
await new Promise((r) => setTimeout(r, 10_000));
for (const room of [...people, ...rooms]) await room.disconnect();
console.log('forwarded log records by level', levels);
process.exit(0);

FfiClient initializes the FFI server with captureLogs set to true, as it
has since the initial commit, but no code in this SDK handles the `logs`
FFI event. In capture mode the Rust logger forwards every record down to
trace level, libwebrtc's dump of each data channel packet included, so a
process decoded and dispatched thousands of unused events per second, and
each one reached every Room's onFfiEvent and its lock.

With capture off the native logger falls back to env_logger, so RUST_LOG
controls the native logs again.

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no bugs or issues to report.

Devin Review

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