Explicitly close sqlite connections in ChannelIndex#287
Merged
Conversation
`ChannelIndex.cache_for_subdir()` returns a `CondaIndexCache` whose `db` attribute lazily opens an `sqlite3.Connection` on first access. The five callers in `ChannelIndex` (`extract_wrapper`, `index_subdir`, `index_subdir_shards`, `_update_channeldata`, `build_run_exports_data`) drop the cache reference without ever calling `close()`, so the connections stay open until the GC finalizes them. On Python 3.13+ the sqlite3 module emits `ResourceWarning: unclosed database` for every connection finalized this way, which makes test runs in downstream consumers like conda-build noisy and can keep the cache `.db` file open longer than API consumers expect. Wrap each call site in `contextlib.closing(...)` so the connection is closed as soon as the cache is no longer needed. Add a regression test that subclasses `sqlite3.Connection` to assert every connection opened by `update_index()` is explicitly closed. Closes #236
3a4f2a4 to
7c631ce
Compare
Contributor
|
Why don't we add |
Member
Author
Works for me, would be nicer, but also something we wouldn't be able to disable? |
Contributor
|
Do we need to be able to disable it? If you don't want it, don't use the with statement? |
dholth
reviewed
May 4, 2026
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.
Description
ChannelIndex.cache_for_subdir()returns aCondaIndexCachewhosedbattribute lazily opens ansqlite3.Connectionon first access. Five callers inChannelIndex(extract_wrapper,index_subdir,index_subdir_shards,_update_channeldata,build_run_exports_data) drop the cache reference without ever callingclose(), so the connections stay open until the GC finalizes them.This causes two issues:
ResourceWarning: unclosed databasefor every connection finalized this way. conda-build CI on Python 3.14 hits this ~1900 times per run, see for example this job..dbfile can stay open longer than API consumers expect afterupdate_index()returns, which is the original report in Use with closing(...) to clean up sqlite connections promptly #236.This PR wraps each
cache_for_subdir(subdir)call site incontextlib.closing(...), as suggested in #236.A regression test (
test_update_index_closes_sqlite_connections) subclassessqlite3.Connectionto track explicitclose()calls and asserts every connection opened byupdate_index()is closed. The test fails onmainand passes with this change.Most of the diff line count is indentation from the new
with closing(...):blocks; there are no behavior changes other than closing connections promptly.Closes #236
Checklist - did you ...
newsdirectory (using the template) for the next release's release notes?Add / update outdated documentation?no documented behavior change.