Skip to content

fix(kook): measure the heartbeat interval with a monotonic clock - #11645

Open
Luckyou-d wants to merge 1 commit into
AstrBotDevs:masterfrom
Luckyou-d:fix/kook-heartbeat-monotonic
Open

Luckyou-d wants to merge 1 commit into
AstrBotDevs:masterfrom
Luckyou-d:fix/kook-heartbeat-monotonic

Conversation

@Luckyou-d

@Luckyou-d Luckyou-d commented Dec 4, 2026 •

Copy link
Copy Markdown

问题

KOOK 适配器的心跳看门狗用墙钟判断超时(修复前是 time.time()):

if time.time() - self.last_heartbeat_time > self.config.heartbeat_timeout:
    self.heartbeat_failed_count += 1

宿主校时(NTP 或人工改时间)前跳时,这段时间差会被算成「很久没收到 PONG」→ 失败计数自增,累加到 max_heartbeat_failures 就判定连接已死、断开重连;后跳则相反,真的丢 PONG 也发现不了。PONG 到达与这次检查之间隔着 await self._send_ping() 和 await asyncio.sleep(...),正是「两次读数之间跨 IO」的典型形状。

改动

last_heartbeat_time 的写入与比较都改用 time.monotonic();字段初始化也从 0 改成 time.monotonic(),这样第一次检查是从连接建立开始计时,而不是从纪元开始(原写法第一次检查必然超时)。

验证

  • ruff check 与 ruff format --check 通过(仅该文件)
  • 没有加单元测试:这个缺陷需要宿主时钟在两次读数之间跳变才能触发,不替换被测代码读取的时钟就制造不出来;按仓库「诚实优先」的原则写明原因,而不是写一个两种实现都会通过的空测试
  • 未跑 KOOK 端到端(需要真实 KOOK 平台凭据)

The heartbeat watchdog compares the time of the last PONG against the wall clock, so a host that
steps forward counts a missed heartbeat that was answered, and after max_heartbeat_failures the
connection is dropped. monotonic() only moves forward; the field starts at monotonic() so the
first interval is measured from connect instead of from the epoch.

@sourcery-ai sourcery-ai Bot 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.

Hey - I've reviewed your changes and they look great!

Sourcery assessment

Approved.


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

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