From bcc34ee7b68c69dc7cdbc2f0654eb3f9dcd27d65 Mon Sep 17 00:00:00 2001 From: Edwin Wu Date: Mon, 28 Jul 2025 18:19:53 -0700 Subject: [PATCH] Make timer queue run on background queue PiperOrigin-RevId: 788236523 --- internal/platform/implementation/apple/timer.mm | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/internal/platform/implementation/apple/timer.mm b/internal/platform/implementation/apple/timer.mm index e638c1cf..86e68c7f 100644 --- a/internal/platform/implementation/apple/timer.mm +++ b/internal/platform/implementation/apple/timer.mm @@ -50,8 +50,10 @@ bool Timer::Create(int delay, int interval, absl::AnyInvocable callback) uint64_t intervalInNanoseconds = interval == 0 ? DISPATCH_TIME_FOREVER : interval * NSEC_PER_MSEC; callback_ = std::move(callback); + // Run the timer on a background queue to avoid deadlocking the main thread, + // which may be blocked waiting for a future to complete. timer_ = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, /*handle=*/0, /*mask=*/0, - dispatch_get_main_queue()); + dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)); dispatch_source_set_event_handler(timer_, ^{ absl::AnyInvocable callback_to_run = nullptr;