Skip to content

ping() and is_connected report a connection the server dropped - #1777

Merged
dimitri-yatsenko merged 3 commits into
masterfrom
fix/1775-ping-dead-connection
Dec 1, 2026
Merged

dimitri-yatsenko merged 3 commits into
masterfrom
fix/1775-ping-dead-connection

Conversation

@dimitri-yatsenko

Copy link
Copy Markdown
Member

Fixes #1775.

Since 2.1.0, Connection.ping() has ignored the adapter's result, so is_connected stayed True after the server dropped the connection, and repr(conn) said "connected". This restores the 2.0 behavior and fixes two cases that restoring it alone would break.

Changes

  • Connection.ping() raises LostConnectionError when the adapter reports the connection dead. is_connected and repr then report a dropped connection as disconnected.
  • PostgreSQLAdapter.ping() reports dead only for a lost connection. A connection inside a failed transaction answers every statement with in_failed_sql_transaction (25P02); that answer shows the connection is alive. Without this, any error inside a PostgreSQL transaction made is_connected False for good.
  • in_transaction reports the recorded transaction state without pinging: self._in_transaction and not self._is_closed. It no longer clears the transaction flag. Before, once ping() worked, reading in_transaction after a dropped connection cleared the flag, and the next query(reconnect=True) reconnected and ran in autocommit instead of raising "Connection was lost during a transaction". It also no longer costs a round trip on every populate, delete, alter and declaration.
  • Changing use_tls (which reconnects) is refused while a transaction is recorded, even one lost with its connection. It checks the recorded flag rather than in_transaction, so a lost transaction is not continued on the new connection.

Tests

tests/integration/test_connection_liveness.py, on MySQL and PostgreSQL:

  • a connection the server dropped: ping() raises, is_connected is False, repr says "disconnected";
  • a closed connection: ping() raises;
  • in_transaction does not ping;
  • a transaction lost with its connection is not continued after a reconnect;
  • changing use_tls is refused after the transaction's connection was found dead;
  • an error inside a transaction leaves the connection alive and the transaction open until it is cancelled.

Of the first ten (before the use_tls case), 8 fail on master. The last one passes on master and fails on PostgreSQL with only the Connection.ping() change.

Notes

…1775)

Connection.ping() raises LostConnectionError when the adapter reports the
connection dead, as before 2.1.0. The PostgreSQL adapter counts a connection
in a failed transaction (25P02) as alive. in_transaction no longer pings or
clears the transaction flag, so a transaction lost with its connection still
raises at the next query, and changing use_tls checks the recorded flag.
@dimitri-yatsenko dimitri-yatsenko added this to the v2.3.7 milestone Dec 1, 2026
@dimitri-yatsenko dimitri-yatsenko added the bug Indicates an unexpected problem or unintended behavior label Dec 1, 2026

@MilagrosMarin MilagrosMarin left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks @dimitri-yatsenko, this is a clean fix with good coverage of the edge cases restoring ping() alone would have broken.

  • CI is green across MySQL 8.0/8.4 and PostgreSQL 15, and #1734 is already in the branch.
  • I traced the in_transaction callers (autopopulate.py, table.py): after a dropped connection each one reaches start_transaction() or query(), which raises "Connection was lost during a transaction" instead of continuing in autocommit, so the safety goal holds.

One small inconsistency: in_transaction returns self._in_transaction and not self._is_closed, and is_connected sets _is_closed = True when the ping fails. So once anything calls is_connected (including repr), in_transaction reports False while the recorded flag is still True. Nothing unsafe follows, since query() and _ensure_tls read _in_transaction directly, but the description says in_transaction "reports the recorded transaction state", and test_transaction_lost_with_connection_is_not_continued only passes because it reads in_transaction before is_connected. Either return self._in_transaction alone, or reword the description and the in_transaction docstring to say a connection found dead reports no transaction. Not blocking.

Approving.

@dimitri-yatsenko
dimitri-yatsenko merged commit d5391a2 into master Dec 1, 2026
25 checks passed
@dimitri-yatsenko
dimitri-yatsenko deleted the fix/1775-ping-dead-connection branch December 1, 2026 19:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Indicates an unexpected problem or unintended behavior

Projects

None yet

Development

Successfully merging this pull request may close these issues.

is_connected and ping() report a dead connection as live (since 2.1.0)

2 participants