python / python/cpython

Occasional incorrect chaining of CancelledError when calling 'cancel' on result of 'asyncio.gather'

Offen
#97,907 5 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

topic-asyncio type-bug
Vorherrschende Sprache
Python
Sterne
77.2k
Forks
36k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

import asyncio

async def f1():
    await asyncio.sleep(1)

async def f2():
    return 42

async def main():
    # case 1
    gfut = asyncio.gather(f1(), f2(), return_exceptions=True)
    # case 2
    #gfut = asyncio.gather(f2(), f1(), return_exceptions=True)
    await asyncio.sleep(0.1)
    gfut.cancel("my message")
    await gfut

asyncio.run(main())

# case 1

#Traceback (most recent call last):
#  File "/Users/vladima/Sources/gather.py", line 14, in main
#    print(await gfut)
#asyncio.exceptions.CancelledError
#
#During handling of the above exception, another exception occurred:
#
#Traceback (most recent call last):
#  File "/Users/vladima/Sources/gather.py", line 16, in <module>
#    asyncio.run(main())
#  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/asyncio/runners.py", line 44, in run
#    return loop.run_until_complete(main)
#  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/asyncio/base_events.py", line 646, in run_until_complete
#    return future.result()
#asyncio.exceptions.CancelledError

# case 2
#Traceback (most recent call last):
#  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/asyncio/tasks.py", line 605, in sleep
#    return await future
#asyncio.exceptions.CancelledError: my message

#During handling of the above exception, another exception occurred:

#Traceback (most recent call last):
#  File "/Users/vladima/Sources/gather.py", line 14, in main
#    print(await gfut)
#asyncio.exceptions.CancelledError
#
#During handling of the above exception, another exception occurred:
#
#Traceback (most recent call last):
#  File "/Users/vladima/Sources/gather.py", line 16, in <module>
#    asyncio.run(main())
#  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/asyncio/runners.py", line 44, in run
#    return loop.run_until_complete(main)
#  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/asyncio/base_events.py", line 646, in run_until_complete
#    return future.result()
#asyncio.exceptions.CancelledError

Currently traceback and description parts of CancelledError depend upon the order of arguments that were passed into asyncio.gather. The reason I believe is this: here fut would be bound to the last item in children which might or might not correspond to the cancelled task and instead it should probably be one of tasks that were actually cancelled (which is still not ideal since there might be many of them but I guess with gather it is as good as it can be).

Linked PRs
  • gh-133243

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne damit, die beiden asyncio.gather-Fälle im Issue auszuführen, und untersuche dann Lib/asyncio/tasks.py um Zeile 812, wo das Issue die von der Reihenfolge abhängige Auswahl des Future identifiziert. Die Aufgabe ist abgeschlossen, wenn das Verhalten von Abbruch-Traceback und Beschreibung nicht mehr von der Reihenfolge der gather-Argumente abhängt; sieh dir den verknüpften PR gh-133243 an, bevor du beginnst.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
backend
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Klar beschrieben
Anfängerfreundlichkeit
25/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.