DuckDB spawns worker threads on module import rather than connection creation
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 48/100
Research direction
Start by reproducing the issue with import duckdb on the reported Ubuntu setup and inspect the import path and duckdb.connect() initialization. Use the provided gdb backtrace, especially ExecuteForever, to trace when the worker pool is created. Done means importing the module alone does not spawn the worker threads, while creating a connection still initializes the required workers.
Written by the indexing model from the issue text.
Description
What happens?
Importing the duckdb Python module (import duckdb) immediately spawns ~100 worker threads (appears to be ncpus), even without creating any connections. This causes unexpected resource consumption in applications that import the module but may not immediately use it.
Expected Behavior
Worker threads should only be created when:
- An actual database connection is established via
duckdb.connect() - Or when explicitly configured to do so
The module import itself should be lightweight and not spawn background threads.
Proposed Solution
Consider lazy initialization of the thread pool:
- Defer TaskScheduler/thread pool creation until first
connect()call - Or provide an environment variable/config option to control thread spawning behavior on import
- Similar to how other database drivers handle connection pooling
Workaround
Currently requires monkey-patching or lazy imports to avoid the thread spawning:
# Must be done before any code imports duckdb
import sys
import importlib.util
def lazy_import_duckdb():
spec = importlib.util.find_spec("duckdb")
module = importlib.util.module_from_spec(spec)
sys.modules["duckdb"] = module
# Don't execute the module yet
return module
To Reproduce
import duckdb # This alone spawns ~100 threads
When attaching gdb to a process that has imported duckdb:
thread apply all btshows 100+ threads with duckdb in their stack traces- All threads show
ExecuteForeverin their call stacks - Base stack frame is
clone3syscall
OS:
Ubuntu 22.04 x86_64
DuckDB Version:
0.6.1
DuckDB Client:
Python
Hardware:
128 core CPU
Full Name:
Nova DasSarma
Affiliation:
Anthropic, PBC
What is the latest build you tested with? If possible, we recommend testing with the latest nightly build.
I have tested with a stable release
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?
- Yes, I have
Did you include all relevant configuration (e.g., CPU architecture, Python version, Linux distribution) to reproduce the issue?
- Yes, I have
- Dominant language
- Python
- Stars
- 186
- Forks
- 113
- Avg merge
- 20h 58m
- Merged PRs (30d)
- 11
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from duckdb/duckdb-python
-
Difficulty 2/5 1-3 hours Newbie friendliness 90/100
duckdb/duckdb-python#627 ·
-
needs triage
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
duckdb/duckdb-python#576 · 3 comments ·
-
needs triage
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
duckdb/duckdb-python#534 ·
-
needs triage
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
duckdb/duckdb-python#386 ·
-
needs triage
Difficulty 4/5 3-5 days Newbie friendliness 48/100
duckdb/duckdb-python#622 ·
All issues in duckdb/duckdb-python
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
enhancement
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100