fix: deadlock on close stereokit

This commit is contained in:
Nova
2023-07-22 07:06:17 -04:00
parent 0d639760e9
commit f0e39195b7
2 changed files with 57 additions and 54 deletions

View File

@@ -17,7 +17,6 @@ use clap::Parser;
use directories::ProjectDirs; use directories::ProjectDirs;
use once_cell::sync::OnceCell; use once_cell::sync::OnceCell;
use stardust_xr::server; use stardust_xr::server;
use std::mem::ManuallyDrop;
use std::path::PathBuf; use std::path::PathBuf;
use std::process::{Command, Stdio}; use std::process::{Command, Stdio};
use std::sync::Arc; use std::sync::Arc;
@@ -189,37 +188,43 @@ fn main() {
let _tokio_handle = event_loop_info.tokio_handle.enter(); let _tokio_handle = event_loop_info.tokio_handle.enter();
#[cfg(feature = "wayland")] #[cfg(feature = "wayland")]
let mut wayland = wayland::Wayland::new().unwrap(); let mut wayland = Some(wayland::Wayland::new().expect("Could not initialize wayland"));
info!("Stardust ready!"); info!("Stardust ready!");
let startup_child = if let Some(project_dirs) = project_dirs.as_ref() { let mut startup_child = if let Some(project_dirs) = project_dirs.as_ref() {
let startup_script_path = cli_args let startup_script_path = cli_args
.startup_script .startup_script
.clone() .clone()
.and_then(|p| p.canonicalize().ok()) .and_then(|p| p.canonicalize().ok())
.unwrap_or_else(|| project_dirs.config_dir().join("startup")); .unwrap_or_else(|| project_dirs.config_dir().join("startup"));
Command::new(startup_script_path) let mut startup_command = Command::new(startup_script_path);
.stdin(Stdio::null())
.env( startup_command.stdin(Stdio::null());
"FLAT_WAYLAND_DISPLAY", startup_command.env(
std::env::var_os("WAYLAND_DISPLAY").unwrap_or_default(), "FLAT_WAYLAND_DISPLAY",
) std::env::var_os("WAYLAND_DISPLAY").unwrap_or_default(),
.env("WAYLAND_DISPLAY", &wayland.socket_name) );
.env("DISPLAY", format!(":{}", wayland.xwayland_state.display)) startup_command.env(
.env( "STARDUST_INSTANCE",
"STARDUST_INSTANCE", event_loop_info
event_loop_info .socket_path
.socket_path .file_name()
.file_name() .expect("Stardust socket path not found"),
.expect("Stardust socket path not found"), );
) #[cfg(feature = "wayland")]
.env("GDK_BACKEND", "wayland") {
.env("QT_QPA_PLATFORM", "wayland") startup_command.env("WAYLAND_DISPLAY", &wayland.as_ref().unwrap().socket_name);
.env("MOZ_ENABLE_WAYLAND", "1") startup_command.env(
.env("CLUTTER_BACKEND", "wayland") "DISPLAY",
.env("SDL_VIDEODRIVER", "wayland") format!(":{}", wayland.as_ref().unwrap().xwayland_state.display),
.spawn() );
.ok() startup_command.env("GDK_BACKEND", "wayland");
startup_command.env("QT_QPA_PLATFORM", "wayland");
startup_command.env("MOZ_ENABLE_WAYLAND", "1");
startup_command.env("CLUTTER_BACKEND", "wayland");
startup_command.env("SDL_VIDEODRIVER", "wayland");
}
startup_command.spawn().ok()
} else { } else {
None None
}; };
@@ -227,14 +232,15 @@ fn main() {
let mut last_frame_delta = Duration::ZERO; let mut last_frame_delta = Duration::ZERO;
let mut sleep_duration = Duration::ZERO; let mut sleep_duration = Duration::ZERO;
debug_span!("StereoKit").in_scope(|| { debug_span!("StereoKit").in_scope(|| {
sk.run( sk.run_stateful(
|sk| { &mut wayland,
move |wayland, _, sk| {
let _span = debug_span!("StereoKit step"); let _span = debug_span!("StereoKit step");
let _span = _span.enter(); let _span = _span.enter();
hmd::frame(sk); hmd::frame(sk);
#[cfg(feature = "wayland")] #[cfg(feature = "wayland")]
wayland.frame_event(sk); wayland.as_mut().unwrap().frame_event(sk);
destroy_queue::clear(); destroy_queue::clear();
if let Some(mouse_pointer) = &mouse_pointer { if let Some(mouse_pointer) = &mouse_pointer {
@@ -264,29 +270,32 @@ fn main() {
); );
#[cfg(feature = "wayland")] #[cfg(feature = "wayland")]
wayland.update(sk); wayland.as_mut().unwrap().update(sk);
drawable::draw(sk); drawable::draw(sk);
audio::update(sk); audio::update(sk);
#[cfg(feature = "wayland")] #[cfg(feature = "wayland")]
wayland.make_context_current(); wayland.as_mut().unwrap().make_context_current();
}, },
|_| { |wayland, _sk| {
info!("Cleanly shut down StereoKit"); info!("Cleanly shut down StereoKit");
if let Some(mut startup_child) = startup_child.take() {
let _ = startup_child.kill();
}
#[cfg(feature = "wayland")]
wayland.take();
}, },
) )
}); });
if let Some(mut startup_child) = startup_child {
let _ = startup_child.kill();
}
let _ = event_stop_tx.send(()); let _ = event_stop_tx.send(());
event_thread event_thread
.join() .join()
.expect("Failed to cleanly shut down event loop") .expect("Failed to cleanly shut down event loop")
.unwrap(); .unwrap();
#[cfg(feature = "wayland")] // #[cfg(feature = "wayland")]
let _wayland = ManuallyDrop::new(wayland); // let _wayland = ManuallyDrop::new(wayland);
info!("Cleanly shut down Stardust"); info!("Cleanly shut down Stardust");
} }

View File

@@ -8,7 +8,7 @@ use once_cell::sync::OnceCell;
use parking_lot::Mutex; use parking_lot::Mutex;
use smithay::{ use smithay::{
reexports::{ reexports::{
calloop::{self, EventLoop, LoopSignal}, calloop::{EventLoop, LoopSignal},
wayland_protocols::xdg::shell::server::xdg_toplevel, wayland_protocols::xdg::shell::server::xdg_toplevel,
wayland_server::{Display, DisplayHandle, Resource, WEnum}, wayland_server::{Display, DisplayHandle, Resource, WEnum},
x11rb::protocol::xproto::Window, x11rb::protocol::xproto::Window,
@@ -21,16 +21,14 @@ use smithay::{
}, },
}; };
use std::{ffi::OsStr, iter::empty, sync::Arc, time::Duration}; use std::{ffi::OsStr, iter::empty, sync::Arc, time::Duration};
use tokio::{sync::oneshot, task::JoinHandle}; use tokio::sync::oneshot;
use tracing::debug; use tracing::debug;
pub static DISPLAY: OnceCell<String> = OnceCell::new(); pub static DISPLAY: OnceCell<String> = OnceCell::new();
pub struct XWaylandState { pub struct XWaylandState {
pub display: u32, pub display: u32,
_xwayland: XWayland, event_loop_signal: LoopSignal,
_event_loop_signal: LoopSignal,
event_loop_join: Option<JoinHandle<Result<(), calloop::Error>>>,
} }
impl XWaylandState { impl XWaylandState {
pub fn create( pub fn create(
@@ -41,7 +39,7 @@ impl XWaylandState {
let (tx, rx) = oneshot::channel(); let (tx, rx) = oneshot::channel();
let event_loop_join = tokio::task::spawn_blocking(move || { tokio::task::spawn_blocking(move || {
let mut event_loop: EventLoop<XWaylandHandler> = EventLoop::try_new()?; let mut event_loop: EventLoop<XWaylandHandler> = EventLoop::try_new()?;
let (xwayland, connection) = XWayland::new(&dh); let (xwayland, connection) = XWayland::new(&dh);
let handle = event_loop.handle(); let handle = event_loop.handle();
@@ -71,9 +69,7 @@ impl XWaylandState {
)?; )?;
let _ = tx.send(XWaylandState { let _ = tx.send(XWaylandState {
display, display,
_xwayland: xwayland, event_loop_signal: event_loop.get_signal(),
_event_loop_signal: event_loop.get_signal(),
event_loop_join: None,
}); });
let wayland_display_handle = wayland_display.lock().handle(); let wayland_display_handle = wayland_display.lock().handle();
let mut handler = XWaylandHandler { let mut handler = XWaylandHandler {
@@ -85,19 +81,17 @@ impl XWaylandState {
event_loop.run(Duration::from_millis(100), &mut handler, |_| ()) event_loop.run(Duration::from_millis(100), &mut handler, |_| ())
}); });
let mut state = rx.blocking_recv()?; let state = rx.blocking_recv()?;
state.event_loop_join.replace(event_loop_join);
let _ = DISPLAY.set(format!(":{}", state.display)); let _ = DISPLAY.set(format!(":{}", state.display));
Ok(state) Ok(state)
} }
} }
// impl Drop for XWaylandState { impl Drop for XWaylandState {
// fn drop(&mut self) { fn drop(&mut self) {
// self.xwayland.shutdown(); self.event_loop_signal.stop();
// self.event_loop_signal.stop(); }
// } }
// }
struct XWaylandHandler { struct XWaylandHandler {
wayland_display: Arc<Mutex<Display<WaylandState>>>, wayland_display: Arc<Mutex<Display<WaylandState>>>,