Skip to content

Return cancellable handles for postponed callbacks - #2171

Closed
hakarrr wants to merge 2 commits into
prompt-toolkit:mainfrom
hakarrr:fix-postponed-callback-cancellation
Closed

hakarrr wants to merge 2 commits into
prompt-toolkit:mainfrom
hakarrr:fix-postponed-callback-cancellation

Conversation

@hakarrr

@hakarrr hakarrr commented Nov 10, 2026

Copy link
Copy Markdown

Fixes #1012.

Return a cancellable asyncio.Handle from call_soon_threadsafe without changing its synchronous calling convention. With no postponement, return the event loop's handle directly. With postponement, keep one cancellation token across scheduling retries and check it before running or rescheduling the callback.

This lets callers cancel after the first postponement, rather than only cancelling the first scheduled attempt. Existing callers that ignore the return value keep the same behavior. Cancellation does not interrupt a callback that has already started. This does not change Application redraw lifecycle behavior.

The tests cover cancellation before dispatch and after a retry, idle and deadline dispatch, loops without _ready, context propagation, callback errors reaching the loop's exception handler, scheduling from another thread, and the implicit running loop.

Validation on Linux:

  • Before the change, the initial regression harness reported 3 failed / 3 passed because the helper returned None.
  • After the change, all 13 focused tests pass.
  • Full suite: 169 passed on each of Python 3.10, 3.11, 3.12, 3.13, 3.14 and free-threaded 3.14.
  • Actual uvloop cancellation and callback delivery check passed on Python 3.13.
  • Whole-tree Ruff check and format check passed.
  • Strict mypy passed with linux, win32 and darwin platform settings. These are type checks, not native Windows or macOS test runs.
  • Re-fetched the published branch and checked that both files exactly match the tested versions, then reran the full Python 3.13 suite and Ruff checks on that remote tree.

hakarrr and others added 2 commits November 11, 2026 00:24
Co-authored-by: generaltony007 <144252170+GeneralTony007@users.noreply.github.com>
Co-authored-by: generaltony007 <144252170+GeneralTony007@users.noreply.github.com>
@hakarrr

hakarrr commented Nov 13, 2026

Copy link
Copy Markdown
Author

Could a maintainer review this change to return cancellable handles for postponed callbacks? Please let me know whether this API is in scope or if changes or further cases are needed before review. Thanks.

@hakarrr hakarrr closed this Nov 16, 2026
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.

Improve call_soon_threadsafe (with max_postpone_timeout) to provide good cancellation.

1 participant