From 8a2e4b6cc0cad35a9f9da493c86126ea5b990cc6 Mon Sep 17 00:00:00 2001 From: tech Date: Thu, 21 May 2026 12:15:19 +0800 Subject: [PATCH] fix: correct PermissionError handling in is_process_alive, add corrupt PID test --- src/autossh_mgr/process.py | 4 +++- tests/unit/test_process.py | 10 ++++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/autossh_mgr/process.py b/src/autossh_mgr/process.py index b480a17..a5d1486 100644 --- a/src/autossh_mgr/process.py +++ b/src/autossh_mgr/process.py @@ -47,8 +47,10 @@ def is_process_alive(pid: int) -> bool: try: os.kill(pid, 0) return True - except (ProcessLookupError, PermissionError): + except ProcessLookupError: return False + except PermissionError: + return True # process exists but owned by another user def check_stale(config_dir: Path, name: str) -> bool: diff --git a/tests/unit/test_process.py b/tests/unit/test_process.py index 531fe08..cd2b846 100644 --- a/tests/unit/test_process.py +++ b/tests/unit/test_process.py @@ -1,11 +1,9 @@ import os import pytest from datetime import datetime, timezone, timedelta -from unittest.mock import patch from autossh_mgr.process import ( write_pid_file, read_pid_file, delete_pid_file, is_process_alive, check_stale, get_status, format_uptime, - TunnelStatus, ) @@ -95,3 +93,11 @@ def test_format_uptime_hours(): def test_format_uptime_days(): started = datetime.now(timezone.utc) - timedelta(days=1, hours=5) assert format_uptime(started) == "1d 5h" + + +def test_read_pid_file_corrupt(config_dir): + import click + pid_path = config_dir / "pids" / "web-service.pid" + pid_path.write_text("only-one-line\n") + with pytest.raises(click.ClickException, match="Corrupt PID file"): + read_pid_file(config_dir, "web-service")