Skip to content

[Bug]: Unchecked RTP packet forwarding to non-connected WebRTC peers in UdpSocketManager causes high CPU churn and event-loop lag #511

Open

Description

@MPoojaSiyag

Bug Description

In src/server/signalling/handlers/udpSocket.ts (lines 100-113), the UDP message handler forwards high-frequency incoming RTP packets (typically 2,000 to 4,000 packets per second from GStreamer) to all sessions registered in this.clients:

socket.on("message", (msg) => {
    for (const client of this.clients.values()) {
        try {
            if (trackKind === "video") {
                client.videoTrack.writeRtp(msg)
            } else {
                client.audioTrack.writeRtp(msg)
            }
            client.bytesSent += msg.length
        } catch {
            // Individual track write errors are non-fatal
        }
    }
})

However, a newly initiated session is added to this.clients during handleOffer, well before DTLS and SRTP negotiation finish (pc.connectionState is "new" or "connecting"). Similarly, disconnected sessions remain in this.clients during their 10-second grace timer (pc.connectionState is "disconnected" or "failed").

Because socket.on("message") iterates over this.clients.values() unconditionally without verifying whether client.pc.connectionState === "connected":

  1. It attempts to call writeRtp(msg) thousands of times per second on sessions whose SRTP cipher contexts are uninitialized or closed.
  2. This throws and catches thousands of internal exceptions per second inside the Node.js event loop, creating heavy CPU churn.
  3. This event loop congestion induces noticeable latency spikes on real-time WebRTC DataChannel input message processing and unnecessarily inflates bytesSent on dead or half-open connections.

Steps to Reproduce

  1. Start the Rein desktop server with GStreamer streaming enabled (npm run dev).
  2. Observe server process CPU utilization when a client connects or during session renegotiation/disconnect grace periods.
  3. Inspect UdpSocketManager.createRtpSocket: for every received UDP datagram (2,000-4,000/s), client.videoTrack.writeRtp(msg) is invoked unconditionally even if client.pc.connectionState is "new", "connecting", "disconnected", or "failed".

Expected Behavior

UdpSocketManager should verify if (client.pc.connectionState !== "connected") continue before attempting track writes, ensuring packets are dispatched only to fully established peer connections.

Desktop Operating System

Windows 11

Client OS and browser

Windows 11, Chrome 130

Rein Version

1.0.0

Console Logs / Error Messages (Optional)

Location: `src/server/signalling/handlers/udpSocket.ts` (lines 100-113).

Screenshots / Recordings (Optional)

N/A (Backend networking / event-loop optimization issue)

Checklist

  • I have searched for existing issues to avoid duplicates

Activity

  1. github-actions commented on Nov 22, 2026

    @github-actions

    Discussions are carried on Discord.

    Instead of commenting here, go to the Discord channel.

    macOS and macOS-related issues are not supported at the moment. We are looking for funding to support macOS and would appreciate sponsorships.

    If you find this project useful, a GitHub star would be appreciated.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingenhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions