python / python/cpython

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

Aperta
#134,471 8 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

stdlib topic-asyncio type-bug
Lingua principale
Python
Stelle
77.2k
Fork
35.9k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia da asyncio.Timeout.aexit e dal relativo gestore _on_timeout, quindi esamina la bozza della PR #134472. Riproduci gli esempi con timeout pari a zero e con timeout positivo; il lavoro è completato quando una cancellazione precedente si propaga come CancelledError, mentre la gestione di un timeout effettivo continua a sollevare TimeoutError, con copertura di regressione.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
backend
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Specificata chiaramente
Idoneità per principianti
30/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.