Fix macOS broker deadlock by processing CFRunLoop in MacMainThreadScheduler#5966
Open
embetten wants to merge 3 commits intoAzureAD:mainfrom
Open
Fix macOS broker deadlock by processing CFRunLoop in MacMainThreadScheduler#5966embetten wants to merge 3 commits intoAzureAD:mainfrom
embetten wants to merge 3 commits intoAzureAD:mainfrom
Conversation
…eduler On macOS, the native MSAL broker runtime may use dispatch_sync to the main GCD queue (e.g. during FallbackToNativeMsal on devices without an SSO extension). The existing Thread.Sleep in the message loop prevents the main thread from servicing the GCD queue, causing a deadlock. Replace Thread.Sleep with CFRunLoopRunInMode on macOS so dispatch_sync calls targeting the main queue can execute while the scheduler waits for new work.
Contributor
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
Fixes a macOS deadlock in MacMainThreadScheduler.StartMessageLoop() by pumping the CoreFoundation run loop (servicing GCD main-queue dispatch_sync) instead of using Thread.Sleep when running on macOS.
Changes:
- Added macOS CoreFoundation P/Invoke bindings in a nested
CoreFoundationInteropclass. - Replaced
Thread.SleepwithCFRunLoopRunInModeon macOS while preservingThread.Sleepelsewhere.
Comment on lines
+63
to
+73
| private static IntPtr s_defaultMode; | ||
|
|
||
| private static IntPtr GetDefaultMode() | ||
| { | ||
| if (s_defaultMode == IntPtr.Zero) | ||
| { | ||
| s_defaultMode = CFStringCreateWithCString( | ||
| IntPtr.Zero, "kCFRunLoopDefaultMode", KCFStringEncodingUTF8); | ||
| } | ||
|
|
||
| return s_defaultMode; |
Author
|
@copilot apply changes based on the comments in this thread |
Contributor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
On macOS, MacMainThreadScheduler.StartMessageLoop() uses Thread.Sleep to wait between processing queued actions. However, the native MSAL broker runtime may use dispatch_sync to the main GCD queue (e.g. during FallbackToNativeMsal on devices without an SSO extension). Because Thread.Sleep does not service the GCD queue, dispatch_sync blocks forever waiting for the main thread — resulting in a deadlock.
This PR replaces Thread.Sleep with CFRunLoopRunInMode on macOS, which processes pending GCD dispatch calls while waiting. On non-macOS platforms, the existing Thread.Sleep behavior is preserved.
Fixes #
#5940
Changes proposed in this request
Testing
Performance impact
CFRunLoopRunInMode with the same sleep interval behaves equivalently to Thread.Sleep for timing purposes, while additionally servicing the run loop. No measurable performance difference expected.
Documentation
I am not aware of any related documentation