Fix recount stop: kill whole process group, not just shell

This commit is contained in:
proitlab committed 2026-08-05 10:29:41 +07:00
1 parent d909bc85bd
commit 6043197d72
1 file changed
+24 -5
+24 -5
View File
@@ -8,6 +8,7 @@ import logging
import os import os
import re import re
import shlex import shlex
import signal
import subprocess import subprocess
import threading import threading
import time import time
@@ -74,13 +75,31 @@ def _api_get(base_url, path, default=None):
def _stop_recount_locked(): def _stop_recount_locked():
global _recount_proc global _recount_proc
if _recount_proc: proc = _recount_proc
_recount_proc = None
if proc is None:
return
try:
pgid = os.getpgid(proc.pid)
except (ProcessLookupError, PermissionError):
return
try:
os.killpg(pgid, signal.SIGTERM)
except (ProcessLookupError, PermissionError):
pass
try:
proc.wait(timeout=5)
except subprocess.TimeoutExpired:
try: try:
_recount_proc.kill() os.killpg(pgid, signal.SIGKILL)
_recount_proc.wait(timeout=3) except (ProcessLookupError, PermissionError):
pass
try:
proc.wait(timeout=3)
except Exception: except Exception:
pass pass
_recount_proc = None except Exception:
pass
def _folder_date(filepath, base): def _folder_date(filepath, base):
@@ -179,7 +198,7 @@ def start_recount():
_current_file = upload_path _current_file = upload_path
cmd_str = RECOUNT_CMD.replace("{path}", shlex.quote(str(upload_path))) cmd_str = RECOUNT_CMD.replace("{path}", shlex.quote(str(upload_path)))
try: try:
_recount_proc = subprocess.Popen(cmd_str, shell=True) _recount_proc = subprocess.Popen(cmd_str, shell=True, start_new_session=True)
print(f"[{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}] Recount started: {cmd_str}") print(f"[{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}] Recount started: {cmd_str}")
except Exception as e: except Exception as e:
_current_file = None _current_file = None