Skip to content

Reconnect the Python async client's owned connection after an interruption - #141

Open
Kludex wants to merge 7 commits into
earendil-works:mainfrom
Kludex:python-async-reconnect
Open

Kludex wants to merge 7 commits into
earendil-works:mainfrom
Kludex:python-async-reconnect

Conversation

@Kludex

@Kludex Kludex commented Aug 13, 2026

Copy link
Copy Markdown

AsyncAbsurd._ensure_connected only connects when _conn is None, and nothing ever resets _conn, so an owned connection that dies underneath the client (idle timeout, server restart, network blip) is terminal: every later call raises psycopg.OperationalError: the connection is closed until the process restarts. We hit this in production with a long-lived client whose connection sits idle between scheduler ticks; one dropped connection permanently broke every subsequent spawn.

This makes _ensure_connected treat an owned connection whose broken flag is set as reconnectable. psycopg's broken is true only for connections that terminated abnormally, so:

  • an explicitly close()d client stays closed (no surprise resurrection),
  • an injected connection (_owned_conn = False) is never replaced, since the caller owns its lifecycle.

The call that discovers the dead connection still raises (psycopg only marks the connection broken when an operation fails on it); the next call reconnects instead of failing forever.

The sync client connects eagerly in __init__ and doesn't keep the conn string, so it isn't covered here; happy to follow up on it (and the TS SDK, which looks similar) if you want the same semantics there.

AI Disclaimer

This PR was developed with the assistance of either Claude or Codex. I've reviewed and verified the changes.

This branch has not been deployed

No deployments
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