Skip to content

Keep a SqliteDataReader usable after its command is disposed - #39337

Merged
AndriySvyryd merged 3 commits into
dotnet:mainfrom
pengdows:sqlite-reader-outlives-command
Oct 22, 2026
Merged

AndriySvyryd merged 3 commits into
dotnet:mainfrom
pengdows:sqlite-reader-outlives-command

Conversation

@alaricd

@alaricd alaricd commented Oct 19, 2026

Copy link
Copy Markdown
Contributor

Fixes #39336

Summary

Disposing a SqliteCommand while its reader is open used to close the reader, because DisposePreparedStatements disposes DataReader before 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.
  • When the reader closes, it calls SqliteCommand.OnReaderClosed(), which completes the disposal: statements are finalized, the command is removed from the connection, and Disposed is raised once.
  • SqliteConnection.Close() calls a new internal CloseReaderAndDispose(). It closes any open reader and disposes each command exactly once, including commands whose disposal was waiting on their reader.
  • Commands without an open reader dispose exactly as before.

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: Disposed is 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 and Disposed is raised exactly once.

The first three fail without the change.

  • Microsoft.Data.Sqlite.Tests: 788 passed, 7 skipped, 0 failed
  • Microsoft.Data.Sqlite.sqlite3.Tests: 788 passed, 0 failed
  • EFCore.Sqlite.FunctionalTests: 38,301 passed, 285 skipped, 0 failed

🤖 Generated with Claude Code

- 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]>

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

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]>

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.

🔵 Needs a closer look

Fix the connection capture in OnReaderClosed() before raising Disposed to prevent a handler from causing a null-reference failure.

No open issues 🎉

2 resolved since last review

Review effort: Lite

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.

🟢 Approval recommended

No unresolved blocking issues were identified, and all approval assessments indicate readiness.

No open issues 🎉

Review effort: Lite

@AndriySvyryd AndriySvyryd left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for your contribution!

@AndriySvyryd
AndriySvyryd merged commit e37ef9f into dotnet:main Oct 22, 2026
18 of 19 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Oct 23, 2026
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.

Microsoft.Data.Sqlite: disposing a SqliteCommand closes its open SqliteDataReader

3 participants