feat(db): configurable connection pool tuning for MySQL / PostgreSQL#72
Merged
shirshanka merged 3 commits intoJun 21, 2026
Merged
Conversation
Without pool_pre_ping=True, asyncpg leaves cancelled-task connections in a broken state inside the pool, causing the next checkout to hang indefinitely under concurrent load. Expose the standard SQLAlchemy pool knobs (size / max_overflow / recycle / pre_ping / timeout) via env vars with sensible defaults, applied to non-SQLite engines only. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Backs the unit-test claim in the PR description: - Settings defaults and env-var override (int + bool coercion) - _get_engine omits pool kwargs for SQLite, applies all five for MySQL / PostgreSQL Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
…ed connections pool_pre_ping issues SELECT 1 on checkout, but on an asyncpg connection wedged by task cancellation that ping can itself hang forever without a per-statement timeout. Add DB_COMMAND_TIMEOUT (default 30s, 0 disables), applied via connect_args for asyncpg only — SQLite and MySQL are untouched. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
shirshanka
approved these changes
Jun 21, 2026
shirshanka
left a comment
Contributor
There was a problem hiding this comment.
Thanks for the contribution! Made a few small tweaks - before approving.
shirshanka
approved these changes
Jun 27, 2026
shirshanka
left a comment
Contributor
There was a problem hiding this comment.
Approving. Core change is correct and well-scoped — pydantic coerces the int/bool pool env vars properly, the SQLite path is preserved unchanged, and AsyncAdaptedQueuePool (default for async MySQL/PostgreSQL) accepts all five kwargs.
Two follow-up commits were pushed to this branch (maintainer edits):
- Unit tests for the pool settings +
_get_enginewiring (SQLite omits pool kwargs; MySQL/Postgres apply them). DB_COMMAND_TIMEOUT(asyncpg-only, default 30s, 0 disables) applied via connect_args, sopool_pre_ping's SELECT 1 on a connection wedged by task cancellation fails fast instead of hanging forever — making the hang fix robust rather than probabilistic.
ruff check/format + mypy clean; 17 unit tests pass.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
Without
pool_pre_ping=True,asyncpgleaves connections that wereinterrupted by task cancellation (CancelledError) in a half-broken
state inside the SQLAlchemy pool. The next checkout of such a
connection hangs indefinitely, which under concurrent load presents
as the whole app stalling — even though no obvious error surfaces.
The default
AsyncAdaptedQueuePool(pool_size=5, max_overflow=10,pool_timeout=30s) is also too small for production deployments and
provides no recycle behavior to survive managed-PG idle timeouts.
What
Expose the standard SQLAlchemy pool knobs via env vars, applied to
non-SQLite engines only:
DB_POOL_SIZEDB_MAX_OVERFLOWDB_POOL_PRE_PINGDB_POOL_TIMEOUTSQLite path is unchanged.
Test
agent execution no longer occurs after enabling
pool_pre_ping