Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

Segfault at interpreter exit when the duckdb module outlives shutdown outside sys.modules

Aperta
#638 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
45/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
cpp, python
Ambito
backend

Direzione di ricerca

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.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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
Lingua principale
Python
Stelle
186
Fork
113
Merge medio
1g 1h
PR unite (30g)
16

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di duckdb/duckdb-python

Tutte le issue di duckdb/duckdb-python

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.