[Bug] Deadlock during RuntimeBroker.ProcessExit causing desktop application to hang while closing #5194
Labels
needs attention
Delete label after triage
public-client
untriaged
Do not delete. Needed for Automation
Uh oh!
There was an error while loading. Please reload this page.
Library version used
4.60.4
.NET version
.NET SDK 8.0.310
Scenario
PublicClient - desktop app
Is this a new or an existing app?
The app is in production, I haven't upgraded MSAL, but started seeing this issue
Issue description and reproduction steps
If my application invokes
GetAccountsAsync
on my public client application while the application is in the process of closing, a deadlock can be observed on MSAL threads that indefinitely prevent the app from shutting down.When this scenario happens, the following threads can be observed in a debugger:
Thread A (name:
msalruntime.dll!thread_start<unsigned int (__cdecl*)(void *),1>
):Thread B (name:
.NET Finalizer
, id:3968
):Notice that thread A is waiting on a lock held by thread B. Thread B is blocking my application from shutting down presumably because
OnProcessExit
is an event handler forProcess.Exited
that needs to return before the process exit routine completes. It's not clear from thread B's callstack, but my hypothesis is that theCancelBackgroundRequests
is waiting for thread A (a background request on a separate thread) to return.Verbose MSAL logs: msal_logs.txt
I found this comment on an abandoned pull request that may be related to this scenario.
Expected behavior
I would expect
GetAccountsAsync
to return an empty collection if it is unable to retrieve accounts due to MSAL being shut down.Identity provider
Microsoft Entra ID (Work and School accounts and Personal Microsoft accounts)
Regression
No response
Solution and workarounds
Thread A above should be provided a cancellation token to cancel the background operation when a shutdown is happening. The thread should cleanly exit when the token is cancelled, which would allow the process to exit. If applicable, an overloaded
GetAccountAsync
method that accepts a cancellation token to use could be available as well.I cannot think of any workaround besides simply ensuring my application does not invoke
GetAccountsAsync
at the wrong time. For some applications, this might be difficult to entirely prevent.The text was updated successfully, but these errors were encountered: