Skip to content

Win32Exception ExecuteDbDataReaderAsync #4094

Description

@bmahouachi

During a higher load on the API, several Win32Exception are raised that are not due to the database.

**Exception message:**
Microsoft.Data.SqlClient.SqlException (0x80131904): Execution Timeout Expired.  The timeout period elapsed prior to completion of the operation or the server is not responding.
 ---> System.ComponentModel.Win32Exception (258): Unknown error 258
   at Microsoft.Data.SqlClient.SqlCommand.EndExecuteReaderAsync(IAsyncResult asyncResult)
   at System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endFunction, Action`1 endAction, Task`1 promise, Boolean requiresSynchronization)
ClientConnectionId:6076c28d-cfa1-456a-9749-b293d75316d8
Error Number:-2,State:0,Class:11
**Stack trace:**
Microsoft.Data.SqlClient.SqlException:
   at System.Threading.Tasks.ContinuationResultTaskFromResultTask`2.InnerInvoke (System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
   at System.Threading.ExecutionContext.RunInternal (System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw (System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
   at System.Threading.ExecutionContext.RunInternal (System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
   at System.Threading.Tasks.Task.ExecuteWithThreadLocal (System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw (System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1+ConfiguredTaskAwaiter.GetResult (System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
   at Dapper.SqlMapper+<QueryRowAsync>d__34`1.MoveNext (Dapper, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null: /_/Dapper/SqlMapper.Async.cs:489)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw (System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1+ConfiguredTaskAwaiter.GetResult (System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
   at XX.XX.XX.XX.Infrastructure.XXRepository+<SubmitXXAsync>d__11.MoveNext (XX.XX.XX.XX.Infrastructure, Version=2.2.0.0, Culture=neutral, PublicKeyToken=null: F:\agent01\_work\17\s\src\Core\src\XX.Infrastructure\XXRepository.cs:73)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw (System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult (System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
   at XX.XX.XX.XX.Infrastructure.XXRepository+<SubmitXXAsync>d__13.MoveNext (XX.XX.XX.XX.Infrastructure, Version=2.2.0.0, Culture=neutral, PublicKeyToken=null: F:\agent01\_work\17\s\src\Core\src\XX.Infrastructure\XXRepository.cs:103)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw (System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1+ConfiguredTaskAwaiter.GetResult (System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
   at XX.XX.XX.XX.Services.Commands.XX.SubmitXXCommandHandler+<Handle>d__8.MoveNext (XX.XX.XX.XX.Services.Commands, Version=2.2.0.0, Culture=neutral, PublicKeyToken=null: F:\agent01\_work\17\s\src\Core\src\XX.Services.Commands\XX\XXXCommandHandler.cs:142)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw (System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult (System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
   at XX.XX.XX.XX.WebApp.Controllers.XXController+<CreateXX>d__9.MoveNext (XX.XX.XX.XX.Api, Version=2.2.0.0, Culture=neutral, PublicKeyToken=null: F:\agent01\_work\17\s\src\Core\src\XX.Api\Controllers\XXController.cs:183)
Inner exception System.ComponentModel.Win32Exception handled at System.Threading.Tasks.ContinuationResultTaskFromResultTask`2.InnerInvoke:

Further technical details

The stored procedure response time:

Image

Microsoft.Data.SqlClient version: 6.1.4
.NET target: .NET 8.0
SQL Server version: Azure SQL server
Operating system: Azure linux app service

Additional context
Actions that we have made trying to fix:

Upgrades:
Dapper: from 2.0.123 to 2.1.35
Microsoft.Data.SqlClient: from 5.1.0 to 6.1.4

Infrastructure scale-out: from 8 instances to 12 instances

Min threads:
When analyzing the memory dump:
Image

We decided to set the minthread:
ThreadPool.SetMinThreads(workerThreads: 100, completionPortThreads: 100);

After setting the minthread to 100 it seems to be fixed. But we think that it's not the right fix to do because the lib is doing sync over async which is causing a thread starvation situation and the fix has to be done there. What do you think?

Activity

  1. cheenamalhotra commented on Mar 28, 2026

    @cheenamalhotra
    Member

    Hi @bmahouachi

    Related to #422 and #1530.

  2. moved this from To triage to Backlog in SqlClient Boardon Mar 28, 2026
  3. added
    Performance 📈Issues that are targeted to performance improvements.
    and removed
    Performance 📈Issues that are targeted to performance improvements.
    on Jun 10, 2026
  4. priyankatiwari08 commented on Sep 17, 2026

    @priyankatiwari08
    Contributor

    /triage

  5. github-actions commented on Sep 17, 2026

    @github-actions

    🔍 Triage Summary (on-demand re-triage)

    Check Result
    Issue type Bug
    Environment All required environment details provided for investigation
    Area Area\Async
    Duplicates Potentially related: #422, #1530 (parent, already linked by maintainer)
    Regression Not indicated — issue persisted after upgrading Microsoft.Data.SqlClient from 5.1.0 to 6.1.4, so this is not a new regression but a pre-existing behavior

    Analysis

    Under sustained load, SqlCommand.EndExecuteReaderAsync surfaces a Win32Exception (258) wrapped inside a SqlException "Execution Timeout Expired" that is not caused by the database itself. The reporter's own investigation (thread-pool dump analysis, and the fact that manually raising ThreadPool.SetMinThreads mitigates the symptom) strongly points to thread-pool starvation from sync-over-async code paths inside the async command-execution internals rather than an actual server-side timeout. This matches the pattern already tracked in the parent issue #1530 and related issue #422. Severity: P2 (has a viable workaround via increasing min threads, but indicates an underlying inefficiency in the async execution path affecting scalability under load).

    Next Steps

    • Recommend reviewing Queries with MultipleActiveResultSets=True (MARS) are very slow / time out on Linux #422 and Intermittent Unknown error 258 with no obvious cause #1530 for prior analysis/context before further investigation, since this looks like the same underlying thread-starvation pattern.
    • Assign to Copilot coding agent to investigate the async execution path in SqlCommand (EndExecuteReaderAsync and the underlying TdsParser/network-read continuations) for any remaining sync-over-async or blocking calls that could starve the thread pool under concurrent load, particularly around timeout handling in the async reader completion path.
    • Ask the reporter (@bmahouachi) if they can share a minimal reproducible load-test sample, as a concrete repro would help confirm whether the root cause is in SqlClient's async internals or an interaction with Dapper's async wrapper.

    Note: This triage summary is auto-generated by an AI agent. The analysis and suggestions above have not been verified by a human maintainer. Please treat as preliminary guidance only.

    Generated by SqlClient Issue Auto-Triage for #4094 · copilot · auto · 47.6 AIC · ⌖ 13 AIC · ⊞ 11.9K · ◷

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Area\AsyncPerformance 📈Issues that are targeted to performance improvements.

Type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions