Repository navigation
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 doesWedges 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.
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsBacklog
Activity
github-actions commentedon Aug 12, 2026
🔍 Triage Summary
Area\Documentation(primary request);Area\Netfx(secondary — sync-over-async hazard specific to .NET Framework STA threading)Analysis
SqlCommand.ExecuteReader(sync) usesAsyncHelper.WaitForCompletion→Task.Wait, which on an STA thread becomes a COM pumping wait that dispatches COM calls but not posted window messages. Independently,OpenAsyncRetry.RetryfiresDbConnection.StateChangeon a thread-pool thread rather than the caller's thread. When aStateChangehandler does a blocking marshal back to the UI thread (Control.Invoke), both threads wait on each other permanently. The reporter explicitly acknowledges the root cause is in the SSMS consumer, but theStateChange-on-threadpool-thread behaviour is undocumented, and #1213 noted this residual sync-over-async risk. Severity P2 — impacts .NET Framework WinForms/WPF consumers using synchronous SqlClient APIs on the UI thread.Next Steps
SqlConnection.StateChangeXML docs and the connection-pooling docs clarifying that the event may be raised on a thread-pool thread during the async-retry open path, and that handlers must not block (e.g., must not callControl.InvokeorDispatcher.Invokesynchronously).StateChange/OnStateChangecall sites inSqlConnectionFactory.SetInnerConnectionEventand related connection-pool internals, (b) update XML doc comments onSqlConnection.StateChangewith a threading note, and (c) updatedoc/connection-pooling documentation with the same warning.OpenAsyncRetryshould marshalStateChangeback to the original synchronization context (if captured), or document explicitly why it does not. The reporter has also raised whetherRunExecuteReaderTdscallingWaitForCompletionon an STA thread should be guarded — this is the broader sync-over-async hazard flagged in Fix Async thread blocking on SqlConnection open for AAD modes #1213 and warrants separate consideration.mdaigle commentedon Sep 18, 2026
We'll investigate how to correct this as part of #3459
UI Thread (STA) ThreadPool Thread ──────────────── ────────────────── SqlCommand.ExecuteReader() └─ RunExecuteReaderTds() └─ _activeConnection.ValidateAndReconnect() │ connection needs (re)open ⇒ kicks off │ async reconnect/open task ───────────┐ ▼ ▼ AsyncHelper.WaitForCompletion(reconnectTask) SqlConnection.OpenAsyncRetry.Retry └─ Task.Wait() ◄─────────┐ └─ TryOpen / TryOpenInner │ │ └─ DbConnection.OnStateChange │ STA ⇒ COM-pump │ └─ consumer's StateChange handler │ wait: services │ └─ Control.Invoke() ───┐ │ COM + *sent* msgs │ (blocking marshal │ │ only, NOT *posted* │ back to UI thread) │ │ window messages │ │ ▼ │ │ ╔═══════════════╗ │ │ ║ BLOCKED ║◄─────────┘ reconnectTask can't complete until StateChange │ ║ waiting on ║ handler returns │ ║ reconnectTask ║ │ ╚═══════════════╝ │ ▲ │ │ Invoke posts a message to the UI thread and blocks until dispatched — │ │ but the UI thread only pumps COM/sent messages while inside Task.Wait, │ │ so the posted message is never serviced │ └────────────────────────────────────────────────────────────────────────────────┘ DEADLOCK (circular wait): ExecuteReader waits on reconnectTask ⇄ reconnectTask waits on StateChange handler's Invoke ⇄ Invoke waits on the UI thread's message pump (which ExecuteReader is occupying)