Skip to content

fix(native_surface): SurfaceRenderer stops and joins its video completion thread before releasing the device - #1500

Merged
Hona merged 1 commit into
devfrom
fix/767-video-completion-thread
Nov 23, 2026
Merged

Hona merged 1 commit into
devfrom
fix/767-video-completion-thread

Conversation

@Hona

@Hona Hona commented Nov 23, 2026

Copy link
Copy Markdown
Member

Fixes #767

Root cause

  • SurfaceRenderer (crates/rune-media/src/native_surface/pipeline.rs) spawned the rune-video-completion thread holding a cloned wgpu::Device and discarded its JoinHandle.
  • With no Drop impl, SurfaceRenderer dropped device before its completion channel sender and never joined the worker, letting an in-flight Device::poll race device and driver teardown (segfaulting at exit on Vulkan/NVIDIA).
  • SurfaceRenderer::submit (crates/rune-media/src/native_surface/pipeline/frame.rs) also cloned the strong SyncSender into encoder.on_submitted_work_done, so channel-disconnect shutdown alone could block if a pending callback still held a sender.

Changes

  • Added CompletionWorker (crates/rune-media/src/native_surface/pipeline/completion.rs) with an explicit Signal state machine (Idle, Pending, Polling { rewake }, Stopped) over a Mutex + Condvar and an owned JoinHandle.
  • Implemented Drop for SurfaceRenderer to release timing_queue (preventing an in-flight completion callback from submitting follow-up timestamp resolves) and call completion.stop(), which marks Stopped, wakes the worker immediately out of idle or its 1 ms poll wait, and joins the thread before SurfaceRenderer.device drops.
  • Handled all exit paths safely:
    • Normal shutdown (idle, between polls, or mid-poll — finishes the single in-flight nonblocking poll while the device is still alive, then exits without polling again).
    • Lingering callback wakers (CompletionWake clones never keep the worker alive).
    • Lost device (Err(_) exits and releases its wgpu::Device immediately).
    • Panics (worker unwind marks Stopped via ExitGuard and drops its device; caller unwind joins without panicking in Drop).
    • Re-entrant drop from a callback running on the completion thread itself (signals Stopped without self-joining).
  • Added crates/rune-media/src/native_surface/completion_tests.rs covering every worker exit path plus real SurfaceRenderer drop-joins-poll tests (with and without timestamp queries).
stateDiagram-v2
    [*] --> Idle
    Idle --> Pending: wake()
    Pending --> Polling: worker takes wakeup
    Polling --> Polling: wake() sets rewake=true
    Polling --> Idle: QueueEmpty & !rewake
    Idle --> Stopped: stop()
    Pending --> Stopped: stop()
    Polling --> Stopped: stop() / poll Err / unwind
    Stopped --> [*]
Loading

Verification

  • cargo test -p rune-media dropping_surface_renderer_: failed on dev before the fix (SurfaceRenderer::drop returned while the worker was still inside poll), passed after the fix.
  • cargo test -p rune-media native_surface: 27 passed (including 11 new tests in native_surface::completion_tests and completion_worker_preserves_cpu_gpu_overlap).
  • cargo check -p rune -p rune-kit: passed.
  • cargo check -p rune-media --no-default-features --target wasm32-unknown-unknown: passed.
  • scripts/file-size.sh: passed (0 errors).
  • Not checked on this host: a real Linux Vulkan/NVIDIA driver session (verified on Windows DX12 and through deterministic unit tests covering worker teardown ordering).

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.

native_surface: the video completion thread polls the device after it is torn down, and the process segfaults

1 participant