sqlite3 - file stays opened even after connection is closed (still opened by the cursor?)
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- Python
- Star
- 77.2k
- Fork
- 35.9k
- Chỉ số merge pull request
- Chỉ số pull request đang chờ
Mô tả
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
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Hướng nghiên cứu
Không có tệp repository hoặc test nào được nêu. Hãy bắt đầu bằng cách chạy trình tái hiện trên Windows và so sánh hành vi khi có và không có c.close(), sau đó truy vết vòng đời của cursor và connection của sqlite3. Hoàn tất có nghĩa là xác định được hành vi dự kiến của file handle sau khi đóng, đồng thời thêm hoặc cập nhật coverage để trường hợp xóa được báo cáo được giải quyết hoặc ghi lại.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- python, sqlite
- Lĩnh vực
- databases
- Loại issue
- Lỗi
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức độ hoạt động
- Đình trệ
- Độ rõ ràng
- Khá rõ ràng
- Mức phù hợp với người mới
- 38/100