Crash when terminating process if Py_Finalize isn't called
- Dominant language
- Python
- Stars
- 187
- Forks
- 112
- Avg merge
- 13h 29m
- Merged PRs (30d)
- 17
Description
### What happens?
If Python is embedded in another application and the process terminates without a chance for Py_Finalize to be called, there's a DuckDBPyConnection object that gets destroyed by the C runtime when the process ends, and that causes the process to crash.
In ~DuckDBPyConnection it's assumed the GIL is already held, but this isn't true when the process is being terminated (gil_scoped_release calls PyEval_SaveThread, and PyEval_SaveThread crashes because the GIL is not held). This object is cleaned up if Py_Finalize is called via a capsule, but that shouldn't be relied on.
A quick fix would be to add Py_IsInitialized and PyGILState_Check checks to ~DuckDBPyConnection, but a better fix would be to not do this cleanup in the destructor of a static and instead to move this to module state so Python can manage its lifecycle. I see there's a comment about PEP 489 already in the code suggesting the same.
### To Reproduce
```
# Importing duckdb is enough to cause this script to crash.
# Comment this line out and it will work fine.
import duckdb
import ctypes
# ctypes releases the GIL around this call (standard behavior for foreign DLL calls,
# unlike ctypes.pythonapi calls). ExitProcess never returns, so the GIL stays released
# from here on - (no Python code running, GIL not held).
# Py_Finalize is never called. ExitProcess still triggers DLL_PROCESS_DETACH for every
# loaded DLL, so _duckdb.pyd's static destructor runs with no GIL held and no valid
# thread state for this thread.
ctypes.windll.kernel32.ExitProcess(0)
```
### OS:
Windows
### DuckDB Package Version:
1.5.4
### Python Version:
3.14.5
### Full Name:
Tony Roberts
### Affiliation:
PyXLL Ltd
### What is the latest build you tested with? If possible, we recommend testing with the latest nightly build.
I have tested with a source build
### Did you include all relevant data sets for reproducing the issue?
Not applicable - the reproduction does not require a data set
### Did you include all code required to reproduce the issue?
- [x] Yes, I have
### Did you include all relevant configuration to reproduce the issue?
- [x] Yes, I have
Contributor guide
Research direction
Start with the DuckDBPyConnection destructor and the Py_Finalize and PyGILState_Check behavior described in the report, then review the existing PEP 489 comment. Use the supplied Windows ctypes.ExitProcess reproduction; done means importing duckdb and terminating without Py_Finalize no longer crashes.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- c, python
- Domain
- backend-api-design
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Active
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100