From da49e71b9d6a211edc6d55ae1864e2df71ef41ef Mon Sep 17 00:00:00 2001 From: VallierDev <24893073+VallierDev@users.noreply.github.com> Date: Sat, 17 Oct 2026 14:19:49 +0800 Subject: [PATCH] fix(x11): keep the shared display open until the last capture is dropped The X11 engine has one process-wide display connection, but every ScreenCapture closed it on drop. During guest assistance the device init's screen-size probe created and dropped a capture after the main one, closing the display under it: the stream stopped with "X11 display wake failed" and the viewer got no video. A capture replaced after a resize had the same hazard. Count live X11 captures and close only on the last drop. Co-Authored-By: Claude Opus 5.5 --- src/capture.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/capture.rs b/src/capture.rs index 8386fc7..6f42712 100644 --- a/src/capture.rs +++ b/src/capture.rs @@ -108,6 +108,7 @@ impl ScreenCapture { if display_ptr.is_null() { return Err("Capture display name unavailable".into()); } let display = unsafe { std::ffi::CStr::from_ptr(display_ptr) }.to_string_lossy().into_owned(); log::info!("Selected capture display: {}", display); + X11_USERS.fetch_add(1, std::sync::atomic::Ordering::AcqRel); Ok(Self { backend, init_error:None, @@ -150,9 +151,15 @@ impl ScreenCapture { } } +/// The X11 engine keeps one process-wide display connection shared by every ScreenCapture. +/// Only the last one may close it: a short-lived capture (device init's screen size during guest +/// assistance, or the replaced capture after a resize) used to close it under the live stream. +static X11_USERS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0); + impl Drop for ScreenCapture { fn drop(&mut self) { - if self.init_error.is_none() && self.backend==crate::desktop_backend::DesktopBackend::X11 { unsafe { x11_close() }; } + if self.init_error.is_none() && self.backend==crate::desktop_backend::DesktopBackend::X11 + && X11_USERS.fetch_sub(1, std::sync::atomic::Ordering::AcqRel) == 1 { unsafe { x11_close() }; } } }