Skip to content

asyncio.Task.cancel() silently drops msg when cancelling via a waiter future #150058

@prince8273

Description

@prince8273

Bug report

Bug description:

pythonimport asyncio

async def main():
async def worker():
await asyncio.sleep(10)

task = asyncio.create_task(worker())
await asyncio.sleep(0)  # let task start and reach the await
task.cancel(msg='timed out waiting for GPU')
try:
    await task
except asyncio.CancelledError:
    print(task._cancel_message)  # prints None, should print the msg

asyncio.run(main())
When Task.cancel(msg=...) is called while the task is waiting on another future, the message is silently discarded. Two causes:

cancel() fast path returns True without storing msg in _cancel_message
__step_run_and_handle_result() calls future_cancel(..., NULL) dropping _cancel_message

CPython versions tested on: main
Operating systems: Windows, Linux

CPython versions tested on:

CPython main branch

Operating systems tested on:

Windows

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions