[6.0.1] Microsoft.Data.Sqlite: Clean up on application exit #26529
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.
Hooks AppDomain.ProcessExit to clean up native resources.
Fixes #26422
Customer Impact
Databases using the write-ahead journal mode (the default for ones created by EF Core) create additional temporary files next to the database file. These files are cleaned up when the native connection is closed.
Previously, Microsoft.Data.Sqlite relied on DbConnection.Close, IDisposable, and finalizers to close the native connection.
A connection pool was added in 6.0 which keeps the native connection open after the DbConnection is closed/disposed/finalized.
Even though we leverage SafeHandle to close native connections, these finalizers are not called when the application is exits. This results in the temporary files not being properly cleaned up.
Regression?
Yes, the connection pool was added in 6.0. The temporary files were always cleaned up if the DbConnection was disposed.
Risk
Low. This is the same mechanism used by other ADO.NET providers.
Verification
Manually verified that the temporary journal files are now cleaned up on application exit.