Repository navigation
ping() and is_connected report a connection the server dropped - #1777
Conversation
…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.
MilagrosMarin
left a comment
There was a problem hiding this comment.
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.
Fixes #1775.
Since 2.1.0,
Connection.ping()has ignored the adapter's result, sois_connectedstayedTrueafter the server dropped the connection, andrepr(conn)said "connected". This restores the 2.0 behavior and fixes two cases that restoring it alone would break.Changes
Connection.ping()raisesLostConnectionErrorwhen the adapter reports the connection dead.is_connectedandreprthen report a dropped connection as disconnected.PostgreSQLAdapter.ping()reports dead only for a lost connection. A connection inside a failed transaction answers every statement within_failed_sql_transaction(25P02); that answer shows the connection is alive. Without this, any error inside a PostgreSQL transaction madeis_connectedFalsefor good.in_transactionreports the recorded transaction state without pinging:self._in_transaction and not self._is_closed. It no longer clears the transaction flag. Before, onceping()worked, readingin_transactionafter a dropped connection cleared the flag, and the nextquery(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 everypopulate,delete,alterand declaration.use_tls(which reconnects) is refused while a transaction is recorded, even one lost with its connection. It checks the recorded flag rather thanin_transaction, so a lost transaction is not continued on the new connection.Tests
tests/integration/test_connection_liveness.py, on MySQL and PostgreSQL:ping()raises,is_connectedisFalse,reprsays "disconnected";ping()raises;in_transactiondoes not ping;use_tlsis refused after the transaction's connection was found dead;Of the first ten (before the
use_tlscase), 8 fail onmaster. The last one passes onmasterand fails on PostgreSQL with only theConnection.ping()change.Notes
LostConnectionError.master: a manualconnect()after a transaction was lost leaves the transaction recorded, soin_transactionreportsTrueon the new session. Worth a follow-up:connect()could treat a recorded transaction as lost.