Skip to content

getUpdates 长轮询在 NoneBot2 v2.5.0 下容易触发 30s read timeout #87

@shoucandanghehe

Description

@shoucandanghehe

问题描述

在 NoneBot2 v2.5.0 + fastapi+aiohttp driver 下,adapter-telegram 的 polling 会调用:

updates = await bot.get_updates(offset=update_offset, timeout=30)

这里的 timeout=30 是 Telegram Bot API getUpdates long polling 的服务端等待时间。

当前 _call_api 构造 Request 时没有显式设置 driver timeout。NoneBot2 v2.5.0 起,未传 Request.timeout 时会使用默认:

Timeout(total=None, connect=5.0, read=30.0, close=10.0)

因此客户端 sock_read=30s 与 Telegram long polling 的服务端等待时间同为 30s。实际运行中,网络稍有抖动,或服务端接近 30s 才返回时,客户端可能先触发 aiohttp read timeout,导致一次正常的长轮询被记录为请求失败。

实际表现

日志中会出现类似错误:

Telegram | Get updates for bot ... failed
aiohttp.client_exceptions.SocketTimeoutError: Timeout on reading data from socket
...
nonebot.adapters.telegram.exception.NetworkError: <NetWorkError message=HTTP request failed>

栈路径大致为:

nonebot.adapters.telegram.adapter.poll
 -> bot.get_updates(offset=..., timeout=30)
 -> nonebot.adapters.telegram.adapter._call_api
 -> nonebot.drivers.aiohttp.Session.request
 -> aiohttp.client_exceptions.SocketTimeoutError
 -> nonebot.adapters.telegram.exception.NetworkError

当前最新分支状态

已确认当前默认分支 beta 的最新提交中仍存在该行为:

  • poll() 中仍固定调用 get_updates(..., timeout=30)
  • _call_api() 中构造的 Request 仍未设置 driver timeout

环境

  • nonebot2: v2.5.0
  • nonebot-adapter-telegram: 0.1.0b20
  • driver: fastapi+aiohttp
  • runtime: CPython 3.14

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions