From 33bbb0520650a03309a218e28fcc87b0be401f0f Mon Sep 17 00:00:00 2001 From: Kyle Sunden Date: Mon, 18 Dec 2023 18:37:06 -0600 Subject: [PATCH] Backport PR #27527: FIX: Add macos timers to the main thread --- src/_macosx.m | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/_macosx.m b/src/_macosx.m index d39f37063c9e..6be86cfe7367 100755 --- a/src/_macosx.m +++ b/src/_macosx.m @@ -1723,11 +1723,15 @@ - (void)flagsChanged:(NSEvent *)event } // hold a reference to the timer so we can invalidate/stop it later - self->timer = [NSTimer scheduledTimerWithTimeInterval: interval - repeats: !single - block: ^(NSTimer *timer) { + self->timer = [NSTimer timerWithTimeInterval: interval + repeats: !single + block: ^(NSTimer *timer) { gil_call_method((PyObject*)self, "_on_timer"); }]; + // Schedule the timer on the main run loop which is needed + // when updating the UI from a background thread + [[NSRunLoop mainRunLoop] addTimer: self->timer forMode: NSRunLoopCommonModes]; + exit: Py_XDECREF(py_interval); Py_XDECREF(py_single);