Skip to content

7.1.0 regression: in-flight OpenAsync fails instantly with pool "Timeout expired" when ClearAllPools()/ClearPool() runs #4714

Closed
1 of 1 issue completed

Description

@DanielBryars

Describe the bug

In 7.1.0, with the default pool (WaitHandleDbConnectionPool, no AppContext switches set), an OpenAsync that is in flight when its pool is shut down faults immediately with:

System.InvalidOperationException: Timeout expired.  The timeout period elapsed prior to obtaining a connection from the pool.  This may have occurred because all pooled connections were in use and max pool size was reached.
   at Microsoft.Data.Common.ADP.ExceptionWithStackTrace(Exception e)

The open has waited ~0 ms (Connect Timeout = 30 s) and no pool is anywhere near Max Pool Size. On 7.0.2 and 7.0.3 the same open completes successfully.

Any SqlConnection.ClearAllPools() anywhere in the process triggers it, because ClearAllPools → DbConnectionPoolGroup.Clear → SqlConnectionFactory.QueuePoolForRelease → pool.Shutdown() runs for every pool. EF Core's EnsureDeleted() calls ClearAllPools(), so any test suite that creates a database per test hits it constantly. DbConnectionPoolGroup.Prune() takes the same QueuePoolForRelease → Shutdown() path for a drained idle pool, so (by code reading — not reproduced) production apps are exposed on the first open after an idle period.

Cause

#4302 ("…harden WaitHandleDbConnectionPool shutdown") added this to the private TryGetConnection, straight after WaitHandle.WaitAny:

if (State is not Running)
{
    ...
    Interlocked.Decrement(ref _waitCount);
    connection = null;
    return false;
}

WaitForPendingOpen interprets false as a timeout:

timeout = !TryGetConnection(next.Owner, delay, allowCreate: true, onlyOneCheckConnection: false, next.Timeout, out connection);
...
else if (timeout)
{
    next.Completion.TrySetException(ADP.ExceptionWithStackTrace(ADP.PooledOpenTimeout()));
}

The sync path survives the same race: SqlConnectionFactory.TryGetConnection sees connection is null with !connectionPool.IsRunning, sleeps and retries against the replacement pool ("We've hit the race condition, where the pool was shut down after we got it from the group"). The async pending-open path has no equivalent, so "pool shut down" surfaces as a pool timeout. Before #4302 there was no State check after WaitAny, and the in-flight request simply completed.

#4302's description says "Existing pooling behavior is unchanged until Shutdown() is invoked" — but Shutdown() is invoked by every ClearPool/ClearAllPools and by pruning.

To reproduce

16 tasks loop OpenAsync/dispose, each on its own pool, while one task calls ClearAllPools() every 5 ms. REPRO_CS = any SQL Server connection string.

using System.Collections.Concurrent;
using System.Diagnostics;
using Microsoft.Data.SqlClient;

// Repro: an async Open that is in flight on a pool when ANY SqlConnection.ClearAllPools() runs.
// Each "worker" opens and closes a connection on its own pool; a "clearer" calls ClearAllPools(), exactly like
// EF Core's EnsureDeleted(). No pool is ever near Max Pool Size.
var baseCs = Environment.GetEnvironmentVariable("REPRO_CS") ?? throw new InvalidOperationException("set REPRO_CS");
int seconds = int.Parse(args.Length > 0 ? args[0] : "20");
int workers = int.Parse(args.Length > 1 ? args[1] : "16");
bool clear = !(args.Length > 2 && args[2] == "noclear");

var asm = typeof(SqlConnection).Assembly;
Console.WriteLine($"Microsoft.Data.SqlClient {FileVersionInfo.GetVersionInfo(asm.Location).ProductVersion?.Split('+')[0]}  ({workers} workers, {seconds}s, ClearAllPools={(clear ? "on" : "OFF")})");

using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(seconds));
long opens = 0, clears = 0;
var errors = new ConcurrentDictionary<string, int>();
var elapsedOfFailures = new ConcurrentBag<long>();

var workerTasks = Enumerable.Range(0, workers).Select(i => Task.Run(async () =>
{
    // One pool per worker, reused: nothing leaks, so a run with clearing OFF is a fair control.
    var cs = new SqlConnectionStringBuilder(baseCs) { ApplicationName = "repro-worker-" + i }.ConnectionString;
    while (!cts.IsCancellationRequested)
    {
        var sw = Stopwatch.StartNew();
        try
        {
            await using var c = new SqlConnection(cs);
            await c.OpenAsync();
            Interlocked.Increment(ref opens);
        }
        catch (Exception e)
        {
            elapsedOfFailures.Add(sw.ElapsedMilliseconds);
            var top = e.StackTrace?.Split('\n').FirstOrDefault()?.Trim() ?? "";
            errors.AddOrUpdate($"{e.GetType().Name}: {e.Message.Split('.')[0]} | top frame: {top}", 1, (_, n) => n + 1);
        }
    }
})).ToArray();

var clearer = Task.Run(async () =>
{
    while (clear && !cts.IsCancellationRequested)
    {
        SqlConnection.ClearAllPools();
        Interlocked.Increment(ref clears);
        await Task.Delay(5);
    }
});

