Skip to content

Conversation

@paultiq
Copy link
Owner

@paultiq paultiq commented Sep 19, 2025

Per discussion, this PR enables free-threading for DuckDB for 3.14t (or later).

This should be considered highly experimental, since it was tested on release candidate builds of 3.14 and without pyarrow, pandas or polars support.

Background

Python 3.14 is in RC3 with release scheduled for Oct 7, 2025.

Python 3.13 released experimental support for free-threading.

In Python 3.14 free-threaded Python is officially supported

Module State

PEP489 - multi-phase-init calls for module_state to be initialized with each interpreter*. This PR takes a half-step towards that, by consolidating global state into a single static DuckDBPyModuleState. The idea being: it's easier to manage concurrency through a single instance than across instances.

Alternative ideas: DuckDBPyModuleState is not absolutely required. The alternative is to keep the import cache/instance cache/etc and add mutexes or scoped_critical_sections to control access.

* each interpreter refers to support for subinterpreters. Multiple subinterpreters is not supported in this PR for a few reasons, primarily due to the difficulty of attaching the import_cache to all the calls that assume a single global import_cache.

scoped_critical_section

DefaultConnection is synchronized with a py::scoped_critical_section

Since cursors are not thread safe. Segfaults occur in both release & these builds when cursors are used in an unsafe manner.

Testing

A set of threading test cases are added to tests/fast/threading. These tests should pass in both GIL enabled and free-threaded (no-GIL) builds.

…(albeit still static), adding critical w/ free-threading enabled for Py_GIL_DISABLED builds, with critical sections for default_connections
…s whether GIL or free threaded. Tested with pytest-run-parallel: `pytest --parallel-threads=10 --iterations=5`
@paultiq paultiq merged commit 7e885e7 into main Sep 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant