From d559f3c35f6fba9243515173a63d6c2c2ea6233f Mon Sep 17 00:00:00 2001 From: Piotr Roszatycki Date: Thu, 12 Nov 2026 16:10:02 +0100 Subject: [PATCH 1/4] Pyjo.Reactor, Pyjo.Promise, Pyjo.UserAgent: the cancellation of coroutines 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 --- .claude/skills/distributions/SKILL.md | 9 +++ Pyjo/Promise.py | 54 +++++++++++++++- Pyjo/Reactor/Asyncio.py | 20 +++++- Pyjo/Reactor/__init__.py | 49 ++++++++++++-- Pyjo/UserAgent/__init__.py | 28 +++++++- t/docs/Promise.py | 31 +++++++++ t/docs/Reactor.py | 17 +++++ t/docs/UserAgent.py | 10 +++ t/stdlib/Promise.py | 92 +++++++++++++++++++++++++++ 9 files changed, 295 insertions(+), 15 deletions(-) diff --git a/.claude/skills/distributions/SKILL.md b/.claude/skills/distributions/SKILL.md index 329482ee..e110c900 100644 --- a/.claude/skills/distributions/SKILL.md +++ b/.claude/skills/distributions/SKILL.md @@ -55,6 +55,15 @@ back here. not nest, as Trio, it raises there: a test of the suite that makes one in the event loop expects that when a new reactor of the class is running already, as `type(reactor).new().is_running` tells. +- **Cancellation.** `run_coroutine` returns a function that cancels the + coroutine where it waits, with the exception of the cancellation of the + event loop, and the promise that it waits for, with `cancel` of + `Pyjo.Promise`, which closes the connection of a request; Pyjoyment cancels + an action that is a coroutine function when its connection closes before + its response ends. A reactor that runs a coroutine as a task of its loop + cancels the task, as `Pyjo.Reactor.Asyncio` does, and its `awaitable` + cancels the promise when the wait of the task is cancelled; one that + returns `None` leaves such an action running to its end. - **State in the kernel.** Pyjoyment closes some sockets before it removes them from the reactor, and forks the workers of `Pyjo.Server.Prefork`, the servers of `Pyjo.Server.Hot` and `Pyjo.Server.Reload`, and the subprocesses. diff --git a/Pyjo/Promise.py b/Pyjo/Promise.py index a17ff2ed..0e544307 100644 --- a/Pyjo/Promise.py +++ b/Pyjo/Promise.py @@ -168,7 +168,7 @@ async def main(): import weakref from collections.abc import Awaitable, Callable, Coroutine, Generator -from typing import TYPE_CHECKING, Any, Concatenate, cast +from typing import TYPE_CHECKING, Any, Concatenate, Self, cast type Anything = builtins.object @@ -201,7 +201,10 @@ class Pyjo_Promise(Pyjo.Base.object): """ def __init__( - self, cb: Callable[[Handler, Handler], Anything] | Awaitable[Any] | concurrent.futures.Future[Any] | None = None + self, + cb: Callable[[Handler, Handler], Anything] | Awaitable[Any] | concurrent.futures.Future[Any] | None = None, + *, + cancel: Callable[[], Anything] | None = None, ) -> None: """:: @@ -209,6 +212,7 @@ def __init__( promise = Pyjo.Promise.new(lambda resolve, reject: ...) promise = Pyjo.Promise.new(coroutine) promise = Pyjo.Promise.new(future) + promise = Pyjo.Promise.new(cancel=lambda: ...) Construct a new :mod:`Pyjo.Promise` object. :: @@ -245,7 +249,17 @@ async def lucky(): with concurrent.futures.ThreadPoolExecutor() as executor: promise = Pyjo.Promise.new(executor.submit(time.sleep, 1)) promise.wait() + + ``cancel`` is the function that :meth:`cancel` calls, which stops what + the promise waits for, as :meth:`Pyjo.UserAgent.Pyjo_UserAgent.start_p` + closes the connection of its request; the one of a coroutine cancels + it. :: + + # A promise of a timer that stops it + promise = Pyjo.Promise.new(cancel=lambda: Pyjo.IOLoop.remove(id)) + id = Pyjo.IOLoop.timer(5, lambda loop: promise.resolve()) """ + self._cancel = cancel self._debug: Pyjo.Exception.object | None = None self._handled = False self._ioloop: weakref.ref[Pyjo.IOLoop.object] | None = None @@ -268,7 +282,7 @@ async def lucky(): def resolve(value: Any) -> None: self.resolve() if value is None else self.resolve(value) - self.ioloop.reactor.run_coroutine(coro, resolve, lambda e: self.reject(e)) + self._cancel = self.ioloop.reactor.run_coroutine(coro, resolve, lambda e: self.reject(e)) elif cb is not None: cb(lambda *values: self.resolve(*values), lambda *values: self.reject(*values)) @@ -370,6 +384,40 @@ def any(cls, *promises: 'Pyjo_Promise') -> 'Pyjo_Promise': """ return _all(3, promises) + # Not in Mojolicious: Mojo::Promise follows the Promise of ECMAScript, which cannot be cancelled + def cancel(self) -> Self: + """:: + + promise = promise.cancel() + + Stop what a pending promise waits for, with the function ``cancel`` + that it was made with, once, as nothing waits for the promise anymore: + a coroutine gets the exception of the cancellation of the reactor, as + :class:`asyncio.CancelledError`, where it waits, and a request of + :meth:`Pyjo.UserAgent.Pyjo_UserAgent.start_p` closes its connection. + The promise is settled as what it waits for ends, and is not reported + as an unhandled rejected promise then. A promise that is settled, or + has nothing to cancel, stays as it is. A coroutine that awaits a + promise cancels it when it is cancelled itself. :: + + # Cancel a coroutine, whose finally blocks run + async def main(): + try: + await Pyjo.Promise.timer(5) + finally: + print('cancelled') + + promise = Pyjo.Promise.new(main()) + Pyjo.IOLoop.timer(1, lambda loop: promise.cancel()) + promise.catch(lambda err: print(type(err).__name__)).wait() + """ + if self._status or (cancel := self._cancel) is None: + return self + self._cancel = None + self._handled = True + cancel() + return self + def catch(self, cb: Handler) -> 'Pyjo_Promise': """:: diff --git a/Pyjo/Reactor/Asyncio.py b/Pyjo/Reactor/Asyncio.py index b520800a..914c7173 100644 --- a/Pyjo/Reactor/Asyncio.py +++ b/Pyjo/Reactor/Asyncio.py @@ -314,7 +314,13 @@ def settle(*values: Any) -> None: if not future.done(): future.set_result(None) + # The promise of a task that is cancelled while it waits, which nothing waits for anymore + def cancelled(future: asyncio.Future[None]) -> None: + if future.cancelled() and callable(cancel := getattr(promise, 'cancel', None)): + cancel() + promise.then(settle, settle) + future.add_done_callback(cancelled) return future # Not in Mojolicious: the release of the event loop of its own, which Perl does in DESTROY, also at the end of a with @@ -335,16 +341,19 @@ def close(self) -> None: # Not in Mojolicious: a task of the loop def run_coroutine( self, coro: Coroutine[Any, Any, Any], resolve: Callable[[Any], Any], reject: Callable[[Any], Any] - ) -> None: + ) -> Callable[[], None]: """:: - reactor.run_coroutine(coro, resolve, reject) + cancel = reactor.run_coroutine(coro, resolve, reject) Run a coroutine as a task of :attr:`loop`, where it can await anything of the loop, and call ``resolve`` with what it returns or ``reject`` with the exception it raises. A task that is not done is something to watch, so that :meth:`start` does not stop before it, and each step of - the coroutine is an event, which ends :meth:`one_tick`. + the coroutine is an event, which ends :meth:`one_tick`. It returns a + function that cancels the task, whose coroutine gets + :class:`asyncio.CancelledError` where it waits, and whose promise that + it waits for is cancelled. """ task = self.loop.create_task(_steps(coro, weakref.WeakMethod(self._step))) self._tasks.add(task) @@ -363,6 +372,11 @@ def done(task: asyncio.Task[Any]) -> None: task.add_done_callback(done) + def cancel() -> None: + task.cancel() + + return cancel + # Not in Mojolicious: a future of the loop def future(self) -> asyncio.Future[Any]: """:: diff --git a/Pyjo/Reactor/__init__.py b/Pyjo/Reactor/__init__.py index ca03d4c7..ad3fffbb 100644 --- a/Pyjo/Reactor/__init__.py +++ b/Pyjo/Reactor/__init__.py @@ -172,6 +172,17 @@ def set_result(self, result: T, /) -> None: ... __path__ = extend_path(__path__, __name__) +# Not in Mojolicious: Perl has no cancellation of an async sub, whose promise waits on +class CancelledError(BaseException): + """ + Exception raised in a coroutine that :meth:`Pyjo_Reactor.run_coroutine` of + the base class runs, where it waits, when it is cancelled, as + :class:`asyncio.CancelledError` is raised in a task of :mod:`asyncio`, so + that ``except Exception`` does not catch it, and its ``finally`` blocks + run. + """ + + class Pyjo_Reactor(Pyjo.EventEmitter.object): """ :mod:`Pyjo.Reactor` inherits all methods from :mod:`Pyjo.EventEmitter` and @@ -263,10 +274,10 @@ def blocking[**P, R](self, cb: Callable[P, R], *args: P.args, **kwargs: P.kwargs # Not in Mojolicious: how a coroutine runs as a promise, as Mojo's async sub def run_coroutine( self, coro: Coroutine[Any, Any, Any], resolve: Callable[[Any], Anything], reject: Callable[[Any], Anything] - ) -> None: + ) -> Callable[[], None] | None: """:: - reactor.run_coroutine(coro, resolve, reject) + cancel = reactor.run_coroutine(coro, resolve, reject) Run a coroutine and call ``resolve`` with what it returns or ``reject`` with the exception it raises. The base class runs it at @@ -276,19 +287,32 @@ def run_coroutine( until it ends, as the reactor of asyncio keeps its tasks. A reactor of an event loop runs it as a task of its loop, where it can await anything of that loop. + + It returns a function that cancels the coroutine, which gets the + exception of the cancellation of the reactor where it waits, as + :class:`CancelledError` of the base class, and cancels the promise + that it waits for, as with :meth:`Pyjo.Promise.Pyjo_Promise.cancel`, + or ``None`` for a reactor that cannot cancel it. :: + + # Cancel the coroutine, whose finally blocks run + cancel = reactor.run_coroutine(main(), resolve, reject) + cancel() """ coroutines = self._coroutines coroutines.add(coro) + # What the coroutine waits for, whose settling resumes it unless it was cancelled meanwhile + current: list[Any] = [None] - def step() -> None: + def step(error: BaseException | None = None) -> None: + current[0] = None try: - waiting = coro.send(None) + waiting = coro.send(None) if error is None else coro.throw(error) except StopIteration as e: coroutines.discard(coro) resolve(e.value) return - except Exception as e: + except (Exception, CancelledError) as e: coroutines.discard(coro) reject(e) return @@ -298,12 +322,23 @@ def step() -> None: coro.close() reject(TypeError(f'{_name(type(self))} cannot wait for {waiting!r}')) return + current[0] = waiting + + def resume(*values: Any) -> None: + if current[0] is waiting: + step() + then(resume, resume) - def resume(*values: Any) -> None: - step() + def cancel() -> None: + if (waiting := current[0]) is None: + return + step(CancelledError()) + if callable(canceller := getattr(waiting, 'cancel', None)): + canceller() step() + return cancel @classmethod def detect(cls) -> str: diff --git a/Pyjo/UserAgent/__init__.py b/Pyjo/UserAgent/__init__.py index ca90c117..5a86b19c 100644 --- a/Pyjo/UserAgent/__init__.py +++ b/Pyjo/UserAgent/__init__.py @@ -853,9 +853,33 @@ def ok(tx): print(tx.result.body) ok.catch(lambda err: warn(f'Connection error: {err}')).wait() + + The promise closes the connection of the request when it is cancelled + with :meth:`Pyjo.Promise.Pyjo_Promise.cancel`, as when the coroutine + that awaits it is cancelled, and is rejected with + ``Request cancelled``. :: + + # Stop a request that nothing waits for + promise = ua.start_p(tx) + promise.cancel() """ - promise = Pyjo.Promise.new() - self.start(tx, lambda ua, tx: ua.transactor.promisify(promise, tx)) + + def done(ua: Pyjo_UserAgent, tx: Pyjo.Transaction.HTTP.object) -> None: + ua.transactor.promisify(promise, tx) + + # Not in Mojolicious: the connection of the request, which a redirect replaces with the same callback, weakly, + # as the promise keeps the function of its cancellation + ref, callback = weakref.ref(self), weakref.ref(done) + + def cancel() -> None: + if (ua := ref()) is None or (cb := callback()) is None: + return + for id, c in list(ua._connections.items()): + if c.get('cb') is cb: + ua._error(id, 'Request cancelled') + + promise = Pyjo.Promise.new(cancel=cancel) + self.start(tx, done) return promise # The arguments of the transactor come before the callback, so the method is not typed further; unlike diff --git a/t/docs/Promise.py b/t/docs/Promise.py index c9397859..6826a4ba 100644 --- a/t/docs/Promise.py +++ b/t/docs/Promise.py @@ -162,6 +162,30 @@ async def main(): 'results', ) +with subtest('cancel'): + with contextlib.redirect_stdout(io.StringIO()) as stdout: + # Cancel a coroutine, whose finally blocks run + async def main(): + try: + await Pyjo.Promise.timer(5) + finally: + print('cancelled') + + promise = Pyjo.Promise.new(main()) + Pyjo.IOLoop.timer(0.01, lambda loop: promise.cancel()) + promise.catch(lambda err: print(type(err).__name__)).wait() + is_ok(stdout.getvalue(), 'cancelled\nCancelledError\n', 'finally block and the cancellation of the reactor') + ok(promise.cancel() is promise, 'settled, as it is') + + # Nothing to cancel + promise = Pyjo.Promise.new() + promise.cancel() + promise.resolve('later') + got = [] + promise.then(got.append) + Pyjo.IOLoop.one_tick() + is_deeply_ok(got, ['later'], 'still pending') + with subtest('catch'): got = [] promise = Pyjo.Promise.reject('Oops') @@ -256,6 +280,13 @@ async def wrap(): promise.then(lambda *values: got.append(values)).wait() is_deeply_ok(got, [()], 'future of concurrent.futures') + # A promise of a timer that stops it + promise = Pyjo.Promise.new(cancel=lambda: Pyjo.IOLoop.remove(id)) + id = Pyjo.IOLoop.timer(5, lambda loop: promise.resolve()) + promise.cancel() + is_ok(Pyjo.IOLoop.singleton.reactor.remove(id), False, 'timer removed') + is_ok(promise.cancel(), promise, 'once') + with subtest('reject and resolve'): got = [] new = Pyjo.Promise.reject('a', 'b') diff --git a/t/docs/Reactor.py b/t/docs/Reactor.py index 5bef2170..093739b7 100644 --- a/t/docs/Reactor.py +++ b/t/docs/Reactor.py @@ -123,6 +123,23 @@ async def coro(): Pyjo.IOLoop.one_tick() is_deeply_ok(got, ['done'], 'driven without asyncio') + # Cancel the coroutine, whose finally blocks run + events = [] + + async def main(): + try: + await reactor.awaitable(Pyjo.Promise.new(cancel=lambda: events.append('promise cancelled'))) + finally: + events.append('finally') + + got = [] + cancel = reactor.run_coroutine(main(), got.append, got.append) + cancel() + is_deeply_ok(events, ['finally', 'promise cancelled'], 'cancelled where it waits, with its promise') + isa_ok(got[0], Pyjo.Reactor.CancelledError, 'rejected') + cancel() + is_ok(len(got), 1, 'once') + with subtest('future'): import concurrent.futures diff --git a/t/docs/UserAgent.py b/t/docs/UserAgent.py index b7925dd3..d1e50c5c 100644 --- a/t/docs/UserAgent.py +++ b/t/docs/UserAgent.py @@ -24,6 +24,7 @@ def root(c): app.routes.any('/test', {'text': 'test'}) +app.routes.any('/slow', lambda c: c.render_later()) app.routes.any('/test.json', lambda c: c.render(json={'user': c.req.url.to_abs().userinfo})) app.routes.any('/search', {'text': 'Mojolicious'}) app.routes.any('/mock-mojolicious', {'text': 'mock'}) @@ -566,6 +567,15 @@ def ok_start(tx): ok_start.catch(lambda err: print(f'Connection error: {err}')).wait() is_ok(stdout.getvalue(), '200\n', 'print(tx.result.body)') + # Stop a request that nothing waits for + tx = ua.build_tx('GET', 'http://example.com/slow') + promise = ua.start_p(tx) + Pyjo.IOLoop.timer(0.05, lambda loop: promise.cancel()) + reason = [] + promise.catch(reason.append).wait() + is_deeply_ok(reason, ['Request cancelled'], 'rejected') + is_ok(tx.res.error, {'message': 'Request cancelled'}, 'error of the transaction') + with subtest('websocket'): with output() as stdout: diff --git a/t/stdlib/Promise.py b/t/stdlib/Promise.py index 6b29abc8..ac56ce63 100644 --- a/t/stdlib/Promise.py +++ b/t/stdlib/Promise.py @@ -1,7 +1,9 @@ import Pyjo.Exception import Pyjo.IOLoop import Pyjo.Promise +import Pyjo.Reactor import Pyjo.Reactor.Asyncio +import Pyjo.Reactor.Poll from Pyjo.Test import * @@ -269,6 +271,96 @@ async def futures(): is_ok(asyncio.run(futures(), loop_factory=asyncio.SelectorEventLoop), 'awaited', 'awaited in a task of asyncio') + +# Not in Mojolicious: the cancellation of a coroutine, on the reactor of asyncio and on the base class, of Poll +def cancellation(name: str, cancelled_error: type[BaseException]) -> None: + with subtest(f'Cancellation of a coroutine on {name}'): + events = [] + + async def main(): + try: + await Pyjo.Promise.timer(5) + except Exception: + events.append('caught as an exception') + finally: + events.append('finally') + + promise = Pyjo.Promise.new(main()) + Pyjo.IOLoop.timer(0.01, lambda loop: promise.cancel()) + reason = [] + promise.catch(reason.append).wait() + is_deeply_ok(events, ['finally'], 'finally, not except Exception') + isa_ok(reason[0], cancelled_error, 'rejected with the cancellation of the reactor') + + with subtest(f'The promise that a cancelled coroutine waits for on {name}'): + events = [] + waiting = Pyjo.Promise.new(cancel=lambda: events.append('promise cancelled')) + + async def inner(): + try: + await Pyjo.Promise.timer(5) + finally: + events.append('inner finally') + + async def outer(): + await waiting + await Pyjo.Promise.new(inner()) + + promise = Pyjo.Promise.new(outer()) + Pyjo.IOLoop.timer(0.01, lambda loop: promise.cancel()) + promise.catch(lambda e: None).wait() + is_deeply_ok(events, ['promise cancelled'], 'cancelled') + + # A coroutine in a promise that the cancelled coroutine waits for + events.clear() + waiting.resolve() + promise = Pyjo.Promise.new(outer()) + Pyjo.IOLoop.timer(0.01, lambda loop: promise.cancel()) + promise.catch(lambda e: None).wait() + is_deeply_ok(events, ['inner finally'], 'cancelled too') + + with subtest(f'A coroutine that goes on after its cancellation on {name}'): + + async def main(): + try: + await Pyjo.Promise.timer(5) + except BaseException: + pass + return await Pyjo.Promise.timer(0.01, 'recovered') + + promise = Pyjo.Promise.new(main()) + Pyjo.IOLoop.timer(0.01, lambda loop: promise.cancel()) + got = [] + promise.then(got.append).wait() + is_deeply_ok(got, ['recovered'], 'fulfilled') + + with subtest(f'Cancellation before the first step on {name}'): + events = [] + + async def main(): + events.append('started') + await Pyjo.Promise.timer(5) + + with warnings.catch_warnings(record=True) as caught: + warnings.simplefilter('always') + early = Pyjo.Promise.new(main()) + early.cancel() + reason = [] + early.catch(reason.append).wait() + del early + gc.collect() + isa_ok(reason[0], cancelled_error, 'rejected') + is_deeply_ok([str(w.message) for w in caught], [], 'no warnings') + + +for reactor, cancelled_error in ( + (Pyjo.Reactor.Asyncio, asyncio.CancelledError), + (Pyjo.Reactor.Poll, Pyjo.Reactor.CancelledError), +): + Pyjo.IOLoop.singleton.reactor = reactor.new() + cancellation(reactor.__name__, cancelled_error) +Pyjo.IOLoop.singleton.reactor = Pyjo.Reactor.Asyncio.new() + with subtest('warnings'): with warnings.catch_warnings(record=True) as warn: warnings.simplefilter('always') From 8826597a6c25ee5c12327d4227c3e9b1c346ef3b Mon Sep 17 00:00:00 2001 From: Piotr Roszatycki Date: Thu, 12 Nov 2026 16:10:03 +0100 Subject: [PATCH 2/4] Pyjoyment: an action that is a coroutine function is cancelled when its 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 --- Pyjoyment/__init__.py | 21 +++++ docs/guides/cookbook.rst | 10 ++ plugins/pyjoyment/skills/async/SKILL.md | 5 + t/Pyjoyment/cancel_lite_app.py | 117 ++++++++++++++++++++++++ 4 files changed, 153 insertions(+) create mode 100644 t/Pyjoyment/cancel_lite_app.py diff --git a/Pyjoyment/__init__.py b/Pyjoyment/__init__.py index 281d5d33..bd609020 100644 --- a/Pyjoyment/__init__.py +++ b/Pyjoyment/__init__.py @@ -238,6 +238,7 @@ def after_server_stop(server, app): ... import inspect import os import sys +import weakref from collections.abc import Callable from typing import TYPE_CHECKING, Any, Self, cast, overload @@ -1059,11 +1060,28 @@ def _action(next: Any, c: Pyjoyment.Controller.object, action: Callable[..., Any # Not in Mojolicious: an action that is a coroutine function runs as a promise, which async sub of Mojo returns if inspect.iscoroutine(val): val = Pyjo.Promise.new(val) + _cancel_on_close(c, val) if isinstance(val, Pyjo.Promise.object): val.catch(functools.partial(_reply_exception, c)) return val +# Unlike Mojolicious: an async sub of Mojo runs to its end after its connection closes, with the requests that it waits +# for, as a promise cannot be cancelled; a coroutine is cancelled where it waits, as nothing reads its response anymore +def _cancel_on_close(c: Pyjoyment.Controller.object, promise: Pyjo.Promise.object) -> None: + # Weakly, as the transaction keeps its subscribers, and the coroutine the controller + controller, ref = weakref.ref(c), weakref.ref(promise) + + def finish(tx: Pyjo.Transaction.HTTP.object) -> None: + if tx.res.is_finished or (promise := ref()) is None or (c := controller()) is None: + return + c.stash()['pyjo.cancelled'] = True + promise.cancel() + + if (tx := c.tx) is not None: + tx.on('finish', finish) + + def _dispatch(next: Callable[[], Any], c: Pyjoyment.Controller.object) -> None: c.app.dispatch(c) @@ -1076,6 +1094,9 @@ def _exception(next: Callable[[], Any], c: Pyjoyment.Controller.object) -> None: def _reply_exception(c: Pyjoyment.Controller.object, e: Any) -> None: + # The cancellation of an action whose connection has closed, which has no response to render + if c.stash().get('pyjo.cancelled') and not isinstance(e, Exception): + return c.helpers.reply.exception(e) diff --git a/docs/guides/cookbook.rst b/docs/guides/cookbook.rst index 7d4c9be8..436418ab 100644 --- a/docs/guides/cookbook.rst +++ b/docs/guides/cookbook.rst @@ -1553,6 +1553,16 @@ Promises returned by actions will automatically get the default :mod:`Pyjoyment` exception handler attached. Making it much harder to ever miss a non-blocking exception again, even if you forgot to handle it yourself. +An action that is a coroutine function is cancelled where it waits when its +connection closes before its response ends, as when the client goes away or +the inactivity timeout of the connection passes. It gets the exception of the +cancellation of the reactor, :class:`asyncio.CancelledError` of the default +one, which ``except Exception`` does not catch, so that its ``finally`` +blocks run, and the request of :mod:`Pyjo.UserAgent` 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 to its end. + Timers ~~~~~~ diff --git a/plugins/pyjoyment/skills/async/SKILL.md b/plugins/pyjoyment/skills/async/SKILL.md index fbf89f72..13d6a26e 100644 --- a/plugins/pyjoyment/skills/async/SKILL.md +++ b/plugins/pyjoyment/skills/async/SKILL.md @@ -76,6 +76,11 @@ app.start() `for`, which waits with the event loop. - `c.ua()` is the user agent of the application: `get_p`, `post_p`, `put_p` and the others give a promise of the transaction. +- An `async def` action whose client goes away before its response ends is + cancelled where it waits: its `finally` blocks run, `except Exception` does + not catch the cancellation, and the request of `get_p` that it waits for + closes its connection. `promise.cancel()` does it for a promise of a + request or of a coroutine. ## Code that blocks diff --git a/t/Pyjoyment/cancel_lite_app.py b/t/Pyjoyment/cancel_lite_app.py new file mode 100644 index 00000000..0c014820 --- /dev/null +++ b/t/Pyjoyment/cancel_lite_app.py @@ -0,0 +1,117 @@ +import os + + +os.environ['PYJO_MODE'] = 'development' + +import Pyjo.IOLoop +import Pyjo.Promise +import Pyjo.Test.Pyjo + +from Pyjo.Test import * +from Pyjoyment.Lite import app, get + + +# Not in Mojolicious: an action that is a coroutine function is cancelled where it waits when its connection closes +# before its response ends, with the request of the user agent that it waits for + + +# A reactor whose run_coroutine cannot cancel a coroutine returns None, and leaves such an action running to its end +async def probe(): + pass + + +if Pyjo.IOLoop.singleton.reactor.run_coroutine(probe(), lambda value: None, lambda e: None) is None: + plan(skip_all='the reactor cannot cancel a coroutine') + +app.log.level = 'fatal' + +events = [] + + +@get('/upstream') +async def upstream(c): + try: + await Pyjo.Promise.timer(5) + finally: + events.append('upstream cancelled') + c.render(text='late') + + +@get('/redirect') +def redirect(c): + c.redirect_to('/upstream') + + +@get('/slow') +async def slow(c): + try: + tx = await c.ua().get_p('/upstream') + c.render(text=tx.result.text) + finally: + events.append('action ended') + + +@get('/rendered') +async def rendered(c): + c.render(text='early') + await Pyjo.Promise.timer(0.05) + events.append('after the response') + + +@get('/events') +async def stream(c): + c.res.headers.content_type = 'text/event-stream' + c.write_sse({'text': 'first'}) + try: + while True: + await Pyjo.Promise.timer(5) + c.write_sse({'text': 'next'}) + finally: + events.append('stream cancelled') + + +def settle(): + # The events of the server after the client has closed its connection + Pyjo.Promise.timer(0.1).wait() + + +t = Pyjo.Test.Pyjo.new() +t.ua.inactivity_timeout = 0.2 + +with subtest('Connection closed while the action waits'): + events.clear() + with app.log.capture('error') as logs: + tx = t.ua.get('/slow') + settle() + is_ok(tx.error, {'message': 'Inactivity timeout'}, 'client gave up') + is_deeply_ok(sorted(events), ['action ended', 'upstream cancelled'], 'action and its request cancelled') + is_deeply_ok(list(logs), [], 'no exception') + +with subtest('Not after the response'): + events.clear() + t.get_ok('/rendered').status_is(200).content_is('early') + settle() + is_deeply_ok(events, ['after the response'], 'the action goes on') + +with subtest('A stream whose client has gone'): + events.clear() + tx = t.ua.get('/events') + settle() + is_ok(tx.res.code, 200, 'stream started') + is_deeply_ok(events, ['stream cancelled'], 'cancelled') + +with subtest('A request after a redirect'): + events.clear() + t.ua.max_redirects = 1 + promise = t.ua.get_p('/redirect') + Pyjo.IOLoop.timer(0.05, lambda loop: promise.cancel()) + reason = [] + promise.catch(reason.append).wait() + settle() + t.ua.max_redirects = 0 + is_deeply_ok(reason, ['Request cancelled'], 'rejected') + is_deeply_ok(events, ['upstream cancelled'], 'the request of the redirect closed') + +t.close() + +done_testing() From 64a90428c6318ae1d49ddc1239dc1a9fd380b27f Mon Sep 17 00:00:00 2001 From: Piotr Roszatycki Date: Thu, 12 Nov 2026 16:19:51 +0100 Subject: [PATCH 3/4] t/Pyjoyment/cancel_lite_app.py: the client goes away once the action has started, not after a time that a slow runner exceeds Co-Authored-By: Claude Opus 5.5 --- t/Pyjoyment/cancel_lite_app.py | 59 +++++++++++++++++++++++++++------- 1 file changed, 47 insertions(+), 12 deletions(-) diff --git a/t/Pyjoyment/cancel_lite_app.py b/t/Pyjoyment/cancel_lite_app.py index 0c014820..e96bcf2f 100644 --- a/t/Pyjoyment/cancel_lite_app.py +++ b/t/Pyjoyment/cancel_lite_app.py @@ -27,9 +27,23 @@ async def probe(): events = [] +# The promises of the actions that have started, which the client closes its connection after, whatever time they take +started: dict[str, Pyjo.Promise.object] = {} + + +def when(name): + promise = started[name] = Pyjo.Promise.new() + return promise + + +def signal(name): + if (promise := started.pop(name, None)) is not None: + promise.resolve() + @get('/upstream') async def upstream(c): + signal('upstream') try: await Pyjo.Promise.timer(5) finally: @@ -51,6 +65,14 @@ async def slow(c): events.append('action ended') +@get('/wait') +async def wait(c): + try: + await Pyjo.Promise.timer(5) + finally: + events.append('action ended') + + @get('/rendered') async def rendered(c): c.render(text='early') @@ -62,6 +84,7 @@ async def rendered(c): async def stream(c): c.res.headers.content_type = 'text/event-stream' c.write_sse({'text': 'first'}) + signal('stream') try: while True: await Pyjo.Promise.timer(5) @@ -70,23 +93,40 @@ async def stream(c): events.append('stream cancelled') +def gone(path, name): + # A request whose client goes away once the action has started + client = t.ua.get_p(path) + when(name).then(lambda *values: client.cancel()) + reason = [] + client.catch(reason.append).wait() + settle() + return reason + + def settle(): # The events of the server after the client has closed its connection Pyjo.Promise.timer(0.1).wait() t = Pyjo.Test.Pyjo.new() -t.ua.inactivity_timeout = 0.2 with subtest('Connection closed while the action waits'): events.clear() with app.log.capture('error') as logs: - tx = t.ua.get('/slow') - settle() - is_ok(tx.error, {'message': 'Inactivity timeout'}, 'client gave up') + reason = gone('/slow', 'upstream') + is_deeply_ok(reason, ['Request cancelled'], 'client gone') is_deeply_ok(sorted(events), ['action ended', 'upstream cancelled'], 'action and its request cancelled') is_deeply_ok(list(logs), [], 'no exception') +with subtest('Inactivity timeout of the client'): + events.clear() + t.ua.inactivity_timeout = 0.5 + tx = t.ua.get('/wait') + settle() + t.ua.inactivity_timeout = 40 + is_ok(tx.error, {'message': 'Inactivity timeout'}, 'client gave up') + is_deeply_ok(events, ['action ended'], 'action cancelled') + with subtest('Not after the response'): events.clear() t.get_ok('/rendered').status_is(200).content_is('early') @@ -95,19 +135,14 @@ def settle(): with subtest('A stream whose client has gone'): events.clear() - tx = t.ua.get('/events') - settle() - is_ok(tx.res.code, 200, 'stream started') + reason = gone('/events', 'stream') + is_deeply_ok(reason, ['Request cancelled'], 'client gone') is_deeply_ok(events, ['stream cancelled'], 'cancelled') with subtest('A request after a redirect'): events.clear() t.ua.max_redirects = 1 - promise = t.ua.get_p('/redirect') - Pyjo.IOLoop.timer(0.05, lambda loop: promise.cancel()) - reason = [] - promise.catch(reason.append).wait() - settle() + reason = gone('/redirect', 'upstream') t.ua.max_redirects = 0 is_deeply_ok(reason, ['Request cancelled'], 'rejected') is_deeply_ok(events, ['upstream cancelled'], 'the request of the redirect closed') From 9584733c62bd58d9eefa7a9fe3ec7aec2386f6d0 Mon Sep 17 00:00:00 2001 From: Piotr Roszatycki Date: Thu, 12 Nov 2026 16:43:17 +0100 Subject: [PATCH 4/4] t/Pyjoyment/cancel_lite_app.py: the client leaves without the promise of cancel as the value of then, whose rejection the next promise would adopt Co-Authored-By: Claude Opus 5.5 --- t/Pyjoyment/cancel_lite_app.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/t/Pyjoyment/cancel_lite_app.py b/t/Pyjoyment/cancel_lite_app.py index e96bcf2f..e87af404 100644 --- a/t/Pyjoyment/cancel_lite_app.py +++ b/t/Pyjoyment/cancel_lite_app.py @@ -96,7 +96,12 @@ async def stream(c): def gone(path, name): # A request whose client goes away once the action has started client = t.ua.get_p(path) - when(name).then(lambda *values: client.cancel()) + + # Not a lambda, whose value would be the promise that cancel returns, which then would adopt its rejection + def leave(*values): + client.cancel() + + when(name).then(leave) reason = [] client.catch(reason.append).wait() settle()