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
16 changes: 15 additions & 1 deletion gateway/shutdown_forensics.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import json
import os
import shutil
import signal
import subprocess
import sys
Expand Down Expand Up @@ -248,14 +249,27 @@ def spawn_async_diagnostic(
except OSError:
return None

# Wrap the snapshot in timeout(1) so a wedged ``ps`` still self-cleans.
# GNU coreutils ships ``timeout`` on Linux; stock macOS has neither
# ``timeout`` nor ``gtimeout`` (the latter arrives with Homebrew
# coreutils). When no timeout binary exists, run bash directly — the
# script's own commands are individually bounded (``head``/``tail``) and
# we detach via ``start_new_session``, so a best-effort snapshot without
# the hard cap is acceptable rather than skipping diagnostics entirely.
timeout_bin = shutil.which("timeout") or shutil.which("gtimeout")
if timeout_bin:
argv = [timeout_bin, f"{timeout_seconds:.0f}", "bash", "-c", script]
else:
argv = ["bash", "-c", script]

try:
# Detach from our process group so the subprocess survives even
# if systemd kills our cgroup with KillMode=control-group (which
# would also reap us anyway, but defense in depth). Without
# start_new_session, a SIGKILL on our cgroup takes the diag down
# before it can flush.
proc = subprocess.Popen(
["timeout", f"{timeout_seconds:.0f}", "bash", "-c", script],
argv,
stdout=fd,
stderr=subprocess.STDOUT,
stdin=subprocess.DEVNULL,
Expand Down
7 changes: 6 additions & 1 deletion tests/gateway/test_background_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,12 @@ async def test_media_files_routed_by_type(self, monkeypatch):
# (default mode requires the file to exist as a regular file).
import os as _os
import tempfile as _tempfile
_tmpdir = _tempfile.mkdtemp(prefix="bg_media_")
# realpath() so the expected paths match what the media-delivery
# validator returns: it resolves symlinks before the denylist check
# (gateway/platforms/base.py validate_media_delivery_path), and on
# macOS the tempdir lives under /var → /private/var. Without this the
# assertions below compare /var/... against the resolved /private/var/...
_tmpdir = _os.path.realpath(_tempfile.mkdtemp(prefix="bg_media_"))
_ogg = _os.path.join(_tmpdir, "clip.ogg")
_mp4 = _os.path.join(_tmpdir, "render.mp4")
_png = _os.path.join(_tmpdir, "chart.png")
Expand Down
8 changes: 7 additions & 1 deletion tests/gateway/test_gateway_shutdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,13 @@ async def test_gateway_stop_systemd_service_restart_exits_cleanly(tmp_path, monk
monkeypatch.setenv("INVOCATION_ID", "systemd-test")
runner._launch_systemd_restart_shortcut = MagicMock()

with patch("gateway.status.remove_pid_file"), patch("gateway.status.write_runtime_status"):
# Pin platform to linux so the systemd clean-exit path is exercised
# regardless of host OS. On macOS the code deliberately keeps a non-zero
# exit for launchd (see test_gateway_stop_launchd_service_restart_keeps_
# nonzero_exit), so without this the assertion fails on a macOS dev box.
with patch("gateway.run.sys.platform", "linux"), patch(
"gateway.status.remove_pid_file"
), patch("gateway.status.write_runtime_status"):
await runner.stop(restart=True, service_restart=True)

runner._launch_systemd_restart_shortcut.assert_called_once_with()
Expand Down