Skip to content

ServerKit is a lightweight, modern server control panel for managing web applications, databases, and services on your VPS or dedicated server. Built with Python/Flask backend and React frontend.

Check the tail stand-in by pid, not a Popen wrapper

Juan Denis4faac6e1cd73
diff --git a/backend/tests/test_log_tail_stop.py b/backend/tests/test_log_tail_stop.py
index 71565ac2..62138ed6 100644
--- a/backend/tests/test_log_tail_stop.py
+++ b/backend/tests/test_log_tail_stop.py
@@ -8,39 +8,54 @@ import sys
import threading
import time
+import psutil
import pytest
from app.services import log_service
from app.services.log_service import LogService, LogStreamer
-# Stands in for `tail -f` on a log that writes one line and then goes quiet.
-QUIET_TAIL = [sys.executable, '-c',
- 'import time; print("hello", flush=True); time.sleep(600)']
-
@pytest.fixture
def quiet_log(tmp_path, monkeypatch):
+ """A real child that writes one line and then goes quiet, like `tail -f`
+ on an idle log. It records its pid so the test can see whether it died."""
path = tmp_path / 'app.log'
path.write_text('')
+ pid_file = tmp_path / 'tail.pid'
+ stand_in = [sys.executable, '-c',
+ f'import os, time; open({str(pid_file)!r}, "w").write(str(os.getpid())); '
+ f'print("hello", flush=True); time.sleep(600)']
monkeypatch.setattr(LogService, 'is_path_allowed', classmethod(lambda cls, p: True))
- spawned = []
- real_popen = log_service.subprocess.Popen
+ monkeypatch.setattr(log_service, 'privileged_cmd', lambda cmd: stand_in)
+ pids = []
+
+ def tail_pid():
+ if not pids:
+ pids.append(int(pid_file.read_text()))
+ return pids[0]
- def recording_popen(*args, **kwargs):
- proc = real_popen(*args, **kwargs)
- spawned.append(proc)
- return proc
+ yield str(path), tail_pid
+ for pid in pids:
+ try:
+ psutil.Process(pid).kill()
+ except psutil.Error:
+ pass
- monkeypatch.setattr(log_service, 'privileged_cmd', lambda cmd: QUIET_TAIL)
- monkeypatch.setattr(log_service.subprocess, 'Popen', recording_popen)
- yield str(path), spawned
- for proc in spawned:
- if proc.poll() is None:
- proc.kill()
+
+def _gone(pid, seconds=10):
+ deadline = time.monotonic() + seconds
+ while time.monotonic() < deadline:
+ try:
+ if psutil.Process(pid).status() == psutil.STATUS_ZOMBIE:
+ return True
+ except psutil.NoSuchProcess:
+ return True
+ time.sleep(0.1)
+ return False
def test_stop_ends_tail_on_a_quiet_log(quiet_log):
- path, spawned = quiet_log
+ path, tail_pid = quiet_log
lines, stop = [], threading.Event()
worker = threading.Thread(target=LogService.tail_log,
args=(path, lines.append, stop), daemon=True)
@@ -54,18 +69,14 @@ def test_stop_ends_tail_on_a_quiet_log(quiet_log):
stop.set()
worker.join(timeout=10)
assert not worker.is_alive()
- assert spawned[0].poll() is not None
+ assert _gone(tail_pid())
def test_stopping_a_stream_reaps_its_tail(quiet_log):
- path, spawned = quiet_log
+ path, tail_pid = quiet_log
streamer = LogStreamer()
q = streamer.start_stream('sid-1', path)
assert q.get(timeout=10)['line'] == 'hello'
streamer.stop_stream('sid-1')
- for _ in range(100):
- if spawned[0].poll() is not None:
- break
- time.sleep(0.1)
- assert spawned[0].poll() is not None
+ assert _gone(tail_pid())