Skip to content

fix: retry a raw retriable asyncpg error, so connect-time failures retry on every sqlalchemy - #58

Merged
lesnik512 merged 1 commit into
mainfrom
fix/retry-raw-connect-errors
Sep 25, 2026
Merged

lesnik512 merged 1 commit into
mainfrom
fix/retry-raw-connect-errors

Conversation

@lesnik512

Copy link
Copy Markdown
Member

Replaces #57, which GitHub closed when its stacked base (#56) merged.

Problem

_is_retriable_link only recognised a retriable asyncpg error wrapped in a DBAPIError. Errors raised while SQLAlchemy is executing a statement always arrive wrapped. Errors raised by an async_creator at connect time arrive differently depending on the SQLAlchemy version:

Factory raises SQLAlchemy 2.0.54 SQLAlchemy 2.1.0
asyncpg.PostgresConnectionError (class 08) raw, not retried DBAPIError, retried

So the same db-retry release retried a connect-time lost connection on 2.1 and gave up on 2.0. The 2.1 behaviour is the one CONTEXT.md describes ("a lost connection (class 08)" is retriable, and RETRIABLE_ASYNCPG_ERRORS "is the whole of it"). The 2.0 gap came from the wrapper check, not from a deliberate rule.

Change

A chain link is now retriable when the asyncpg error it carries, either the raw exception or a DBAPIError's orig.__cause__, is in RETRIABLE_ASYNCPG_ERRORS. It is still one isinstance against the tuple, as ADR 0002 describes, and the 40003 invariant is unchanged; a raw StatementCompletionUnknownError case pins it.

Behaviour change for SQLAlchemy 2.0 users: under postgres_retry, a connect-time PostgresConnectionError (and subclasses) is now retried. A raw SerializationError or PostgresConnectionError raised anywhere in the chain, including from direct asyncpg use inside a decorated coroutine, is now retried too.

Tests

  • test_is_retriable: raw 40001 and 08000 are retriable; raw 40003 and a raw base PostgresError are not.
  • test_a_connect_time_connection_error_is_retried: an async_creator that raises PostgresConnectionError is called once per attempt under postgres_retry.

Before the fix, the raw-error cases failed on both versions and the connect-time test failed on 2.0.54 only. After it, the full suite passes against Postgres at 100% coverage on sqlalchemy 2.0.54 and 2.1.0 (versions confirmed from inside the test interpreter); just lint-ci green.

@lesnik512
lesnik512 merged commit 05d8516 into main Sep 25, 2026
12 checks passed
@lesnik512
lesnik512 deleted the fix/retry-raw-connect-errors branch September 25, 2026 13:35
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