Skip to content

Introduce scoped DatabaseFacade.UseCommandTimeout - #39403

Open
SkyDevLab wants to merge 1 commit into
dotnet:mainfrom
SkyDevLab:feature/18895-use-command-timeout
Open

SkyDevLab wants to merge 1 commit into
dotnet:mainfrom
SkyDevLab:feature/18895-use-command-timeout

Conversation

@SkyDevLab

@SkyDevLab SkyDevLab commented Oct 24, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #18895

Description

Following the accepted API proposal, this PR introduces UseCommandTimeout overloads on DatabaseFacade (RelationalDatabaseFacadeExtensions):

public static IDisposable UseCommandTimeout(this DatabaseFacade databaseFacade, int? timeout)
public static IDisposable UseCommandTimeout(this DatabaseFacade databaseFacade, TimeSpan timeout)

Key Behaviors

  • Temporarily sets the command timeout for the lifetime of the returned IDisposable scope.
  • Restores the previous command timeout on scope disposal (including when exceptions occur).
  • Supports nested scopes.
  • Handles Timeout.InfiniteTimeSpan (mapped to 0) and validates negative timeouts consistently with SetCommandTimeout.
  • Updated public API baseline in \EFCore.Relational.baseline.json.
  • Added tests covering scoped execution, nested scopes, exception safety, and argument validation.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Public API documentation should clarify the supported null and infinite-timeout behaviors before approval.

1 open issue
What changed in this PR

This pull request introduces scoped command-timeout configuration for relational DatabaseFacade instances, with nested scopes and exception-safe restoration.

Changes:

  • Adds UseCommandTimeout overloads for nullable seconds and TimeSpan values.
  • Supports nested scopes, restoration, validation, and infinite-timeout handling.
  • Adds relational and SQL Server tests.
  • Updates the relational public API baseline.
File Summary
test/​EFCore.SqlServer.Tests/​Extensions/​SqlServerDatabaseFacadeExtensionsTest.cs Covers scoping, nesting, restoration, exception safety, infinite timeouts, and validation.
test/​EFCore.Relational.Tests/​Extensions/​RelationalDatabaseFacadeExtensionsTest.cs Verifies behavior for non-relational providers.
src/​EFCore.Relational/​Extensions/​RelationalDatabaseFacadeExtensions.cs Implements the scoped timeout APIs and restoration logic. Minor documentation updates should clarify null reset behavior and Timeout.InfiniteTimeSpan mapping.
src/​EFCore.Relational/​EFCore.Relational.baseline.json Records the two new public overloads.

Review effort: Lite

Comment on lines +1011 to +1012
/// <param name="timeout">The timeout to use, in seconds.</param>
/// <returns>An <see cref="IDisposable" /> that resets the command timeout to its previous value when disposed.</returns>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

commandTimeout for some query only

2 participants