Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .claude/skills/distributions/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
54 changes: 51 additions & 3 deletions Pyjo/Promise.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -201,14 +201,18 @@ 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:
"""::

promise = Pyjo.Promise.new()
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. ::

Expand Down Expand Up @@ -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
Expand All @@ -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))
Expand Down Expand Up @@ -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':
"""::

Expand Down
20 changes: 17 additions & 3 deletions Pyjo/Reactor/Asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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]:
"""::
Expand Down
49 changes: 42 additions & 7 deletions Pyjo/Reactor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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:
Expand Down
28 changes: 26 additions & 2 deletions Pyjo/UserAgent/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions Pyjoyment/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand All @@ -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)


Expand Down
10 changes: 10 additions & 0 deletions docs/guides/cookbook.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
~~~~~~

Expand Down
5 changes: 5 additions & 0 deletions plugins/pyjoyment/skills/async/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Loading
Loading