Segfault at interpreter exit when the duckdb module outlives shutdown outside sys.modules
Maintainer thường phản hồi trong vòng 1 ngày
Đánh giá
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức phù hợp với người mới
- 45/100
Hướng nghiên cứu
Start by running the provided repro on Python 3.11, then inspect DuckDBPyConnection::default_connection and import_cache in src/duckdb_py/pyconnection.cpp and _clean_default_connection in src/duckdb_py/duckdb_python.cpp. Trace cleanup during interpreter shutdown when duckdb is absent from sys.modules; done means the repro exits successfully with status 0 while the module reference remains alive.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
What happens?
The process segfaults at interpreter exit (exit status -11 / 139) when the duckdb module object stays alive but is no longer registered in sys.modules at shutdown. Nothing is printed by -X faulthandler, so the crash appears to happen after interpreter finalization.
A common way to get into this state is a test that uses unittest.mock.patch.dict(sys.modules): DuckDB is imported for the first time inside the block, and leaving the block restores the old snapshot, which removes duckdb/_duckdb from sys.modules. Any remaining reference, such as a sqlalchemy.ext.compiler.compiles hook registered by duckdb_engine, keeps the module alive.
Likely cause: the process-global default connection and import cache (DuckDBPyConnection::default_connection / import_cache, src/duckdb_py/pyconnection.cpp around lines 70-72 in v1.5.2) are released only by DuckDBPyConnection::Cleanup(). That function runs only when the _clean_default_connection capsule is destroyed (src/duckdb_py/duckdb_python.cpp around lines 1154-1157 in v1.5.2; the nanobind capsule on main has the same design). The interpreter clears module dicts during shutdown only for modules that are still in sys.modules, so the capsule is never destroyed while Python is alive. The C++ statics are then destroyed at exit(), after the interpreter is gone.
A cleanup that does not depend on the module dict being cleared would avoid this, e.g. one registered with Python's atexit, or Py_AtExit.
To Reproduce
import gc
import sys
from unittest import mock
with mock.patch.dict(sys.modules):
import duckdb # duckdb/_duckdb are removed from sys.modules on exit
def holder():
pass
holder.module = duckdb # any surviving reference keeps the module alive
gc.freeze()
print("exiting", duckdb.__version__, flush=True)
$ python repro.py; echo $?
exiting 1.5.5
Segmentation fault
139
With the same reference but without the patch.dict block, the process exits with status 0. It also exits with status 0 if the module is freed before shutdown.
OS:
Linux x86_64 (Debian, kernel 6.1)
DuckDB Package Version:
1.5.2 and 1.5.5
Python Version:
3.11.15
Full Name / Affiliation
N/A
Did you include all relevant configuration to reproduce the issue?
- Yes
- Ngôn ngữ chính
- Python
- Star
- 191
- Fork
- 120
- Merge trung bình
- 14 giờ 1 phút
- Pull request đã merge (30 ngày)
- 23
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Không có mẫu pull request
- Đọc 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.
Issue khác của duckdb/duckdb-python
-
duckdb.experimental.spark fails to import without typing_extensions, which is not a runtime dependencyCó thể đã có người làm @evertlammerts đã nhận hôm nay. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
duckdb/duckdb-python#653 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Type stubs omit the `connection` keyword on module-level `read_json` and `write_csv`Có thể đã có người làm @tojacob03 đã nhận 17 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 90/100
duckdb/duckdb-python#627 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
`read_json` accepts a list of paths at runtime but the type stub only allows a single path.Có thể đã có người làm @aleks-drozy đã nhận 75 ngày trước. Đang mởneeds triage
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
duckdb/duckdb-python#576 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Possible reference leak of the argument tuple in `FunctionCall()`Có thể đã có người làm Có pull request liên kết đang mở hoặc đã được merge. Đang mởneeds triage
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
duckdb/duckdb-python#534 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Missing ROW_GROUPS_PER_FILE argument in write_parquet functionCó thể đã có người làm Có pull request liên kết đang mở hoặc đã được merge. Đang mởneeds triage
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
duckdb/duckdb-python#386 ·
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của duckdb/duckdb-python
Issue tương tự
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 82/100
EchoTools/nevr-runtime#651 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
bug frontend rt sev-3
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
Maintainer thường phản hồi trong vòng 2 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 65/100
noontide-co/mainbranch#1211 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
[1.x Bug] run-operation --sql reports success but rolls back writes on transaction-capable adaptersĐang mởtriage
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 64/100
dbt-labs/dbt#16784 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày