sqlite3 - file stays opened even after connection is closed (still opened by the cursor?)
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 77.2k
- Forks
- 35.9k
- PR merge metrics
- PR metrics pending
Description
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 it del c).
Another detail - if we create a table, but don't insert anything to it, issue doesn't occur.
from pathlib import Path
import sqlite3
db_path = Path("new_db.sqlite")
if db_path.exists():
db_path.unlink()
db = sqlite3.connect(db_path)
c = db.cursor()
c.execute("CREATE TABLE test (id INTEGER PRIMARY KEY, value TEXT)")
# Commenting out resolves the issue.
c.executemany("INSERT INTO test (value) VALUES (?)", [(f"value_{i}",) for i in range(1000)])
db.commit()
# c.close() # Required to avoid the error.
db.close()
# PermissionError:
# [WinError 32] The process cannot access the file because it is being used by another process: 'new_db.sqlite'
db_path.unlink()
CPython versions tested on:
3.11
Operating systems tested on:
Windows
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
No repository file or test is named. Start by running the Windows reproducer and comparing behavior with and without c.close(), then trace the sqlite3 cursor and connection lifecycle. Done means establishing the intended post-close file-handle behavior and adding or updating coverage so the reported deletion case is resolved or documented.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python, sqlite
- Domain
- databases
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100