Skip to content

gh-150058: asyncio: preserve Task.cancel() msg through fut_waiter and CancelledError paths#150057

Open
prince8273 wants to merge 3 commits into
python:mainfrom
prince8273:fix-task-cancel-msg-propagation
Open

gh-150058: asyncio: preserve Task.cancel() msg through fut_waiter and CancelledError paths#150057
prince8273 wants to merge 3 commits into
python:mainfrom
prince8273:fix-task-cancel-msg-propagation

Conversation

@prince8273
Copy link
Copy Markdown

@prince8273 prince8273 commented May 19, 2026

When Task.cancel(msg=...) is called while the task is waiting on another
future, the message was silently discarded in two places:

  1. cancel(): the fast path (_fut_waiter.cancel(msg) returns True)
    returned immediately without storing msg in _cancel_message.

  2. __step_run_and_handle_result(): the CancelledError handler called
    future_cancel(..., NULL), dropping _cancel_message even when set.

Fix both in tasks.py (_PyTask) and _asynciomodule.c (Task).

Also includes:

  • Regression test in Lib/test/test_asyncio/test_tasks.py:
    test_task_cancel_waiter_future_with_message covers the full
    round-trip — cancel with msg while awaiting a future, verify the
    message reaches the final CancelledError.
  • What's New entry in Doc/whatsnew/3.16.rst.

@python-cla-bot
Copy link
Copy Markdown

python-cla-bot Bot commented May 19, 2026

All commit authors signed the Contributor License Agreement.

CLA signed

@bedevere-app
Copy link
Copy Markdown

bedevere-app Bot commented May 19, 2026

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@prince8273 prince8273 changed the title asyncio: preserve Task.cancel() msg through fut_waiter and CancelledError paths gh-150058: asyncio: preserve Task.cancel() msg through fut_waiter and CancelledError paths May 19, 2026
@bedevere-app
Copy link
Copy Markdown

bedevere-app Bot commented May 19, 2026

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@prince8273 prince8273 force-pushed the fix-task-cancel-msg-propagation branch from 2807a14 to 8b1d2b2 Compare May 19, 2026 06:10
@read-the-docs-community
Copy link
Copy Markdown

Documentation build overview

📚 cpython-previews | 🛠️ Build #32752233 | 📁 Comparing 3d3f306 against main (517d3d2)

  🔍 Preview build  

2 files changed
± whatsnew/3.16.html
± whatsnew/changelog.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant