Skip to content

autoimport: track and close SQLite connections across worker threads - #889

Open
mcepl wants to merge 1 commit into
masterfrom
886-track-close-SQL-conns
Open

mcepl wants to merge 1 commit into
masterfrom
886-track-close-SQL-conns

Conversation

@mcepl

@mcepl mcepl commented Sep 27, 2026

Copy link
Copy Markdown
Contributor

AutoImport dynamically allocates per-thread SQLite connections using threading.local(). However, AutoImport.close() only closed the connection belonging to the calling thread, orphaning any connections created by worker threads and triggering ResourceWarning on Python 3.12+.

Keep a thread-safe registry of all active connections on the AutoImport instance, create connections with check_same_thread=False, and ensure AutoImport.close() commits and closes all registered connections. Also expose close_thread_connection() for worker threads and implement context manager protocol support on AutoImport.

Fixes: #886

Checklist (delete if not relevant):

  • I have added tests that prove my fix is effective or that my feature works
  • I have updated CHANGELOG.md
  • I have made corresponding changes to user documentation for new features
  • I have made corresponding changes to library documentation for API changes

AutoImport dynamically allocates per-thread SQLite connections using
threading.local(). However, AutoImport.close() only closed the connection
belonging to the calling thread, orphaning any connections created by
worker threads and triggering ResourceWarning on Python 3.12+.

Keep a thread-safe registry of all active connections on the AutoImport
instance, create connections with check_same_thread=False, and ensure
AutoImport.close() commits and closes all registered connections. Also
expose close_thread_connection() for worker threads and implement
context manager protocol support on AutoImport.

Fixes: #886
@codecov

codecov Bot commented Sep 27, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 95.31250% with 6 lines in your changes missing coverage. Please review.
✅ Project coverage is 95.33%. Comparing base (2bd17a8) to head (411c697).

Files with missing lines Patch % Lines
rope/contrib/autoimport/sqlite.py 90.00% 6 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #889      +/-   ##
==========================================
- Coverage   95.34%   95.33%   -0.01%     
==========================================
  Files         134      134              
  Lines       26819    26922     +103     
==========================================
+ Hits        25570    25667      +97     
- Misses       1249     1255       +6     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

AutoImport.close() leaks SQLite connections created in worker threads (ResourceWarning: unclosed database)

1 participant