Skip to content

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

Closed

Description

@adeerkhan

The lib test binary segfaults at process exit, in the video completion thread, on a poll that
reaches driver state that has already gone.

What happens

cargo test -p rune --lib finishes every test, prints its summary, and then the process dies with
signal 11:

test result: FAILED. 1884 passed; 18 failed; 11 ignored; 0 measured; 0 filtered out; finished in 176.98s
error: process didn't exit successfully: .../rune-<hash> (signal: 11, SIGSEGV: invalid memory reference)

It is intermittent: it happened in three of five full runs at dev 2e2ade78 and 1a286bb8 on
Linux, and not in the others. A core dump (coredumpctl) says which thread and where:

Signal: 11 (SEGV) si_code: SEGV_MAPERR
  TID: 69886 (rune-video-comp)

#0  ??? () at /lib64/libnvidia-glcore.so.615.78.08
#1  ??? () at /lib64/libnvidia-glcore.so.615.78.08
#2  <ash::device::Device>::get_semaphore_counter_value ()
#3  <wgpu_hal::vulkan::Fence>::get_latest ()
#4  <wgpu_hal::vulkan::Device as wgpu_hal::Device>::get_fence_value ()
#5  <wgpu_hal::vulkan::Device as wgpu_hal::dynamic::device::DynDevice>::get_fence_value ()
#6  <wgpu_core::device::resource::Device>::maintain ()
#7  <wgpu_core::device::resource::Device>::poll_and_return_closures ()
#8  <wgpu_core::global::Global>::device_poll ()
#9  <wgpu::backend::wgpu_core::CoreDevice as wgpu::dispatch::DeviceInterface>::poll ()
#10 <wgpu::api::device::Device>::poll ()
#11 <rune::native_surface::pipeline::SurfaceRenderer>::with_pipeline::{closure#1} ()
       at crates/rune/src/native_surface/pipeline.rs:167

Why

crates/rune/src/native_surface/pipeline.rs starts a thread named rune-video-completion that
polls the device in a loop:

let (completion, receiver) = std::sync::mpsc::sync_channel(1);
let poll_device = device.clone();
std::thread::Builder::new()
    .name("rune-video-completion".into())
    .spawn(move || {
        while receiver.recv().is_ok() {
            loop {
                match poll_device.poll(wgpu::PollType::Poll) { ... }
            }
        }
    })

SurfaceRenderer holds that sender and the wgpu::Device, and it has no Drop, so its fields
drop in declaration order — the device first, and the sender (which is what ends the thread's
wait) afterwards. The thread is therefore still polling a device that is being torn down, and
Device::poll walks the device's queues and fences, which the driver has already let go of.

The thread is not joined either, so the process can exit while it is inside a driver call.

Impact

A crash in a background thread that races the device's teardown. It shows up in the test binary
here, but the same race is in any app that builds a SurfaceRenderer — a native surface (a video
or a camera) dropped while the completion thread polls — and it ends the process. On the NVIDIA
driver it is a segfault; the same use-after-free may read freed memory quietly elsewhere.

Fix

Stop the thread before the device goes: drop the sender, then join it, in a Drop for
SurfaceRenderer — so the poll in flight finishes while the device is still whole.

A branch with that fix is at
adeerkhan/rune, commit
"native_surface: join the video completion thread before the device goes".

Activity

  1. adeerkhan commented on Nov 12, 2026

    @adeerkhan
    Author

    Verified on the branch: the crash is gone.

    The same command, five full runs on this host, before and after the commit:

    build run exit
    2e2ade78 cargo test -p rune --lib 139 (SIGSEGV)
    1a286bb8 cargo test -p rune --lib 139 (SIGSEGV)
    branch, before this commit direct run 139 (SIGSEGV)
    branch, before this commit cargo test -p rune --lib 101
    branch, after this commit direct run × 3 101, 101, 101

    So three of five runs crashed before the commit and none of the three after it. The failures
    themselves are unchanged (the same 18-19, listed in #639).

    The branch is at
    adeerkhan/rune, commit
    "native_surface: join the video completion thread before the device goes": SurfaceRenderer drops
    the sender and joins the thread in a Drop, so the poll in flight finishes while the device is
    still whole.

    Two things I could not check here, on Linux with one GPU: whether a shipped app that drops a
    native surface while the completion thread polls hits the same race (the test binary does the same
    thing at exit), and whether another driver shows it as a quiet read of freed memory rather than a
    segfault.

  2. Hona commented on Nov 23, 2026

    @Hona
    Member

    SurfaceRenderer previously left its rune-video-completion thread detached and dropped its wgpu::Device without stopping or joining the worker, letting an in-flight Device::poll race device and driver teardown. Fixed on dev in #1500 (358329f): SurfaceRenderer now releases its timing queue and stops and joins the completion thread in Drop before releasing the device.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions