Skip to content

Close connections opened by other threads in AutoImport.close() - #888

Open
cristianchiriac wants to merge 1 commit into
python-rope:masterfrom
cristianchiriac:fix/autoimport-close-thread-connections
Open

cristianchiriac wants to merge 1 commit into
python-rope:masterfrom
cristianchiriac:fix/autoimport-close-thread-connections

Conversation

@cristianchiriac

Copy link
Copy Markdown

Description

AutoImport opens one SQLite connection per thread, but close() only closed the connection of the thread calling it. Connections opened by worker threads stayed open until they were garbage collected, which shows up as ResourceWarning: unclosed database on Python 3.13+.

This keeps track of each thread's connection and closes all of them in close(). Connections of threads that have already finished are closed when a new thread opens its connection, so the list doesn't keep growing in long running processes.

To be able to close them from another thread, connections are now created with check_same_thread=False. Each thread still only uses its own connection.

Note: most of the unclosed database warnings in the test suite come from tests that create an AutoImport and never call close(), so this change doesn't remove those.

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

AutoImport opens one SQLite connection per thread, but close() only
closed the one of the calling thread, so connections made by worker
threads were left open (ResourceWarning: unclosed database).

Keep track of every thread's connection and close them all in close().
Connections of threads that have finished are closed when a new one is
opened, so the list doesn't grow in long running processes.

Fixes python-rope#886

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