Skip to content
Closed
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
11 changes: 11 additions & 0 deletions hermes_cli/web_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -1691,6 +1691,17 @@ async def get_action_status(name: str, lines: int = 200):
exit_code = proc.poll()
running = exit_code is None
pid = proc.pid
if not running:
# Reap the finished child to prevent zombie accumulation.
try:
proc.wait(timeout=1)
except Exception:
pass
_ACTION_PROCS.pop(name, None)
# Preserve the result so subsequent polls keep reporting the real
# exit code/pid instead of falling back to None once the handle
# is gone.
_ACTION_RESULTS[name] = {"exit_code": exit_code, "pid": pid}

return {
"name": name,
Expand Down
44 changes: 44 additions & 0 deletions tests/hermes_cli/test_web_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -823,6 +823,50 @@ def fake_spawn(subcommand, name):
assert resp.json() == {"ok": True, "pid": 12345, "name": "hermes-update"}
assert calls == [(["update"], "hermes-update")]

def test_finished_action_proc_is_reaped_and_removed(self):
"""Processes that have exited are reaped via .wait() and removed
from _ACTION_PROCS so they do not accumulate as zombies."""
import hermes_cli.web_server as web_server

waited = []

class FinishedProc:
pid = 99999

def poll(self):
return 0

def wait(self, timeout=None):
waited.append(timeout)
return 0

name = "gateway-restart"
proc = FinishedProc()
web_server._ACTION_PROCS[name] = proc
web_server._ACTION_RESULTS.pop(name, None)
try:
status = self.client.get(f"/api/actions/{name}/status")
assert status.status_code == 200
data = status.json()
assert data["running"] is False
assert data["exit_code"] == 0
assert data["pid"] == 99999
# The proc should have been reaped and removed.
assert waited, "proc.wait() was not called"
assert name not in web_server._ACTION_PROCS

# A second poll, after the handle is gone, must still report the
# real exit code/pid from _ACTION_RESULTS rather than None.
status2 = self.client.get(f"/api/actions/{name}/status")
assert status2.status_code == 200
data2 = status2.json()
assert data2["running"] is False
assert data2["exit_code"] == 0
assert data2["pid"] == 99999
finally:
web_server._ACTION_PROCS.pop(name, None)
web_server._ACTION_RESULTS.pop(name, None)

def test_get_status_filters_unconfigured_gateway_platforms(self, monkeypatch):
import gateway.config as gateway_config
import hermes_cli.web_server as web_server
Expand Down
Loading