From e315dd07cab777b851ce15548e61ba6a79ff2e17 Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Sun, 15 Nov 2026 08:53:00 -0800 Subject: [PATCH] Fix a race condition with the wake window and main callbacks --- src/events/SDL_events.c | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/events/SDL_events.c b/src/events/SDL_events.c index b9dbe0abb4..5adf6ae4dd 100644 --- a/src/events/SDL_events.c +++ b/src/events/SDL_events.c @@ -1649,6 +1649,19 @@ static Sint64 SDL_events_get_polling_interval(void) return poll_intervalNS; } +static bool SDL_HasMainThreadCallbacks(void) +{ + bool result = false; + + SDL_LockMutex(SDL_main_callbacks_lock); + if (SDL_main_callbacks_head) { + result = true; + } + SDL_UnlockMutex(SDL_main_callbacks_lock); + + return result; +} + static int SDL_WaitEventTimeout_Device(SDL_VideoDevice *_this, SDL_Window *wakeup_window, SDL_Event *event, Uint64 start, Sint64 timeoutNS) { Sint64 loop_timeoutNS = timeoutNS; @@ -1690,9 +1703,10 @@ static int SDL_WaitEventTimeout_Device(SDL_VideoDevice *_this, SDL_Window *wakeu } } SDL_SetAtomicPointer(&_this->wakeup_window, wakeup_window); - /* Check the queue before waiting: another thread might have - added an event (right before wakeup_window was set: race condition)*/ - if (SDL_PeepEvents(NULL, 0, SDL_PEEKEVENT, SDL_EVENT_FIRST, SDL_EVENT_LAST) > 0) { + // Check again before waiting: another thread might have added an event + // or a main thread callback before wakeup_window was set + if (SDL_PeepEvents(NULL, 0, SDL_PEEKEVENT, SDL_EVENT_FIRST, SDL_EVENT_LAST) > 0 || + SDL_HasMainThreadCallbacks()) { SDL_SetAtomicPointer(&_this->wakeup_window, NULL); continue; }