node-sdks icon indicating copy to clipboard operation
node-sdks copied to clipboard

Memory leak when publishing tracks

Open thepacketloss opened this issue 10 months ago • 0 comments

Select which package(s) are affected

@livekit/rtc-node

Describe the bug

When publishing tracks there will be some memory leaks that will increase over time. I used heaptrack in order to track memory leak location and I will heaptrack file and result screenshot.

btw I tested this scenario using rust sdk and in that case memory leaks only happens when I unpublish tracks multiple times but using node sdk in addition to that memory leaks happen even publishing single track normally. also I created an issue in rust sdk repo. https://github.com/livekit/rust-sdks/issues/612

heap.zip

Image

Reproduction

const room = new Room();
await room.connect('ws://localhost:7880', jwt, {autoSubscribe: true, dynacast: true});

room.on(RoomEvent.TrackSubscribed, async (track, publication, participant) => {
    const source = new VideoSource(1920, 1080);
    const av1_track = LocalVideoTrack.createVideoTrack('av1_track', source);
    const vp9_track = LocalVideoTrack.createVideoTrack('vp9_track', source);
    const vp8_track = LocalVideoTrack.createVideoTrack('vp8_track', source);
    await room.localParticipant.publishTrack(av1_track, {
        simulcast: false,
        videoCodec: VideoCodec.AV1,
        source: TrackSource.SOURCE_SCREENSHARE,
        videoEncoding: {
            maxBitrate: 250_000,
            maxFramerate: 10
        }
    })
    await room.localParticipant.publishTrack(vp9_track, {
        simulcast: false,
        videoCodec: VideoCodec.VP9,
        source: TrackSource.SOURCE_SCREENSHARE,
        videoEncoding: {
            maxBitrate: 250_000,
            maxFramerate: 10
        }
    })
    await room.localParticipant.publishTrack(vp8_track, {
        simulcast: false,
        videoCodec: VideoCodec.VP8,
        source: TrackSource.SOURCE_SCREENSHARE,
        videoEncoding: {
            maxBitrate: 250_000,
            maxFramerate: 10
        }
    })

    const stream = new VideoStream(track);
    for await (const buf of stream) {
        await source.captureFrame(buf.frame);
    }

    await source.close()
    stream.close()
});

Logs


System Info

Binaries:
    Node: 23.9.0 - /usr/bin/node
    Yarn: 1.22.22 - /usr/bin/yarn
    npm: 11.2.0 - /usr/bin/npm
    pnpm: 8.15.4 - ~/.local/share/pnpm/pnpm
    bun: 1.1.21 - ~/.bun/bin/bun
  npmPackages:
    @livekit/rtc-node: ^0.13.8 => 0.13.8 
    livekit-server-sdk: ^2.10.2 => 2.10.2

LiveKit server version

latest self hosted version

Severity

blocking all usage of LiveKit

Additional Information

No response

thepacketloss avatar Mar 30 '25 10:03 thepacketloss