From 72ffcb5733b2e69c484840c0868690247eb744d6 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 20 Oct 2026 09:11:32 +0530 Subject: [PATCH] macOS: Fix a possible use-after-free of the tick lock when quitting The I/O and talk threads can call glfwPostEmptyEvent() until Boss.destroy() joins them, which is after the main loop has returned and released the NSLock, so they could message a freed lock. The lock only guarded a single flag, so replace it with an atomic exchange. This removes the lifetime problem entirely, is cheaper than locking on every wakeup, also fixes the unsynchronized access of the flag from the main thread and no longer drops wakeups posted before the main loop starts. Closes #10705 --- glfw/cocoa_init.m | 50 +++++++++++++---------------------------------- 1 file changed, 14 insertions(+), 36 deletions(-) diff --git a/glfw/cocoa_init.m b/glfw/cocoa_init.m index df20121cc..eb3680225 100644 --- a/glfw/cocoa_init.m +++ b/glfw/cocoa_init.m @@ -30,7 +30,7 @@ #include "../kitty/monotonic.h" #include // For MAXPATHLEN #include -#include +#include // Needed for _NSGetProgname #include @@ -1051,45 +1051,24 @@ _glfwPlatformGetVersionString(void) { static GLFWtickcallback tick_callback = NULL; static void *tick_callback_data = NULL; -static bool tick_callback_requested = false; -static pthread_t main_thread; -static NSLock *tick_lock = NULL; +// Atomic rather than lock protected, as it can be set from other threads at +// any time, including after the main loop has returned and before they are joined. +static atomic_bool tick_callback_requested = false; +static void +queue_tick_callback(void) { + [NSApp performSelectorOnMainThread:@selector(tick_callback) withObject:nil waitUntilDone:NO]; +} void _glfwDispatchTickCallback(void) { - if (tick_lock && tick_callback) { - while (true) { - bool do_call = false; - [tick_lock lock]; - if (tick_callback_requested) { - do_call = true; - tick_callback_requested = false; - } - [tick_lock unlock]; - if (do_call) tick_callback(tick_callback_data); - else break; - } - } -} - -static void -request_tick_callback(void) { - if (!tick_callback_requested) { - tick_callback_requested = true; - [NSApp performSelectorOnMainThread:@selector(tick_callback) withObject:nil waitUntilDone:NO]; - } + if (!tick_callback) return; + while (atomic_exchange(&tick_callback_requested, false)) tick_callback(tick_callback_data); } void _glfwPlatformPostEmptyEvent(void) { - if (pthread_equal(pthread_self(), main_thread)) { - request_tick_callback(); - } else if (tick_lock) { - [tick_lock lock]; - request_tick_callback(); - [tick_lock unlock]; - } + if (!atomic_exchange(&tick_callback_requested, true)) queue_tick_callback(); } @@ -1101,13 +1080,12 @@ _glfwPlatformStopMainLoop(void) { void _glfwPlatformRunMainLoop(GLFWtickcallback callback, void *data) { - main_thread = pthread_self(); tick_callback = callback; tick_callback_data = data; - tick_lock = [NSLock new]; + // A request made while there was no main loop may have had its selector + // consumed without dispatch, so queue it again + if (atomic_load(&tick_callback_requested)) queue_tick_callback(); [NSApp run]; - [tick_lock release]; - tick_lock = NULL; tick_callback = NULL; tick_callback_data = NULL; }