Skip to content

Video source lifecycle memory fixes - #1400

Merged
alan-george-lk merged 7 commits into
alan/bugfix-cuda-context-lifecyclefrom
alan/bugfix-video-source-lifecycle
Sep 8, 2026
Merged

Video source lifecycle memory fixes#1400
alan-george-lk merged 7 commits into
alan/bugfix-cuda-context-lifecyclefrom
alan/bugfix-video-source-lifecycle

Conversation

@alan-george-lk

@alan-george-lk alan-george-lk commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Stop the raw-video keepalive task from cloning NativeVideoSource. It now uses a Weak liveness check so dropping the source actually releases the native handle and black I420 keepalive buffer.
  • Previously, a source that never received captureFrame kept that task alive forever (~1.32 MiB per unused 720p source).
  • Also pair CUDA context init/shutdown on NVIDIA encoder/decoder factories, drop leftover FFI handles in dispose(), and fail closed if nvEncInitializeEncoder throws.

Testing

  • Adds regression tests directly
  • 1,000 create/drop cycles peaked at ~1.35 GiB; after the fix they peak at ~41 MiB.

@github-actions

github-actions Bot commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

Changeset ✓

This PR includes a changeset covering all affected packages:

Package Bump
libwebrtc patch
livekit patch
livekit-ffi patch
webrtc-sys patch

@alan-george-lk alan-george-lk changed the title WIP: Video source lifecycle Video source lifecycle Sep 4, 2026
@alan-george-lk alan-george-lk changed the title Video source lifecycle Video source lifecycle memory fixes Sep 4, 2026
@alan-george-lk
alan-george-lk marked this pull request as ready for review September 4, 2026 16:06
devin-ai-integration[bot]

This comment was marked as resolved.

@alan-george-lk
alan-george-lk force-pushed the alan/bugfix-video-source-lifecycle branch from ebcf10b to 41a6073 Compare September 4, 2026 18:16
Comment thread libwebrtc/src/native/video_source.rs Outdated
Comment thread libwebrtc/src/native/video_source.rs Outdated
Comment thread libwebrtc/src/native/video_source.rs Outdated
loop {
interval.tick().await;

if !keepalive_should_continue(&captured_frames) {

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.

note: Not related to changes in this PR, but I wonder what happens if you initially publish some frames (which would kill this task) but later stop publishing frames (e.g., camera temporally goes offline so there are no frames available). This might be the correct logic, but worth verifying with WebRTC.

Comment thread libwebrtc/src/native/video_source.rs Outdated
break;
}

let mut builder = vf_sys::ffi::new_video_frame_builder();

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.

note: Also not related to this PR, but this is something we should work to eliminate over time; FFI details between Rust and libwerbtc should not be public API to this layer.

@alan-george-lk
alan-george-lk force-pushed the alan/bugfix-video-source-lifecycle branch from 41a6073 to b583cc1 Compare September 7, 2026 23:10
@alan-george-lk
alan-george-lk force-pushed the alan/bugfix-video-source-lifecycle branch from 8db24e5 to d046afd Compare September 8, 2026 13:58
@alan-george-lk
alan-george-lk merged commit a213934 into main Sep 8, 2026
22 checks passed
@alan-george-lk
alan-george-lk deleted the alan/bugfix-video-source-lifecycle branch September 8, 2026 15:56
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.

2 participants