Segfault at interpreter exit when the duckdb module outlives shutdown outside sys.modules
メンテナーはふだん 1 日以内に返信
評価
調査の方向性
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.
索引モデルが issue の本文から書いたものです。
説明
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
- 主要言語
- Python
- スター
- 191
- フォーク
- 120
- 平均マージ
- 14時間 1分
- マージ済み PR(30日)
- 23
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートなし
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
duckdb/duckdb-python のほかの issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
duckdb/duckdb-python#653 ·
メンテナーはふだん 1 日以内に返信
-
Type stubs omit the `connection` keyword on module-level `read_json` and `write_csv`対応中かも @tojacob03 が 16 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 90/100
duckdb/duckdb-python#627 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
`read_json` accepts a list of paths at runtime but the type stub only allows a single path.対応中かも @aleks-drozy が 74 日前に担当しました。 オープンneeds triage
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
duckdb/duckdb-python#576 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
Possible reference leak of the argument tuple in `FunctionCall()`対応中かも このイシューにリンクされたプルリクエストがオープン中、またはマージ済みです。 オープンneeds triage
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
duckdb/duckdb-python#534 ·
メンテナーはふだん 1 日以内に返信
-
Missing ROW_GROUPS_PER_FILE argument in write_parquet function対応中かも このイシューにリンクされたプルリクエストがオープン中、またはマージ済みです。 オープンneeds triage
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
duckdb/duckdb-python#386 ·
メンテナーはふだん 1 日以内に返信
duckdb/duckdb-python の issue をすべて見る
似ている issue
-
Path subtraction removes repeated suffix characters instead of one suffix対応中かも @Gwerdonatus が今日担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
joke2k/django-environ#671 ·
-
good first issue help wanted windows
難易度 2/5 1〜3時間 初心者へのやさしさ 80/100
-
test_pgbouncer deploys the server under test from Charmhub instead of the local build (track/1.31)オープンbug
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
canonical/temporal-k8s-operator#195 · コメント 1 件 ·
メンテナーはふだん 4 日以内に返信
-
`burr` fails to start when the Python path has a space in it対応中かも @enekoreto が今日担当しました。 オープンstatus/needs-triage
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
メンテナーはふだん 5 日以内に返信
-
device support request
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
zigpy/zha-device-handlers#5461 ·
メンテナーはふだん 4 日以内に返信