Connect timeout propagated through pool#4270
Merged
Merged
Conversation
Co-authored-by: Copilot <copilot@github.com>
Co-authored-by: Copilot <copilot@github.com>
Thread a single TimeoutTimer from SqlConnection.Open through DbConnectionInternal/DbConnectionClosed -> SqlConnectionFactory -> IDbConnectionPool implementations down to physical connection creation. Replaces per-layer TimeoutTimer construction. - DbConnectionInternal: OpenConnection creates the timer; TryOpenConnection, TryReplaceConnection, TryOpenConnectionInternal accept TimeoutTimer. - DbConnectionClosed/SqlConnectionInternal: signature pass-through. - SqlConnectionFactory: TryGetConnection, CreateNonPooledConnection, and CreateReplaceConnectionContinuation accept and forward TimeoutTimer. - IDbConnectionPool: TryGetConnection/ReplaceConnection now require TimeoutTimer. - ChannelDbConnectionPool: removes its local timer creation; uses caller's. - WaitHandleDbConnectionPool: PendingGetConnection carries TimeoutTimer; CreateObject/UserCreateRequest/ReplaceConnection accept it. Maintenance worker uses CreationTimeout-based timer. - SqlConnection.TryOpenInner: creates the TimeoutTimer for the open path. - Tests: TransactedConnectionPoolTest mock updated; new PoolTestExtensions provides legacy 3-arg overloads that derive the timer from owningObject.ConnectionTimeout for existing test call sites.
Add an overload TimeoutTimer.StartNew(TimeSpan, TimeProvider) and route all clock reads (current time, expiration check, remaining ms, Reset) plus CreateCancellationTokenSource through the supplied TimeProvider. The existing parameterless StartNew now delegates to TimeProvider.System, preserving behavior for production call sites. This lets tests inject Microsoft.Extensions.Time.Testing.FakeTimeProvider and trigger timeout / cancellation behavior by advancing virtual time, removing wall-clock dependence and timing flakiness. - TimeoutTimer: store TimeProvider, derive ExpirationTicks via timeProvider.GetUtcNow().UtcDateTime.ToFileTimeUtc() (file-time scale preserved for compatibility with TdsParser consumers); CTS scheduled via new CancellationTokenSource(TimeSpan, TimeProvider). - src.csproj: add Microsoft.Bcl.TimeProvider for the net462 TFM (required for the System.TimeProvider type on .NET Framework). - UnitTests.csproj: add Microsoft.Extensions.TimeProvider.Testing for FakeTimeProvider. - Directory.Packages.props: pin both packages. - Add TimeoutTimerTimeProviderTests covering: System default, null guard, IsExpired/MillisecondsRemaining advancement, Reset, CTS firing on virtual-time advance, already-expired CTS, infinite timer, and the TimeProvider property accessor.
Co-authored-by: Copilot <copilot@github.com>
Co-authored-by: Copilot <copilot@github.com>
Co-authored-by: Copilot <copilot@github.com>
Co-authored-by: Copilot <copilot@github.com>
Co-authored-by: Copilot <copilot@github.com>
Co-authored-by: Copilot <copilot@github.com>
Co-authored-by: Copilot <copilot@github.com>
Co-authored-by: Copilot <copilot@github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 17 out of 17 changed files in this pull request and generated 2 comments.
Comments suppressed due to low confidence (1)
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/ConnectionPool/WaitHandleDbConnectionPool.cs:918
- The PendingGetConnection DueTime is still computed from CreationTimeout/ADP.TimerCurrent rather than the passed-in TimeoutTimer. For async opens this can schedule pending work beyond the caller’s remaining budget. Consider setting DueTime based on the TimeoutTimer (e.g., its ExpirationTicks or remaining duration) so WaitForPendingOpen computes delays that respect the overall timeout.
var pendingGetConnection =
new PendingGetConnection(
CreationTimeout == 0 ? Timeout.Infinite : ADP.TimerCurrent() + ADP.TimerFromSeconds(CreationTimeout / 1000),
owningObject,
taskCompletionSource,
mdaigle
commented
May 13, 2026
apoorvdeshmukh
previously approved these changes
May 27, 2026
apoorvdeshmukh
left a comment
Contributor
There was a problem hiding this comment.
LGTM! Just wondering if we should create a separate issue to add any additional event tracing for scenarios where we throw exceptions to identify what caused it in the first place, but this is beyond the scope of this PR.
cheenamalhotra
requested changes
May 28, 2026
cheenamalhotra
approved these changes
May 28, 2026
cheenamalhotra
requested changes
May 28, 2026
cheenamalhotra
previously approved these changes
May 28, 2026
benrr101
approved these changes
May 28, 2026
cheenamalhotra
approved these changes
May 29, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This pull request introduces improvements to connection timeout handling in the SQL client by switching from simple timeouts to a more robust
TimeoutTimersystem. It also adds theMicrosoft.Bcl.TimeProviderdependency, and updates several method signatures to consistently propagate the timeout budget across connection creation and pooling logic. These changes help ensure that the total timeout budget is respected throughout the connection process, improving reliability and observability for connection attempts.Dependency updates:
Microsoft.Bcl.TimeProvideras a dependency in both the solution's package version list and the main project file to support advanced timeout handling. [1] [2] [3]Timeout handling improvements:
TimeoutTimerclass throughout the connection and pooling code, ensuring that the overall timeout budget is consistently tracked and enforced during connection attempts and retries. [1] [2] [3] [4] [5]TryGetConnectionandGetInternalConnectionto accept and use aTimeoutTimerinstead of aTimeSpan, and to derive cancellation tokens from the timer for accurate timeout enforcement. [1] [2]API and signature changes:
TryOpenConnection,TryReplaceConnection, and related overrides) to include theTimeoutTimerparameter, ensuring the timeout is propagated through all relevant layers. [1] [2] [3] [4] [5]Connection creation and pooling:
TimeoutTimerthrough to the physical connection factory, so that time spent waiting in the pool is deducted from the overall connection timeout budget. [1] [2] [3]Code cleanup and comments:
These changes collectively improve the accuracy and reliability of connection timeouts, making the client's behavior more predictable under high load or slow network conditions.