-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
sqlite3 - file stays opened even after connection is closed (still opened by the cursor?) #135117
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
could not replicate this on arch linux. python 3.11.9 (no venv) |
This is not true. Closing a connection does not implicitly cause cursors to become "dead"1; it only decrefs the cursors. Clinging on to a reference may defer the actual closing of the SQLite database. It is good resource management to always explicitly close your cursors when you are done using them. Perhaps we could explain this better in the docs. Footnotes
|
Expanding on this: We use the SQLite C API
Explicitly (calling |
I suggest we start with a docs update. We might also want to emit a resource warning. |
Sorry, if it's kind of ambigious. I meant it as in "the cursor becomes unusable", basically "closed". Since all cursors become unusable once connection is closed, so maybe it would made sense for them to close their connection to the file too. I don't know how it's internally structured and whether it's possible to close all cursors on So, some documentation note will probably do - it seems closing cursor explicitly or implicitly (by deleting it) before reaccessing the file could be crucial on Windows. Not sure if there's a similar angle on closing cursor for Unix, but for anyone using sqlite, it's a small caveat to keep in mind in case they want their code to run cross-platform, though there won't be errors on Unix. |
It sounds like an interesting idea. |
Uh oh!
There was an error while loading. Please reload this page.
Bug report
Bug description:
Python 3.11.9
Consider the snippet below.
It creates a new sqlite database, then closing the connection to it and trying to delete the file. On Windows it fails since sqlite file is still used by Python.
It seems the created cursor is still using the file, though all connections to it are closed.
Which is kind of unexpected, since when you close the connection to the database, connection object and all cursors are effectively become dead and they cannot be reopened, so nothing should be still using the file after connection is closed.
What helps is to close cursor explicitly (
c.close()
or just delete itdel c
).Another detail - if we create a table, but don't insert anything to it, issue doesn't occur.
CPython versions tested on:
3.11
Operating systems tested on:
Windows
The text was updated successfully, but these errors were encountered: