From 01a224425d2a90e660bbfffe8d1cbc6b93da24bd Mon Sep 17 00:00:00 2001 From: Mason Reed Date: Mon, 27 Jan 2025 16:56:15 -0500 Subject: Fix some rust race conditions and comment some likely suspects FYI The binary view saving stuff can deadlock right now, so we document that now :/ --- rust/src/headless.rs | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) (limited to 'rust/src/headless.rs') diff --git a/rust/src/headless.rs b/rust/src/headless.rs index 2642b2b0..78c04a96 100644 --- a/rust/src/headless.rs +++ b/rust/src/headless.rs @@ -148,26 +148,26 @@ impl Default for InitializationOptions { /// This initializes the core with the given [`InitializationOptions`]. pub fn init_with_opts(options: InitializationOptions) -> Result<(), InitializationError> { // If we are the main thread that means there is no main thread, we should register a main thread handler. - if options.register_main_thread_handler - && is_main_thread() - && MAIN_THREAD_HANDLE.lock().unwrap().is_none() - { - let (sender, receiver) = std::sync::mpsc::channel(); - let main_thread = HeadlessMainThreadSender::new(sender); - - // This thread will act as our main thread. - let main_thread_handle = std::thread::Builder::new() - .name("HeadlessMainThread".to_string()) - .spawn(move || { - // We must register the main thread within said thread. - main_thread.register(); - while let Ok(action) = receiver.recv() { - action.execute(); - } - })?; - - // Set the static MAIN_THREAD_HANDLER so that we can close the thread on shutdown. - *MAIN_THREAD_HANDLE.lock().unwrap() = Some(main_thread_handle); + if options.register_main_thread_handler && is_main_thread() { + let mut main_thread_handle = MAIN_THREAD_HANDLE.lock().unwrap(); + if main_thread_handle.is_none() { + let (sender, receiver) = std::sync::mpsc::channel(); + let main_thread = HeadlessMainThreadSender::new(sender); + + // This thread will act as our main thread. + let join_handle = std::thread::Builder::new() + .name("HeadlessMainThread".to_string()) + .spawn(move || { + // We must register the main thread within said thread. + main_thread.register(); + while let Ok(action) = receiver.recv() { + action.execute(); + } + })?; + + // Set the static MAIN_THREAD_HANDLER so that we can close the thread on shutdown. + *main_thread_handle = Some(join_handle); + } } match crate::product().as_str() { -- cgit v1.3.1