Skip to content

Fix MacMainThreadScheduler crash when ManagedThreadId != 1 (#646)#649

Open
embetten wants to merge 9 commits intomasterfrom
users/embetten/main-thread-bug
Open

Fix MacMainThreadScheduler crash when ManagedThreadId != 1 (#646)#649
embetten wants to merge 9 commits intomasterfrom
users/embetten/main-thread-bug

Conversation

@embetten
Copy link
Contributor

@embetten embetten commented Feb 25, 2026

Fixes #646 — MacMainThreadScheduler.StartMessageLoop() throws "Message loop must be started on the main thread" when Environment.CurrentManagedThreadId is not 1, which can happen when launched via dotnet tool run or dotnet run where internal runtime threads consume thread ID 1 before Main() executes.

MSAL's MacMainThreadScheduler assumes ManagedThreadId == 1 means "main thread." This is a .NET runtime implementation detail, not a guarantee. The dotnet tool shim and certain hosting configurations cause Main() to run on a thread where this check fails.

This change proposes falling back to no longer use the MacMainThreadScheduler when the thread != 1. Windows and Linux brokers will be unaffected, but mac broker will become unavailable and fall back to browser or DCF.

@embetten
Copy link
Contributor Author

validated on linux-x64, win x64, arm64. Need to finialze mac testing

@embetten embetten marked this pull request as ready for review March 4, 2026 23:58
@embetten
Copy link
Contributor Author

embetten commented Mar 4, 2026

finished mac testing

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.

[Bug]: Throws "Message loop must be started on the main thread" at "MacMainThreadScheduler.StartMessageLoop()"

1 participant