python / python/cpython

`asyncio.timeout(0)` swallows a prior task cancellation

未关闭
#134,471 8 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

stdlib topic-asyncio type-bug
主要语言
Python
星标
77.2k
派生
35.9k
PR 合并指标
PR 指标待抓取

描述

Bug report

Bug description:

async with asyncio.timeout(0) will catch and process a prior unrelated cancellation of the enclosing task.

import asyncio

async def test(timeout):
    task = asyncio.current_task()
    task.cancel()

    try:
        async with asyncio.timeout(timeout):
            await asyncio.sleep(1)
    except TimeoutError:
        print("timeout caught")
    except asyncio.CancelledError:
        print("CancelledError caught")
        raise

    await asyncio.sleep(2)
    print("not cancelled")

asyncio.run(test(timeout=0))  # prints "timeout caught" and "not_cancelled"

This code prints timeout caught and not_cancelled, which means that asyncio.timeout(0) swallows the preceding task.cancel(), so the task pretty much ignores cancellation and proceeds.

Notably, that doesn't happen with a positive timeout. In the above example, asyncio.run(test(timeout=0.001)) will only print CancelledError caught, which I believe is the correct behavior.
In this case, asyncio.Timeout's internal timeout handler (_on_timeout) never gets to run, so the context manager doesn't interfere at all - it doesn't cancel/uncancel the task and simply passes CancelledError through.

Possible solution

I believe this behavior was unintentedly introduced in #102815, where the logic in __aexit__ was changed to only consider new cancel requests when deciding if it should raise a TimeoutError. That was necessary for Timeout to work correctly if used while handling a CancelledError, but now it can get confused between "internal" and "external" cancellations that happened on the same loop iteration.

I think we could capture task's _must_cancel flag when entering the context manager. If it was set, it means that CancelledError was supposed to be delivered and Timeout shouldn't mess with that attempt.

Draft PR to illustrate the idea: https://github.com/python/cpython/pull/134472

Additional context

I've hit this issue in production with redis-py, which uses asyncio.timeout(0) internally in the connection parser:

# reduced for brevity
from redis.asyncio import Redis

async def test(timeout):
    redis: Redis = await get_redis()
    task = asyncio.current_task()
    task.cancel()
    await redis.set("foo", "bar")
    assert False, "not cancelled"
CPython versions tested on:

3.12, 3.13, CPython main branch, 3.14, 3.11

Operating systems tested on:

macOS

Linked PRs
  • gh-134472

贡献指南

打开贡献指南

从这里开始

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

调研方向

先从 asyncio.Timeout.aexit 及其 _on_timeout 处理程序开始,然后查看 PR #134472 的草稿。复现零超时和正超时的示例;完成标准是:先前的取消会以 CancelledError 传播,而真正的超时处理仍会引发 TimeoutError,并包含回归测试覆盖。

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

评估

技术栈
python
领域
backend
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
描述清楚
新手友好度
30/100

把新 issue 发到你的邮箱

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