From 89d4c06decf95bfcc0c11d86bb8eb0f9f634cb36 Mon Sep 17 00:00:00 2001 From: Christian Duerr Date: Thu, 20 Aug 2020 18:12:01 +0000 Subject: [PATCH] Fix crash on NetBSD The `_lwp_self` function cannot be used to reliably determine the main thread, see https://github.com/alacritty/alacritty/issues/2631#issuecomment-676723289. It might always be equal to the PID, but it's certainly not always 1 when the thread is the main thread. However, Rust's built in `Thread::id` and `Thread::name` function will always return `ThreadId(1)` and `Some("main")`. Since converting the thread's ID to a number is not supported on stable Rust, checking that the thread is labeled `Some("main")` seems like the most reliable option. It should also be a good fallback in general. --- CHANGELOG.md | 1 + src/platform_impl/linux/mod.rs | 4 +--- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e27cf5ff..2b3b767f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,7 @@ - On macOS, add `NSWindow.hasShadow` support. - **Breaking:** On Web, `set_cursor_position` and `set_cursor_grab` will now always return an error. - **Breaking:** `PixelDelta` scroll events now return a `PhysicalPosition`. +- On NetBSD, fixed crash due to incorrect detection of the main thread. # 0.22.2 (2020-05-16) diff --git a/src/platform_impl/linux/mod.rs b/src/platform_impl/linux/mod.rs index 0fee6832..73b84777 100644 --- a/src/platform_impl/linux/mod.rs +++ b/src/platform_impl/linux/mod.rs @@ -730,7 +730,5 @@ fn is_main_thread() -> bool { #[cfg(target_os = "netbsd")] fn is_main_thread() -> bool { - use libc::_lwp_self; - - unsafe { _lwp_self() == 1 } + std::thread::current().name() == Some("main") }