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

未关闭 适合新手
#1,775 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 5 天内回复

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
76/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
mysql, postgresql, python
领域
backend, database

调研方向

从 Connection.ping() 和 src/datajoint/connection.py 开始,检查 MySQL 和 PostgreSQL 适配器的 ping() 方法。检查 is_connected 如何处理 ping 失败,并查找可扩展的现有连接测试;issue 没有指定测试文件。完成标准是:连接断开时,ping() 会抛出异常,且 is_connected 返回 False,包括 PostgreSQL 的情况。

由索引模型根据 Issue 内容生成。

描述

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.
主要语言
Python
星标
198
派生
107
平均合并
4 天 7 小时
30 天内合并 PR
12

环境准备

  • 提供 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

datajoint/datajoint-python 的其他 Issue

查看 datajoint/datajoint-python 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。