await Task.WhenAll(workerTasks.Append(clearer));
Console.WriteLine($"opens ok={opens}  ClearAllPools calls={clears}  FAILED opens={errors.Values.Sum()}");
foreach (var kv in errors.OrderByDescending(k => k.Value)) Console.WriteLine($"  x{kv.Value}  {kv.Key}");
if (!elapsedOfFailures.IsEmpty)
{
    var ms = elapsedOfFailures.OrderBy(x => x).ToArray();
    Console.WriteLine($"  failed-open latency ms: min={ms[0]} median={ms[ms.Length / 2]} max={ms[^1]}   (Connect Timeout is 30000)");
}

Results (SQL Server 2022 in Docker, .NET 10, Windows 11, 10–15 s runs):

Version ClearAllPools Successful opens Failed opens Failure latency
7.0.2 on 8,404 0 —
7.0.3 on 397,362 0 —
7.1.0 on 7,051 / 666,333 / 810,677 187 / 1,154 / 1,684 min 0, median 0, max 44 ms
7.0.2 off 53,412,474 0 —
7.1.0 off 46,728,760 0 —

(Throughput varies a lot run to run; the failure count is the signal. Long runs with clearing on can also hit genuine network connect timeouts from port exhaustion — those are SqlException after ~29 s and are unrelated.)

Expected behavior

As in 7.0.x: an open in flight on a pool that gets shut down should complete, or be retried transparently against the replacement pool as the sync path does. If it must fail, not as PooledOpenTimeout — the message sends people hunting for a connection leak that does not exist.

Further technical details

Microsoft.Data.SqlClient: 7.1.0 (regression from 7.0.3)
.NET target: net10.0
SQL Server: 2022 (Linux container)
OS: Windows 11; also seen on ubuntu GitHub runners

Real-world impact

An xUnit project using EF Core with a database per test (965 tests): 0 failures in the previous 40 CI runs on 7.0.2; on 7.1.0, 4 of 5 runs fail with 1–4 tests each, a different test every time. Identical binaries apart from SqlClient: 7.0.2 0/5 failed runs, 7.1.0 4/5.

Sub-issues

Sub-issues

1 of 1 Issues completed
Loading 1 sub-issues

Activity

  1. github-actions commented on Sep 20, 2026

    @github-actions

    🔍 Triage Summary

    Check Result
    Issue type Bug
    Environment All required environment details provided for investigation
    Area Area\Connection Pooling
    Duplicates None found
    Regression Likely regression from v7.0.3 (introduced in #4302, "harden WaitHandleDbConnectionPool shutdown")

    Analysis

    This is a well-documented regression in WaitHandleDbConnectionPool.TryGetConnection: PR #4302 added a State is not Running short-circuit after WaitHandle.WaitAny that returns (true, null) on the async pending-open path whenever the pool is shut down mid-wait (e.g. via SqlConnection.ClearAllPools()/ClearPool() or DbConnectionPoolGroup.Prune()). WaitForPendingOpen treats a null connection with no exception as a timeout and throws PooledOpenTimeout, even though no actual timeout occurred and Max Pool Size was never approached. The reporter confirms the sync path already has a retry-against-replacement-pool loop (SqlConnectionFactory, "We've hit the race condition") that the async path lacks — code inspection of WaitHandleDbConnectionPool.cs and SqlConnectionFactory.cs in this repo confirms both code paths as described. Impact is high for any app using EF Core's EnsureDeleted()/database-per-test patterns, or any process calling ClearAllPools()/ClearPool() concurrently with in-flight opens; production apps may also see spurious failures after idle-pool pruning. Severity: P1 (functional regression causing spurious connection failures under a common, legitimate usage pattern, with quantified reproduction data).

    Next Steps

    Assign to Copilot coding agent to investigate WaitHandleDbConnectionPool.TryGetConnection (async pending-open path, guarded by the State is not Running checks added in #4302) and align its behavior with the sync path in SqlConnectionFactory.GetConnection/TryGetConnection: either retry the async pending-open transparently against the replacement pool (mirroring the sync retry-with-backoff loop), or surface a distinct, deterministic "pool shut down" signal instead of overloading PooledOpenTimeout. Add regression tests exercising concurrent OpenAsync + ClearAllPools()/ClearPool() and pool pruning to prevent recurrence, and verify against SQL Server 2022 and the reporter's repro harness. Bisection between 7.0.3 and 7.1.0 (specifically #4302) is recommended to confirm the fix scope.

    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 #4714 · copilot · auto · 54.4 AIC · ⌖ 12.5 AIC · ⊞ 11.4K · ◷

  2. cheenamalhotra commented on Sep 21, 2026

    @cheenamalhotra
    Member

    @mdaigle can you take a look?

  3. added
    Regression 💥
    on Sep 21, 2026
  4. added this to the 7.1.1 milestone on Sep 21, 2026
  5. moved this from To triage to In progress in SqlClient Boardon Sep 21, 2026
  6. mdaigle commented on Sep 21, 2026

    @mdaigle
    Contributor

    Thanks @DanielBryars, I think what I'll do is revert the short circuit after the wait handle. Any connection that is in flight will be allowed to continue and will be closed upon return (since the pool is shut down), matching the previous behavior.

    We have a potential future improvement to retry async requests in the same way we retry sync requests. That would avoid the wasteful open on a shut down pool by routing the request to a new, active pool. I'll log an issue for that and evaluate it as part of some upcoming async path improvements.

  7. modified the milestones: 7.1.1, 8.0.0-preview1 on Sep 23, 2026
  8. moved this from In progress to Done in SqlClient Boardon Sep 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Regression 💥

Type

Projects

Relationships

None yet

Development

No branches or pull requests

Issue actions