Repository navigation
Allow Close to be called more than once - #373
Merged
Merged
Conversation
DbConnection.Close: "An application can call Close or CloseAsync more than one time. No exception is generated." A second Close returns without raising StateChange again. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Coverage Report for CI Build 39415913370Coverage increased (+0.06%) to 92.552%Details
Uncovered ChangesNo uncovered changes found. Coverage RegressionsNo coverage regressions found. Coverage Stats💛 - Coveralls |
Owner
|
Thanks, this looks good to me. Could you add one more test: [Fact]
public async Task CloseNeverOpenedConnectionDoesNotThrow()
{
await using var duckDBConnection = new DuckDBConnection("DataSource=:memory:");
var stateChanges = 0;
duckDBConnection.StateChange += (_, _) => stateChanges++;
duckDBConnection.Close();
await duckDBConnection.CloseAsync();
duckDBConnection.State.Should().Be(ConnectionState.Closed);
stateChanges.Should().Be(0, "a connection that was never opened does not change state");
} |
This branch is waiting to be deployed
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.
A second
DuckDBConnection.Close()(orCloseAsync(), orClose()afterDispose()) throwsInvalidOperationException: Connection is already closed.The contract
DbConnection.Close, Remarks:
The same sentence is on SqlConnection.Close, and Npgsql, MySqlConnector, Microsoft.Data.Sqlite and SqlClient all accept a repeated
Close.Why it matters
The common pattern of closing explicitly and then letting
usingdispose works today only becauseDisposechecks the state first. Code that closes in more than one place (afinallyplus a helper, or a data layer that closes on error and again on cleanup) gets an exception from DuckDB.NET and no other provider.Background
The current behaviour was chosen deliberately in c0ea769 ("calling Close() on a connection when the connection is already closed or disposed will throw an exception"), with
ExceptionOnDoubleCloseandExceptionOnDisposeThenClosepinning it. This PR replaces those two tests, because the documented contract says the opposite. If you would rather keep the exception, I'm happy to close this. The quote above is the whole argument.Change
Close()returns when the connection is already closed. It does not raiseStateChangea second time. Tests:CloseCanBeCalledMoreThanOnce(sync and async, and checks thatStateChangefires once) andCloseAfterDisposeDoesNotThrowreplace the two tests above. The full suite passes.Conformance test:
Close_can_be_called_more_than_once.Found by the pengdows ADO.NET conformance suite, which runs the documented
System.Data.Commoncontract against every provider; each failure it reports cites the learn.microsoft.com sentence the provider departs from.🤖 Generated with Claude Code