Repository navigation
Keep a SqliteDataReader usable after its command is disposed - #39337
Merged
AndriySvyryd merged 3 commits intoOct 22, 2026
Merged
Conversation
- Disposing a SqliteCommand while its reader is open no longer closes the reader; the command finishes disposing (finalizing its statements and raising Disposed) when the reader closes - Closing the connection still closes the reader and disposes the command - SqlClient, Npgsql and System.Data.SQLite already allow reading after the command is disposed Co-Authored-By: Claude Opus 5.5 <[email protected]>
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
Connection shutdown can fail through recursive closure or a null reader reference after cleanup errors.
2 open issues
What changed in this PR
Keeps Microsoft.Data.Sqlite readers usable after their commands are disposed, addressing #39336.
Changes:
- Defers command cleanup until its reader closes.
- Updates connection shutdown to close readers explicitly.
- Adds tests for reader usability, deferred cleanup, and connection closure.
| File | Description |
|---|---|
| test/Microsoft.Data.Sqlite.Tests/SqliteCommandTest.cs | Adds disposal and reader-lifetime regression tests. |
| src/Microsoft.Data.Sqlite.Core/SqliteDataReader.cs | Completes deferred command disposal on reader closure. |
| src/Microsoft.Data.Sqlite.Core/SqliteConnection.cs | Uses explicit reader-and-command cleanup during shutdown. |
| src/Microsoft.Data.Sqlite.Core/SqliteCommand.cs | Tracks deferred disposal and adds cleanup helpers. |
Review effort: Balanced
…led reader cleanup - Closing a reader opened with CommandBehavior.CloseConnection calls back into Close(). The nested call closed the inner connection and cleared the command list, so the outer call threw NullReferenceException or ArgumentOutOfRangeException. Close() now ignores the nested call. This also happened before deferred disposal, whenever such a reader was open. - If closing the reader of a disposed command threw (for example SQLITE_BUSY when resetting an unfinished INSERT ... RETURNING), the command stayed pending with no reader, and Close() threw NullReferenceException. It now disposes the command directly. Co-Authored-By: Claude Opus 5.5 <[email protected]>
AndriySvyryd
approved these changes
Oct 22, 2026
AndriySvyryd
left a comment
Member
There was a problem hiding this comment.
Thanks for your contribution!
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.


Fixes #39336
Summary
Disposing a
SqliteCommandwhile its reader is open used to close the reader, becauseDisposePreparedStatementsdisposesDataReaderbefore finalizing the statements. SqlClient, Npgsql and System.Data.SQLite all keep the reader usable, so code that disposes the command before consuming the reader works everywhere except Microsoft.Data.Sqlite.SqliteCommand.Dispose(true)with an open reader now records that the disposal is pending and returns. The reader keeps stepping through the command's statements.SqliteCommand.OnReaderClosed(), which completes the disposal: statements are finalized, the command is removed from the connection, andDisposedis raised once.SqliteConnection.Close()calls a new internalCloseReaderAndDispose(). It closes any open reader and disposes each command exactly once, including commands whose disposal was waiting on their reader.Testing
New tests in
SqliteCommandTest:Dispose_leaves_open_reader_usable: reads every row and the second result set after the command is disposed.Dispose_with_open_reader_completes_when_reader_closes:Disposedis not raised until the reader closes, and no statements remain on the connection afterwards (sqlite3_next_stmt).Dispose_with_open_reader_works_with_CloseConnection: the reader is still usable and closing it still closes the connection.Connection_Close_closes_reader_of_disposed_command: the reader is closed andDisposedis raised exactly once.The first three fail without the change.
Microsoft.Data.Sqlite.Tests: 788 passed, 7 skipped, 0 failedMicrosoft.Data.Sqlite.sqlite3.Tests: 788 passed, 0 failedEFCore.Sqlite.FunctionalTests: 38,301 passed, 285 skipped, 0 failed🤖 Generated with Claude Code