Skip to content

bugfix: fix cancelled tasks pending forever#161

Merged
oremanj merged 1 commit into
python-trio:masterfrom
ngtrunghuan:fix-cancelled-tasks-pending-asyncio-futures
May 15, 2026
Merged

bugfix: fix cancelled tasks pending forever#161
oremanj merged 1 commit into
python-trio:masterfrom
ngtrunghuan:fix-cancelled-tasks-pending-asyncio-futures

Conversation

@ngtrunghuan
Copy link
Copy Markdown

@ngtrunghuan ngtrunghuan commented May 14, 2026

When an AsyncHandle is cancelled before the _main_loop is able to dequeue it (often due to heavy CPU load), trio-asyncio currently leaves the underlying asyncio.Future hanging in the PENDING state forever, preventing the cancellation to travel upwards back into the trio loop, leading to us missing task cancellation/completion. Specifically, _main_loop_one greedily drops it at if obj._cancelled: return without running _run, leaving the result_future pending forever. The fix is to always run AsyncHandle._run.

The change includes

  • a new failing test in tests/test_trio_asyncio.py::test_async_handle_cancelled_before_dequeue that forces a cancellation before the _main_loop has a chance to dequeue the pending task.
  • the fix to show that even in such situation, the task finally gets resolved into its rightful "cancelled" state.

@ngtrunghuan ngtrunghuan force-pushed the fix-cancelled-tasks-pending-asyncio-futures branch 4 times, most recently from 1f7f3a2 to 1ffb060 Compare May 14, 2026 04:02
@oremanj
Copy link
Copy Markdown
Member

oremanj commented May 14, 2026

This looks good. Please add a newsfragment (newsfragments/161.bugfix.rst) describing this change for the release notes. I just merged #162 which should fix the CI issues once you rebase.

When an AsyncHandle is cancelled, _main_loop_one drops it at `if obj._cancelled: return`
without running `_run`, leaving the result_future pending forever. The fix is to always
run `AsyncHandle._run`.
@ngtrunghuan ngtrunghuan force-pushed the fix-cancelled-tasks-pending-asyncio-futures branch from 1ffb060 to 0265af1 Compare May 15, 2026 04:38
@ngtrunghuan
Copy link
Copy Markdown
Author

This looks good. Please add a newsfragment (newsfragments/161.bugfix.rst) describing this change for the release notes. I just merged #162 which should fix the CI issues once you rebase.

Thanks Josh - I've done both. Please let me know if the formatting on the newsfragment needs work.

@oremanj oremanj merged commit 0d54b36 into python-trio:master May 15, 2026
25 checks passed
@trio-bot
Copy link
Copy Markdown

trio-bot Bot commented May 15, 2026

Hey @ngtrunghuan, it looks like that was the first time we merged one of your PRs! Thanks so much! 🎉 🎂

If you want to keep contributing, we'd love to have you. So, I just sent you an invitation to join the python-trio organization on Github! If you accept, then here's what will happen:

  • Github will automatically subscribe you to notifications on all our repositories. (But you can unsubscribe again if you don't want the spam.)

  • You'll be able to help us manage issues (add labels, close them, etc.)

  • You'll be able to review and merge other people's pull requests

  • You'll get a [member] badge next to your name when participating in the Trio repos, and you'll have the option of adding your name to our member's page and putting our icon on your Github profile (details)

If you want to read more, here's the relevant section in our contributing guide.

Alternatively, you're free to decline or ignore the invitation. You'll still be able to contribute as much or as little as you like, and I won't hassle you about joining again. But if you ever change your mind, just let us know and we'll send another invitation. We'd love to have you, but more importantly we want you to do whatever's best for you.

If you have any questions, well... I am just a humble Python script, so I probably can't help. But please do post a comment here, or in our chat, or on our forum, whatever's easiest, and someone will help you out!

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants