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

Aperta Adatta ai principianti
#1,775 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 5 giorni

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
76/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
mysql, postgresql, python
Ambito
backend, database

Direzione di ricerca

Inizia da Connection.ping() in src/datajoint/connection.py ed esamina i metodi ping() degli adattatori MySQL e PostgreSQL. Verifica come is_connected gestisce gli errori di ping e cerca test di connessione esistenti da estendere; l’issue non indica alcun file di test. Il lavoro è completato quando una connessione interrotta fa sì che ping() sollevi un’eccezione e che is_connected restituisca False, anche nel caso di PostgreSQL.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

bug triage

Bug Report

Description

Connection.ping() no longer raises when the connection is dead, so Connection.is_connected stays True after the server kills the connection or stops. It only turns False after close(). We check is_connected in a health endpoint, and it reported a stopped server as connected.

Connection.ping() calls self.adapter.ping(self._conn) and discards the result. MySQLAdapter.ping() catches every exception and returns False, so ping() never raises, and is_connected never reaches its except branch.

Reproducibility

Include:

  • OS: Linux (Ubuntu on WSL2)
  • Python 3.12.13
  • MySQL 8.4.11
  • local-docker
  • DataJoint 2.3.6 (the code is unchanged on master)
Steps:
import datajoint as dj
import pymysql

conn = dj.conn()
config = dj.config
admin = pymysql.connect(host=config['database.host'], port=config['database.port'],
                        user=config['database.user'], password=config['database.password'])
with admin.cursor() as cursor:
    cursor.execute(f'KILL {conn.connection_id}')

print(conn.adapter.ping(conn._conn))  # False
print(conn.is_connected)              # True
conn.ping()                           # does not raise
print(conn)                           # DataJoint connection (connected) root@...
Expected Behavior

ping() raises and is_connected returns False once the server has dropped the connection, as in 0.14.4 and 2.0.0, where ping() called self._conn.ping(reconnect=False) directly.

Additional Research and Context
  • Introduced in 2.1.0, when ping() started going through the backend adapters.
  • Connection.ping() ignores the adapter's result: https://github.com/datajoint/datajoint-python/blob/v2.3.6/src/datajoint/connection.py#L392-L401
  • The adapters return False instead of raising: MySQL, PostgreSQL (the PostgreSQL case is not tested).
  • repr (L251) and in_transaction (L531) read is_connected, so they are wrong in the same way.
  • #1734 relies on is_connected noticing a dead connection ("Closed by close(), or found dead by is_connected").
  • Possible fix: in Connection.ping(), raise errors.LostConnectionError when self.adapter.ping(self._conn) returns False.
Lingua principale
Python
Stelle
198
Fork
107
Merge medio
4g 7h
PR unite (30g)
12

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di datajoint/datajoint-python

Tutte le issue di datajoint/datajoint-python

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.