Skip to content

Share a single DuckDB instance across State with a global thread knob#299

Merged
timkpaine merged 1 commit into
mainfrom
ac/duckdb
Jul 7, 2026
Merged

Share a single DuckDB instance across State with a global thread knob#299
timkpaine merged 1 commit into
mainfrom
ac/duckdb

Conversation

@arhamchopra

@arhamchopra arhamchopra commented Jul 7, 2026

Copy link
Copy Markdown
Collaborator

Description

State created one DuckDB connection per Struct channel, and DuckDB sizes its worker pool to the core count per instance, so threads scaled as channels x (cores - 1) and could exhaust the host thread limit. All DuckDBState objects now share one process-wide instance (each with its own cursor); the pool defaults to threads=1 and is tunable via modify_duckdb_threads / restore_duckdb_threads.

This fixes #292.

Type of Change

  • Bug fix
  • New feature
  • Documentation update
  • Refactor / code cleanup
  • CI / build configuration
  • Other (describe below)

Checklist

  • Linting passes (make lint)
  • Tests pass (make test)
  • New tests added for new functionality
  • Documentation updated (if applicable)
  • Changelog / version bump (if applicable)

Comment thread csp_gateway/server/gateway/csp/state.py Outdated
# Struct channels (and with the host core count), every DuckDBState shares a single process-wide DuckDB
# instance -- each state operates on its own cursor -- so the pool is created once and its size is a
# single global knob. See https://github.com/Point72/csp-gateway/issues/292
_DUCKDB_THREADS_ORIGINAL = 1

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should probably initialize to something like min(os.cpu_count(), 4) or something

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

State created one DuckDB connection per Struct channel, and DuckDB sizes
its worker pool to the core count per instance, so threads scaled as
channels x (cores - 1) and could exhaust the host thread limit. All
DuckDBState objects now share one process-wide instance (each with its own
cursor); the pool defaults to threads=1 and is tunable via
modify_duckdb_threads / restore_duckdb_threads.

Signed-off-by: Arham Chopra <arham.chopra@cubistsystematic.com>
@github-actions

github-actions Bot commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

Test Results

652 tests   644 ✅  7m 37s ⏱️
  1 suites    8 💤
  1 files      0 ❌

Results for commit e670421.

@codecov

codecov Bot commented Jul 7, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 93.75000% with 6 lines in your changes missing coverage. Please review.
✅ Project coverage is 87.64%. Comparing base (19bd44a) to head (e670421).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
csp_gateway/tests/server/gateway/csp/test_state.py 92.00% 6 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #299      +/-   ##
==========================================
+ Coverage   87.55%   87.64%   +0.09%     
==========================================
  Files         143      143              
  Lines       14849    14943      +94     
==========================================
+ Hits        13001    13097      +96     
+ Misses       1848     1846       -2     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@arhamchopra arhamchopra added the type: feature Feature requests label Jul 7, 2026
@timkpaine timkpaine merged commit dea4c2b into main Jul 7, 2026
13 checks passed
@timkpaine timkpaine deleted the ac/duckdb branch July 7, 2026 17:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type: feature Feature requests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Control the duckdb threads in state API

2 participants