mirror of
https://github.com/agentscope-ai/ReMe.git
synced 2026-11-27 23:58:47 +00:00
Some checks failed
CI and Release / Docker / Build and test / amd64 (push) Has been cancelled
CI and Release / Docker / Build and test / arm64 (push) Has been cancelled
Security / CodeQL / Analyze python (push) Has been cancelled
CI / Python quality / Pre-commit (push) Has been cancelled
CI / Python tests / Unit Tests - py3.12 (push) Has been cancelled
CI / Python tests / Unit Tests - py3.13 (push) Has been cancelled
CI / Python tests / Unit Tests - py3.14 (push) Has been cancelled
CI / Python packages / Build and verify distributions (push) Has been cancelled
CI / Python quality / GitHub Actions (push) Has been cancelled
CI / Python tests / Hermes embedded - ubuntu-latest (push) Has been cancelled
CI / Python tests / Hermes embedded - windows-latest (push) Has been cancelled
CI / Python tests / Unit Tests - py3.11 (push) Has been cancelled
CI / ReMe Studio / Studio checks (push) Has been cancelled
Deploy / Documentation / Build documentation (push) Has been cancelled
CI / Windows / CLI smoke - py3.11 (push) Has been cancelled
Security / CodeQL / Analyze javascript-typescript (push) Has been cancelled
CI and Release / Docker / Publish multi-platform tags (push) Has been cancelled
Deploy / Documentation / deploy (push) Has been cancelled
* refactor(core): reorganize the ReMe runtime and shared helpers * test(status): import the base component from its canonical module * test: remove redundant unit checks * refactor(core): use canonical runtime imports and shared CLI discovery Restore evolve behavior to the base branch and update its imports. Use Application as the runtime entry point and encode SSE in the HTTP service. * fix(cli): honor caller config when service replay is incomplete Use partial startup overrides for port probes and resolve named configs before requesting the service process working directory.
61 lines
2.2 KiB
Python
61 lines
2.2 KiB
Python
"""Application-owned HTTP listener for subprocess clients."""
|
|
|
|
import asyncio
|
|
import socket
|
|
from contextlib import contextmanager
|
|
|
|
import uvicorn
|
|
from starlette.types import ASGIApp
|
|
|
|
from ...constants import REME_IPV4_LOOPBACK_HOST
|
|
|
|
|
|
class _LocalServer(uvicorn.Server):
|
|
"""A server embedded in an application that owns its signal handlers."""
|
|
|
|
@contextmanager
|
|
def capture_signals(self):
|
|
"""Leave signal handling to the containing application."""
|
|
yield
|
|
|
|
|
|
class LoopbackHTTP:
|
|
"""Serve an ASGI application on an ephemeral IPv4 loopback port."""
|
|
|
|
def __init__(self, app: ASGIApp, *, lifespan: str = "auto", shutdown_timeout: int | None = None) -> None:
|
|
self._config = uvicorn.Config(
|
|
app,
|
|
log_config=None,
|
|
access_log=False,
|
|
ws="none",
|
|
lifespan=lifespan,
|
|
timeout_graceful_shutdown=shutdown_timeout,
|
|
)
|
|
self._server: uvicorn.Server | None = None
|
|
self._task: asyncio.Task | None = None
|
|
self._socket: socket.socket | None = None
|
|
|
|
async def listen(self) -> str:
|
|
"""Start serving and return the bound origin."""
|
|
if self._server is None:
|
|
self._socket = socket.socket()
|
|
self._socket.bind((REME_IPV4_LOOPBACK_HOST, 0))
|
|
self._server = _LocalServer(self._config)
|
|
self._task = asyncio.create_task(self._server.serve(sockets=[self._socket]))
|
|
while not self._server.started:
|
|
if self._task.done():
|
|
await self._task
|
|
raise RuntimeError("Loopback HTTP endpoint stopped before startup")
|
|
await asyncio.sleep(0.01)
|
|
return f"http://{REME_IPV4_LOOPBACK_HOST}:{self._socket.getsockname()[1]}"
|
|
|
|
async def close(self) -> None:
|
|
"""Stop serving and close the bound socket, including after failed startup."""
|
|
try:
|
|
if self._server is not None:
|
|
self._server.should_exit = True # pylint: disable=attribute-defined-outside-init
|
|
await self._task
|
|
finally:
|
|
if self._socket is not None:
|
|
self._socket.close()
|
|
self._server = self._task = self._socket = None
|