Repository navigation
feat: bound statements with a default command_timeout - #86
Merged
Merged
Conversation
A statement on a pooled connection to a primary that stops answering hung until TCP keepalive gave up. The keyword bounds each statement; a command_timeout in the DSN query wins. Default stays None.
The bound is now on by default. None turns it off; a command_timeout in the DSN still wins. Adds ADR 0003.
Before 0.32, close() after a command_timeout waits with no bound for a cancel a frozen server never answers, so the caller still hangs.
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.
Closes #85.
build_connection_factoryused to bound only the connect. A statement on a pooled connection to a primary that stopped answering (a paused VM, a partition that drops packets) waited for TCP keepalive, which can take hours, so failover never reached the caller.The factory now takes a keyword-only
command_timeout: float | None = 60.0and hands it to asyncpg. Acommand_timeoutin the DSN query wins over the keyword, andNoneturns the bound off.Behaviour change
This is on by default. Any statement that runs longer than 60 seconds now raises
TimeoutError, andpostgres_retrydoes not retry it, because the statement may have completed. Engines that run migrations or long reports need a higher value orcommand_timeout=None. WithNone, a frozen primary hangs the statement as before.The asyncpg floor rises from 0.28 to 0.32. On asyncpg 0.30 and 0.31 the bound fires, but SQLAlchemy then discards the connection through asyncpg's
close(), which waits with no bound for the server to acknowledge the cancel. A frozen primary never does, so the caller still hangs, only one step later. The floors CI jobs caught this on the first push (3.13 on 0.30, 3.14 and 3.14t on 0.31), and it reproduces locally with the hang lasting past 30s. asyncpg 0.32 puts the cancel wait and the disconnect under one deadline.Changelog note:
build_connection_factorynow bounds each statement at 60s by default (command_timeout). Pass a larger value orNonefor long-running statements; acommand_timeoutin the DSN still takes precedence. db-retry now requiresasyncpg>=0.32.Why a default and not opt-in
An opt-in keyword defaulting to
Nonewas the other option. It changes nothing for existing users, but the people it misses are the ones who never read the failover docs, and they are exactly the ones who hang. faststream-outbox and faststream-redis-timers also put a default bound inside the library, using their lease. db-retry has no lease to borrow, so 60s is a chosen number: well above ordinary OLTP statements, and a minute instead of hours on a frozen primary. The reasoning is recorded indocs/adr/0003-statements-get-a-default-bound.md.Tests
tests/test_command_timeout.pyputs a small TCP proxy in front of Postgres. Itsfreeze()stops forwarding on the connections already open but lets new ones through, which is how a paused primary looks to a pool. Withcommand_timeout=0.5, the statement on the frozen pooled connection raisesTimeoutErrorand the next statement gets a fresh connection and succeeds. Withcommand_timeout=None, the same test hits its 5s outer budget, which is the hang.asyncpg.connectchecks the precedence for single-host and multi-host DSNs, and that leaving the keyword out sends 60.0.just test --cov=.: 73 passed, 100% coverage. Ruff,uvx ty@latest checkandjust adr-checkpass.Failover drill
tests_drnow passescommand_timeout=3as the keyword instead of the DSN param. Twojust test-drruns:command_timeout=3TimeoutErrorWith the 60s default and no keyword, the pause drill would recover after about a minute instead of hanging.