Skip to content

Fix GCD deadlock on sync queue draw#113

Merged
jdtsmith merged 1 commit into
emacs-mac-30_1_expfrom
gcd-sync-no-deadlock
Nov 17, 2025
Merged

Fix GCD deadlock on sync queue draw#113
jdtsmith merged 1 commit into
emacs-mac-30_1_expfrom
gcd-sync-no-deadlock

Conversation

@jdtsmith

@jdtsmith jdtsmith commented Oct 25, 2025

Copy link
Copy Markdown
Owner

TLDR: A queue attempting to flush itself from the "inside" with a call to dispatch_sync deadlocks, so don't do that.

Another attempt to fix the dreaded (but rare) GCD draw deadlock, that often (but not always) occurs on waking from sleep. See #78.

Here we assume the problem arises from a serial queue deadlock which occurs when a block from the queue running on the main GUI thread attempts to use dispatch_sync on the same queue.

So an asynchronous block on the global_focus_drawing_queue tries to "flush" the queue, using dispatch_sync to quickly wait for it to complete all its submitted blocks. But if this happens on the main thread, dispatch_sync is waiting for the queue to drain, and the currently executing block on the selfsame queue is waiting for dispatch_sync to complete: deadlock!

Apparently this isn't a modern approach to drain GCD queues, but I kept it intact, and simply do nothing to sync if we are calling in to the same queue.

See also #86 for another (failed) attempt.

  • src/macappkit.m (set_global_focus_view_frame): set identifier key on
    drawing queue for easy identification.
    (mac_draw_queue_sync): Do not dispatch empty block if already running
    from drawing queue.

* src/macappkit.m (set_global_focus_view_frame): set identifier key on
drawing queue for easy identification.
(mac_draw_queue_sync): Do not dispatch empty block if already running
from drawing queue.
@jdtsmith

Copy link
Copy Markdown
Owner Author

I have had no recurrences of the issue in #78, so I am again cautiously optimistic that this is the right fix.

@jdtsmith jdtsmith merged commit 32451b7 into emacs-mac-30_1_exp Nov 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant