Skip to content

Pyjoyment, Pyjo.Promise: async actions and their requests are cancelled when the connection closes - #611

Merged
dex4er merged 4 commits into
mainfrom
cancel-actions
Nov 12, 2026
Merged

dex4er merged 4 commits into
mainfrom
cancel-actions

Conversation

@dex4er

@dex4er dex4er commented Nov 12, 2026 •

Copy link
Copy Markdown
Collaborator

The first step of #266: an async def action whose connection closes before its response ends is cancelled, with the request of the user agent that it waits for. The next steps of #266 come as pull requests stacked on this one.

Changes

  • Pyjo.Reactor.run_coroutine returns a function that cancels the coroutine where it waits, and cancels the promise that it waits for; None for a reactor that cannot.
    • The base class, which Pyjo.Reactor.Poll, gevent and Twisted run, throws Pyjo.Reactor.CancelledError, a BaseException, as except Exception does not catch asyncio.CancelledError either.
    • Pyjo.Reactor.Asyncio cancels the task, and its awaitable cancels the promise when the wait of the task is cancelled; uvloop and Winloop inherit it.
  • Pyjo.Promise:
    • cancel= gives a promise the function of its cancellation, and cancel() calls it once, for a pending promise, which is then not reported as an unhandled rejected promise.
    • The promise of a coroutine cancels the coroutine.
  • Pyjo.UserAgent.start_p, and so get_p and the others: cancel() closes the connection of the request, also of the one after a redirect, found by its callback, and the promise is rejected with Request cancelled.
  • Pyjoyment: an action that is a coroutine function is cancelled when the transaction finishes before its response does, as when the client goes away or the inactivity timeout passes, with # Unlike Mojolicious:, whose async sub runs to its end. The cancellation renders no exception page; an action that has rendered its response goes on to its end.
  • Docs: the cookbook (async/await), the skills async and distributions.

Reactors of the distributions

  • Uvloop, gevent and Twisted pass t/Pyjoyment/cancel_lite_app.py and the tests of promises and of the user agent.
  • run_coroutine of Pyjo-Reactor-Trio returns None, so the action runs to its end there, as before. cancel_lite_app.py skips on a reactor that cannot cancel, so that the daily suite of Trio stays green, until Trio cancels its task with a cancel scope, in Pyjo.Reactor.Trio: run_coroutine returns a function that cancels the task Pyjo-Reactor-Trio#17, which tests against this branch.

Validation

  • The suite on asyncio and on Pyjo.Reactor.Poll with PYTHONDEVMODE=1, without a ResourceWarning or an unhandled rejected promise
  • t/servers, Sphinx with -W, trunk check
  • t/Pyjoyment/cancel_lite_app.py: a client that gives up while the action waits for a request, a stream of Server-Sent Events whose client has gone, an action that goes on after its response, and a request cancelled after a redirect
  • The cancellation in t/stdlib/Promise.py on both reactors: finally runs and except Exception does not catch it, the promise that the coroutine waits for and a coroutine in it are cancelled, a coroutine that goes on after it, and a cancellation before the first step without warnings

Part of #266

🤖 Generated with Claude Code

dex4er and others added 2 commits November 12, 2026 16:10
…tines and of requests

run_coroutine returns a function that cancels the coroutine where it waits,
with CancelledError of the base class or asyncio.CancelledError of the task of
Pyjo.Reactor.Asyncio, and cancels the promise that it waits for. A promise
takes the function of its cancellation with cancel=, and cancel() calls it
once, as nothing waits for the promise anymore, which then is not reported as
an unhandled rejected promise; the one of a coroutine cancels it, and the one
of start_p closes the connection of its request, also after a redirect, and
is rejected with "Request cancelled".

Part of #266

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ts connection closes before its response ends

Unlike Mojolicious, whose async sub runs to its end with the requests that it
waits for: the coroutine gets the cancellation where it waits, its finally
blocks run, and the request of the user agent that it waits for closes its
connection, so that a request to a slow service, as a model of language,
whose answer nobody reads anymore, stops too. An action that has rendered its
response goes on, and the cancellation renders no exception page.

Part of #266

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@read-the-docs-community

read-the-docs-community Bot commented Nov 12, 2026 •

Copy link
Copy Markdown

Documentation build overview

📚 Pyjoyment | 🛠️ Build #35843317 | 📁 Comparing 9584733 against latest (5976f17)

  🔍 Preview build  

6 files changed · ± 6 modified

± Modified

@github-actions

github-actions Bot commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Docker Smoke Test

Job Tests Passed ✅ Skipped ⚠️ Failed ❌ Log
smoke (app) 11 11 0 0 ✅ 131298471797
smoke (fastcgi) 5 5 0 0 ✅ 131298471943
smoke (kubernetes) 8 8 0 0 ✅ 131298471866
smoke (lambda) 2 2 0 0 ✅ 131298471922
smoke (lambda-image) 5 5 0 0 ✅ 131298471752
smoke (lambda-stream) 2 2 0 0 ✅ 131298471990
smoke (lambda-zip) 9 9 0 0 ✅ 131298471833
smoke (lite) 4 4 0 0 ✅ 131298471460
smoke (lite-pep723) 4 4 0 0 ✅ 131298472002
All 50 50 0 0 ✅

The run of the workflow

@github-actions

github-actions Bot commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Tests

Job Tests Passed ✅ Skipped ⚠️ Failed ❌ Log
prove (python 3.12, Asyncio) 7451 7422 29 0 ✅ 131298463165
prove (python 3.12, Poll) 7423 7392 31 0 ✅ 131298463447
prove (python 3.13, Asyncio) 7451 7422 29 0 ✅ 131298463194
prove (python 3.13, Poll) 7423 7392 31 0 ✅ 131298463459
prove (python 3.14, Asyncio) 7482 7458 24 0 ✅ 131298463008
prove (python 3.14, Poll) 7454 7428 26 0 ✅ 131298462993
prove (python 3.15, Asyncio) 7482 7458 24 0 ✅ 131298463138
prove (python 3.15, Poll) 7454 7428 26 0 ✅ 131298463448
prove (python 3.15t, Asyncio) 7482 7458 24 0 ✅ 131298463242
prove (python 3.15t, Poll) 7454 7428 26 0 ✅ 131298462878
prove (python mise, Asyncio) 7561 7526 35 0 ✅ 131298462490
prove (python mise, Asyncio, Windows) 7282 7216 66 0 ✅ 131298462810
prove (python mise, Poll) 7533 7496 37 0 ✅ 131298462752
prove (python mise, Poll, Windows) 7254 7186 68 0 ✅ 131298462636
prove (python pypy3.12, Asyncio) 7451 7422 29 0 ✅ 131298463250
prove (python pypy3.12, Poll) 7423 7392 31 0 ✅ 131298462800
pytest (python 3.12, ubuntu-26.04-arm) 440 414 26 0 ✅ 131298462737
pytest (python 3.15, ubuntu-26.04-arm) 440 419 21 0 ✅ 131298462768
pytest (python 3.15, windows-2025) 440 391 49 0 ✅ 131298462799
servers (python 3.12) 54 54 0 0 ✅ 131298462830
servers (python 3.15) 53 53 0 0 ✅ 131298462743
All 120487 119855 632 0 ✅

The run of the workflow

…has started, not after a time that a slow runner exceeds

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… of cancel as the value of then, whose rejection the next promise would adopt

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

1 participant