Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

asyncio.timeout(0) swallows a prior task cancellation #134471

Open
Labels
stdlibPython modules in the Lib dirtopic-asynciotype-bugAn unexpected behavior, bug, or error
@okhaliavka

Description

@okhaliavka

Bug report

Bug description:

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

importasyncioasyncdeftest(timeout):task=asyncio.current_task()task.cancel()try:asyncwithasyncio.timeout(timeout):awaitasyncio.sleep(1)exceptTimeoutError:print("timeout caught")exceptasyncio.CancelledError:print("CancelledError caught")raiseawaitasyncio.sleep(2)print("not cancelled")asyncio.run(test(timeout=0))# prints "timeout caught" and "not_cancelled"

This code printstimeout caught andnot_cancelled, which means thatasyncio.timeout(0) swallows the precedingtask.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 printCancelledError 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 passesCancelledError through.

Possible solution

I believe this behavior was unintentedly introduced in#102815, where the logic in__aexit__ was changed to only considernew cancel requests when deciding if it should raise aTimeoutError. That was necessary forTimeout to work correctly if used while handling aCancelledError, 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 thatCancelledError was supposed to be delivered andTimeout shouldn't mess with that attempt.

Draft PR to illustrate the idea:#134472

Additional context

I've hit this issue in production withredis-py, whichusesasyncio.timeout(0) internally in the connection parser:

# reduced for brevityfrom redis.asyncio import Redisasync 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibPython modules in the Lib dirtopic-asynciotype-bugAn unexpected behavior, bug, or error

    Projects

    Status

    Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp