INNER CODE UNIT · Python
call_service_handler_sync
ministackorg/ministack · ministack/app.py:293
def call_service_handler_sync(handler, method, path, headers, body, query_params, timeout: float | None = None):
"""Call an async service handler from a worker thread and return its result.
In-process callers used to drive handlers with ``coro.send(None)``, relying
on an unwritten invariant that service handlers never actually await. That
held only while every handler was synchronous underneath. The moment one
awaits — offloading Docker work, for instance — the first step runs with no
running event loop and raises ``RuntimeError: no running event loop``, which
escapes past the caller's ``except StopIteration``.
So the coroutine is scheduled on the loop that is actually running and this
thread waits for the result. Callers must be on a worker thread: waiting on
the loop from the loop itself would deadlock, so that is refused loudly
rather than hanging.
"""
loop = _MAIN_LOOP
if loop is None or loop.is_closed():
# No server running (unit tests, CLI use): drive it on a private loop.