Sync ExecuteReader on an STA thread deadlocks against a StateChange handler that marshals to the UI thread (OpenAsyncRetry path)
@mdaigle is already working on this.
Since Sep 18, 2026.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 64/100
- Issue type
- Documentation
- Clarity
- Mostly clear
- Activity status
- Quiet
- Domain
- databases, documentation
Research direction
Locate the SqlConnection.StateChange documentation and the connection-pooling documentation mentioned in the issue; use the OpenAsyncRetry.Retry behavior and the minimal WinForms repro as context. Done means the documentation clearly warns that StateChange handlers may run on a thread-pool thread during async retry and must not block while marshaling to a UI thread.
Written by the indexing model from the issue text.
Description
Summary
This is not a claim that SqlClient is at fault. The blocking call is in a closed-source consumer (SSMS). I am raising it here because the deadlock is only reachable through two SqlClient behaviours, one of which was explicitly flagged as a residual risk in #1213, and because a note in the docs would probably prevent the next occurrence of it.
Two SqlClient behaviours combine:
- Synchronous
SqlCommand.ExecuteReaderfunnels intoAsyncHelper.WaitForCompletion→Task.Wait. On an STA thread that becomes a COM pumping wait, which dispatches COM calls and sent window messages but not posted ones. SqlConnection.OpenAsyncRetry.RetryraisesDbConnection.StateChangeon a thread-pool thread, not on the thread that initiated the open.
If a consumer's StateChange handler marshals to the UI thread with a blocking call (Control.Invoke, Dispatcher.Invoke), and that UI thread is simultaneously inside a synchronous SqlClient call, the two deadlock permanently. Neither behaviour is a bug on its own.
Versions
Microsoft.Data.SqlClient6.1.5 (file 6.15.26114.3)- .NET Framework 4.8.9324.0, x64, STA
- Observed in SQL Server Management Studio 22.8.12023.21 against Azure SQL Database with Microsoft Entra authentication
Observed stacks
Captured with ClrMD against the live wedged process.
UI thread (STA):
Microsoft.Data.SqlClient.SqlCommand.ExecuteReader
Microsoft.Data.SqlClient.SqlCommand.RunExecuteReaderTds
Microsoft.Data.SqlClient.AsyncHelper.WaitForCompletion
System.Threading.Tasks.Task.Wait
System.Threading.ManualResetEventSlim.Wait
System.Threading.Monitor.ObjWait
System.Threading.SynchronizationContext.WaitHelper
Thread-pool worker:
ThreadPoolWorkQueue.Dispatch
Task.Execute
Microsoft.Data.SqlClient.SqlConnection+OpenAsyncRetry.Retry
Microsoft.Data.SqlClient.SqlConnection.TryOpen
Microsoft.Data.SqlClient.SqlConnection.TryOpenInner
Microsoft.Data.ProviderBase.DbConnectionClosedConnecting.TryOpenConnection
Microsoft.Data.SqlClient.SqlConnectionFactory.SetInnerConnectionEvent
System.Data.Common.DbConnection.OnStateChange
<consumer's StateChange handler>
System.Windows.Forms.Control.Invoke <-- blocking marshal to the UI thread
System.Windows.Forms.Control.WaitForWaitHandle
System.Threading.WaitHandle.WaitOne
The query is never sent. Server-side the sessions show cpu_time 0 and last_request_end_time equal to last_request_start_time.
Minimal repro
.NET Framework 4.8 WinForms, [STAThread], Microsoft.Data.SqlClient 6.1.5. On a button click:
var completion = new TaskCompletionSource<bool>();
Task.Run(() =>
{
Thread.Sleep(1500); // let the UI thread enter its wait first
Invoke((Action)(() => { })); // blocking marshal, as a StateChange handler would do
completion.SetResult(true);
});
completion.Task.Wait(TimeSpan.FromMinutes(3)); // as AsyncHelper.WaitForCompletion does
Wedges every time and stays wedged. Swapping the single Invoke for BeginInvoke completes in ~1.5s with no other change and no external stimulus.
Worth noting for anyone debugging something similar: while wedged, IsHungAppWindow returns false and the process sits near 0% CPU, because the COM wait is still dispatching. It does not look like a hang from outside.
Relationship to #1213
#1213 fixed the token-acquisition leg of this same family by wrapping AcquireTokenAsync in
Task.Run to escape the WinForms SynchronizationContext. In review, @roji noted:
Changing Task.Run should indeed resolve the Winforms deadlock, since the code runs without the Winforms SynchronizationContext.
and also:
This is still sync-over-async and therefore strongly discouraged, since it can cause various starvation/pseudo-deadlock effects
This looks like a concrete instance of that residual risk in the wild, four majors later, reached through the StateChange callback rather than through token acquisition.
What I am actually asking for
Not necessarily a code change. In rough order of value:
- Document the hazard.
DbConnection.StateChangebeing raised on a thread-pool thread during the async-retry path is not obvious, and it means handlers must never block. A note onSqlConnection.StateChangeor in the connection-pooling docs would be cheap and would have saved this investigation. - Route it internally if you can. The actual blocking call is in
Microsoft.SqlServer.Management.DataTools(SSMS, closed source), which is in the same org. It is also reported at Developer Community 10857872, "SSMS Studio is busy after timeout", where it has been open and under investigation for a couple of years. The fix looks like one word:Invoke→BeginInvoke. - Optional, and your call entirely: whether
RunExecuteReaderTdsshould be able to reachWaitForCompletionat all when called synchronously on an STA thread, given the known hazard.
Happy to attach the full repro project and raw stack dumps.
- Dominant language
- C#
- Stars
- 990
- Forks
- 340
- Avg merge
- 3d 1h
- Merged PRs (30d)
- 62
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from dotnet/SqlClient
-
:new: Triage Needed Area\Engineering Performance :chart_with_upwards_trend:
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
-
Area\Tests
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
-
Code Health :pill: Good First Issue :sparkles:
Difficulty 1/5 Under an hour Newbie friendliness 76/100
-
Difficulty 4/5 3-5 days Newbie friendliness 48/100
-
Difficulty 5/5 Over a week Newbie friendliness 30/100
All issues in dotnet/SqlClient
Similar issues
-
type/automation type/tech-debt
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
-
t/bug
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
-
ci-failure-cause test-failure
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
-
area:auth FE mvp P3
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
klasolsson81/jobbliggaren#1788 ·