Skip to content

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

Closed
prince8273 wants to merge 4 commits into
python:mainfrom
prince8273:fix-task-cancel-msg-propagation
Closed

gh-150058: asyncio: preserve Task.cancel() msg through fut_waiter and CancelledError paths#150057
prince8273 wants to merge 4 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

read-the-docs-community Bot commented May 19, 2026

Documentation build overview

📚 cpython-previews | 🛠️ Build #32756060 | 📁 Comparing 8f63ba1 against main (517d3d2)

  🔍 Preview build  

4 files changed
± library/shutil.html
± library/zipfile.html
± whatsnew/3.16.html
± whatsnew/changelog.html

@picnixz
Copy link
Copy Markdown
Member

picnixz commented May 19, 2026

Please stop updating your branch, it wastes CI resources. Read the devguide to understand our guidelines.

@prince8273 prince8273 closed this May 19, 2026
@prince8273
Copy link
Copy Markdown
Author

Closing this PR. After testing on the CPython main branch locally, the cancel message is correctly preserved in CancelledError.args. The bug I reported does not reproduce — the fix is not needed. Apologies for the noise.

@prince8273 prince8273 deleted the fix-task-cancel-msg-propagation branch May 19, 2026 15:21
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.

2 participants