Skip to content

The agent that grows with you

fix(webapp-access): Start and Stop restart the gateway after releasing the config lock

emozillaf5713e9e9e8b
diff --git a/hermes_cli/web_routers/webapp_access.py b/hermes_cli/web_routers/webapp_access.py
index f1518dc8d04..91df88571bb 100644
--- a/hermes_cli/web_routers/webapp_access.py
+++ b/hermes_cli/web_routers/webapp_access.py
@@ -82,11 +82,13 @@ async def _in_gateway_home(gateway: Optional[str], fn: Callable[[], Any]) -> Any
raise HTTPException(status_code=400, detail=str(exc)) from exc
-def _restart(gateway_profile: Optional[str], what: str) -> dict:
- """The status after asking the gateway to restart onto the new config. A refused restart (a live
- chat) keeps the saved config and reports ``restart_error``."""
- restart = _restart_gateway_after(gateway_profile, what=what, label="Web Access")
- return {**webapp_access.access_status(), **restart}
+async def _restart(gateway: Optional[str], what: str) -> dict:
+ """The status after asking the *gateway* profile's gateway to restart onto the saved config. Runs
+ after the config write released the config lock, as main's config routes do: the restart spawn
+ and the status query (a control-socket round trip) hold up no config, skills or tools request.
+ A refused restart (a live chat) keeps the saved config and reports ``restart_error``."""
+ restart = await asyncio.to_thread(_restart_gateway_after, gateway, what=what, label="Web Access")
+ return {**await _in_gateway_home(gateway, webapp_access.access_status), **restart}
@router.get("/api/webapp-access/status")
@@ -102,12 +104,8 @@ async def start_webapp_access(body: StartBody, profile: Optional[str] = None):
# The bundle before anything lasting: a failed build registers, writes and restarts nothing.
await _in_gateway_home(gateway, webapp_access.build_bundle)
await _in_gateway_home(gateway, lambda: webapp_access.register(callback))
-
- def _start(gateway):
- webapp_access.serve(host, public_url)
- return _restart(gateway, "turning on Web Access")
-
- return await _scoped(profile, _start, config_write=True)
+ await _scoped(profile, lambda _gateway: webapp_access.serve(host, public_url), config_write=True)
+ return await _restart(gateway, "turning on Web Access")
@router.post("/api/webapp-access/stop")
@@ -116,9 +114,9 @@ async def stop_webapp_access(profile: Optional[str] = None):
def _stop(gateway):
webapp_access.turn_off()
- return _restart(gateway, "turning off Web Access")
+ return gateway
- return await _scoped(profile, _stop, config_write=True)
+ return await _restart(await _scoped(profile, _stop, config_write=True), "turning off Web Access")
@router.post("/api/webapp-access/restart")
@@ -126,4 +124,4 @@ async def restart_webapp_access(profile: Optional[str] = None):
"""Apply the saved config to a gateway that has not restarted onto it yet: restart that gateway
and nothing else (no Portal registration, no build, no config write)."""
profile = destructive_profile(profile, "POST /api/webapp-access/restart")
- return await _scoped(profile, lambda gateway: _restart(gateway, "applying Web Access"))
+ return await _restart(await _scoped(profile, lambda gateway: gateway), "applying Web Access")
diff --git a/tests/hermes_cli/test_webapp_access.py b/tests/hermes_cli/test_webapp_access.py
index f9d75168b35..1fa511c33ee 100644
--- a/tests/hermes_cli/test_webapp_access.py
+++ b/tests/hermes_cli/test_webapp_access.py
@@ -680,3 +680,53 @@ def test_a_webapp_without_a_web_access_client_signs_in_with_the_configured_one(p
await webapp.close()
asyncio.run(scenario())
+
+
+def _lock_is_free(lock) -> bool:
+ """Whether another thread can take *lock* now (the caller's own hold of an RLock does not count)."""
+ import threading
+
+ free: list[bool] = []
+
+ def probe() -> None:
+ taken = lock.acquire(timeout=1)
+ if taken:
+ lock.release()
+ free.append(taken)
+
+ other = threading.Thread(target=probe, daemon=True)
+ other.start()
+ other.join(5)
+ return free == [True]
+
+
[email protected]("action", ["start", "stop", "restart"])
+def test_the_gateway_restart_and_status_run_outside_the_config_lock(client, nous, monkeypatch, action):
+ """Start and Stop write the config under the config lock every config, skills and tools request
+ takes, then restart the gateway and read its status outside that lock: the restart spawn and the
+ status query (a control-socket round trip of up to 5 seconds) hold no other request up."""
+ import hermes_cli.web_server_gateway as gw
+ from hermes_cli import web_server, webapp_access
+
+ lock = web_server._CONFIG_MUTATION_LOCK
+ free_during: dict[str, bool] = {}
+
+ def restart(_profile, *, what, label):
+ free_during["restart"] = _lock_is_free(lock)
+ return {"restart_started": True}
+
+ real_state = webapp_access._gateway_state
+
+ def gateway_state(home):
+ free_during["status"] = _lock_is_free(lock)
+ return real_state(home)
+
+ monkeypatch.setattr(gw, "_restart_gateway_after", restart)
+ monkeypatch.setattr(webapp_access, "_gateway_state", gateway_state)
+ response = {"start": lambda: client.post("/api/webapp-access/start", json={"mode": "lan"}),
+ "stop": lambda: client.post("/api/webapp-access/stop"),
+ "restart": lambda: client.post("/api/webapp-access/restart")}[action]()
+ assert response.status_code == 200, response.text
+ assert free_during == {"restart": True, "status": True}
+
+