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.
Python astracloudflarecontrol-panelcyberpaneldnsfablehosting-control-panelhosting-panelkimi-k3server-managementserverkitubuntu-web-panelwebserver
End tail -f when a log stream stops
Juan Denis9b0323ebfb99
diff --git a/backend/app/jobs/thread_ownership.py b/backend/app/jobs/thread_ownership.pyindex 85e414a0..c7a31c53 100644--- a/backend/app/jobs/thread_ownership.py+++ b/backend/app/jobs/thread_ownership.py@@ -119,6 +119,11 @@ THREAD_OWNERSHIP = {'lifecycle': LIFECYCLE_REQUEST_STREAM,'rationale': 'Tail lifetime is coupled to a caller-provided stop event.',},+ 'app/services/log_service.py:tail_log:_stop_when_asked': {+ 'owner': 'log stream session',+ 'lifecycle': LIFECYCLE_REQUEST_STREAM,+ 'rationale': 'Waits on the stream stop event and kills tail, which a blocked read cannot notice.',+ },'app/services/metrics_history_service.py:start_collection:collection_loop': {'owner': 'metrics history collector','lifecycle': LIFECYCLE_PROCESS_LOOP,diff --git a/backend/app/services/log_service.py b/backend/app/services/log_service.pyindex a2a3081e..fb948cfc 100644--- a/backend/app/services/log_service.py+++ b/backend/app/services/log_service.py@@ -437,23 +437,34 @@ class LogService:process = subprocess.Popen(privileged_cmd(['tail', '-f', '-n', '0', filepath]),stdout=subprocess.PIPE,- stderr=subprocess.PIPE,+ stderr=subprocess.DEVNULL,text=True)+ except Exception as e:+ callback({'error': str(e)})+ return- while True:- if stop_event and stop_event.is_set():- process.terminate()- break-- line = process.stdout.readline()- if line:- callback({'line': line.strip(), 'filepath': filepath})- elif process.poll() is not None:- break-+ # readline() blocks until the log grows, so a stop checked between+ # lines never fired on a quiet log and `tail -f` ran forever. Killing+ # the process from a watcher closes stdout, which ends the read.+ if stop_event is not None:+ def _stop_when_asked():+ stop_event.wait()+ process.terminate()+ threading.Thread(target=_stop_when_asked, daemon=True).start()+ try:+ for line in process.stdout:+ callback({'line': line.strip(), 'filepath': filepath})except Exception as e:callback({'error': str(e)})+ finally:+ if process.poll() is None:+ process.terminate()+ try:+ process.wait(timeout=5)+ except subprocess.TimeoutExpired:+ process.kill()+ process.wait()@staticmethoddef _format_size(size: int) -> str:diff --git a/backend/app/sockets.py b/backend/app/sockets.pyindex ef10b41f..479b9425 100644--- a/backend/app/sockets.py+++ b/backend/app/sockets.py@@ -460,17 +460,19 @@ def handle_subscribe_logs(data):# Start log streamlog_queue = log_streamer.start_stream(sid, filepath)- # Create thread to emit log updates+ # Emit until this stream is stopped or replaced. A quiet log is not the+ # end of the stream: giving up after 30s without a line left the client+ # silently subscribed to nothing.def emit_logs():- while True:+ while log_streamer.queues.get(sid) is log_queue:try:- log_data = log_queue.get(timeout=30)- if 'error' in log_data:- socketio.emit('log_error', log_data, room=sid)- break- socketio.emit('log_line', log_data, room=sid)- except:+ log_data = log_queue.get(timeout=5)+ except queue.Empty:+ continue+ if 'error' in log_data:+ socketio.emit('log_error', log_data, room=sid)break+ socketio.emit('log_line', log_data, room=sid)thread = threading.Thread(target=emit_logs, daemon=True)thread.start()diff --git a/backend/tests/test_log_tail_stop.py b/backend/tests/test_log_tail_stop.pynew file mode 100644index 00000000..71565ac2--- /dev/null+++ b/backend/tests/test_log_tail_stop.py@@ -0,0 +1,71 @@+"""Stopping a log stream must end `tail -f` even when the log is quiet.++The stop flag was checked between lines, and readline() blocks until the+log grows, so on a quiet log the stop never fired: every closed log viewer+left a `sudo tail -f` running for the life of the panel.+"""+import sys+import threading+import time++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)']+++def quiet_log(tmp_path, monkeypatch):+ path = tmp_path / 'app.log'+ path.write_text('')+ monkeypatch.setattr(LogService, 'is_path_allowed', classmethod(lambda cls, p: True))+ spawned = []+ real_popen = log_service.subprocess.Popen++ def recording_popen(*args, **kwargs):+ proc = real_popen(*args, **kwargs)+ spawned.append(proc)+ return proc++ 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 test_stop_ends_tail_on_a_quiet_log(quiet_log):+ path, spawned = quiet_log+ lines, stop = [], threading.Event()+ worker = threading.Thread(target=LogService.tail_log,+ args=(path, lines.append, stop), daemon=True)+ worker.start()+ for _ in range(100):+ if lines:+ break+ time.sleep(0.1)+ assert lines == [{'line': 'hello', 'filepath': path}]++ stop.set()+ worker.join(timeout=10)+ assert not worker.is_alive()+ assert spawned[0].poll() is not None+++def test_stopping_a_stream_reaps_its_tail(quiet_log):+ path, spawned = 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