Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.
Go aialertsdashboarddockerdocker-monitoringgohomelabinfrastructure-monitoringkubernetesmonitoringmspproxmoxproxmox-backup-serverproxmox-mail-gatewayproxmox-veself-hostedtruenastypescriptvspherewebhooks
Own audit subprocess cleanup through completion and cancellation
pulse-triage[bot]25c811bad797
diff --git a/scripts/npm-audit-retry.sh b/scripts/npm-audit-retry.shindex 95f086a6a4..355a420fe4 100755--- a/scripts/npm-audit-retry.sh+++ b/scripts/npm-audit-retry.sh@@ -102,35 +102,16 @@ export npm_config_fetch_timeout=$((ATTEMPT_TIMEOUT * 1000))DEADLINE=$(( $(date +%s) + MAX_SECONDS ))-# Run one audit under a hard wall-clock bound, portably: `timeout` is not-# present on every developer machine, so a watchdog subshell kills the npm-# process if it outlives the limit. Blocking on `wait` for the real child-# avoids the zombie-liveness race that a `kill -0` poll would hit.+# Run one audit under a hard wall-clock bound on Linux and macOS. Killing just+# npm or a watchdog shell leaves their children alive after the verdict. The+# Python helper (already required for classification) owns a separate process+# group, stops it on timeout/cancellation and removes leftovers on completion.+# It retains the original report and exit status; it never queries npm again.+audit_runner_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"run_audit() {local limit="$1" out="$2"-- : >"${out}"- "${NPM_BIN}" "${AUDIT_ARGS[@]}" >"${out}" 2>/dev/null &- local npm_pid=$!-- (- sleep "${limit}"- kill -TERM "${npm_pid}" 2>/dev/null- sleep 2- kill -KILL "${npm_pid}" 2>/dev/null- ) >/dev/null 2>&1 &- local killer_pid=$!-- wait "${npm_pid}" 2>/dev/null- local status=$?-- kill -TERM "${killer_pid}" 2>/dev/null- wait "${killer_pid}" 2>/dev/null-- # Keep the observed command status. Even a complete-looking zero summary- # cannot establish a clean audit when npm failed or was stopped afterwards.- # Positive findings in that same response still take precedence.- return "${status}"+ python3 "${audit_runner_dir}/npm_audit_process.py" \+ "${limit}" "${out}" "${NPM_BIN}" "${AUDIT_ARGS[@]}"}# Classify one audit run. Prints a verdict word, summary and (for a finding)diff --git a/scripts/npm_audit_process.py b/scripts/npm_audit_process.pynew file mode 100644index 0000000000..69b7995ac3--- /dev/null+++ b/scripts/npm_audit_process.py@@ -0,0 +1,103 @@+#!/usr/bin/env python3+"""Run one audit with an owned deadline and process group, preserving its bytes."""++from __future__ import annotations++import os+import signal+import subprocess+import sys+import time+from pathlib import Path+++def signal_group(process: subprocess.Popen, signum: int) -> None:+ # start_new_session makes this the audit's group, never the invoking+ # shell's or another job's. The leader may have exited with children left.+ try:+ os.killpg(process.pid, signum)+ except ProcessLookupError:+ pass+++def run_audit(limit: float, output: Path, command: list[str]) -> int:+ process = None+ status = None+ timed_out = False+ interrupted = [None]+ previous_handlers = {}++ def interrupt(signum: int, _frame: object) -> None:+ # Do not raise between Popen creating the child and returning its+ # handle: cleanup must have the actual group identity before it acts.+ interrupted[0] = signum++ try:+ for signum in (signal.SIGTERM, signal.SIGINT, signal.SIGHUP):+ previous_handlers[signum] = signal.signal(signum, interrupt)+ with output.open("wb") as report:+ if interrupted[0] is None:+ process = subprocess.Popen(+ command, stdout=report, stderr=subprocess.DEVNULL,+ start_new_session=True,+ )+ deadline = time.monotonic() + limit+ while interrupted[0] is None:+ remaining = deadline - time.monotonic()+ if remaining <= 0:+ timed_out = True+ break+ try:+ status = process.wait(timeout=min(0.1, remaining))+ break+ except subprocess.TimeoutExpired:+ pass+ if timed_out:+ signal_group(process, signal.SIGTERM)+ try:+ status = process.wait(timeout=2)+ except subprocess.TimeoutExpired:+ pass+ except OSError as exc:+ print(f"npm audit process could not start: {exc.strerror}", file=sys.stderr)+ status = 127+ finally:+ # A second cancellation must not interrupt cleanup. KILL also owns+ # TERM-resistant descendants after a timeout or an already exited+ # leader. No sleeper/watchdog process is needed on the success path.+ for signum in previous_handlers:+ signal.signal(signum, signal.SIG_IGN)+ try:+ if process is not None:+ signal_group(process, signal.SIGKILL)+ observed_status = process.wait()+ if status is None:+ status = observed_status+ finally:+ for signum, handler in previous_handlers.items():+ signal.signal(signum, handler)+ if interrupted[0] is not None:+ return 128 + interrupted[0]+ # A process that exits zero in its TERM handler still missed the deadline.+ # Its zero summary must not become a clean audit.+ if timed_out and status >= 0:+ status = -signal.SIGTERM+ return status if status >= 0 else 128 - status+++def main() -> int:+ if len(sys.argv) < 4:+ print("Usage: npm_audit_process.py seconds report command [args...]", file=sys.stderr)+ return 2+ try:+ limit = float(sys.argv[1])+ if not 0 < limit < float("inf"):+ raise ValueError+ except ValueError:+ print("npm audit process needs a finite positive deadline", file=sys.stderr)+ return 2+ return run_audit(limit, Path(sys.argv[2]), sys.argv[3:])+++if __name__ == "__main__":+ sys.exit(main())diff --git a/scripts/tests/test_npm_audit_retry.py b/scripts/tests/test_npm_audit_retry.pyindex 5b05d16f13..518ddaca55 100755--- a/scripts/tests/test_npm_audit_retry.py+++ b/scripts/tests/test_npm_audit_retry.py@@ -5,9 +5,11 @@ from __future__ import annotationsimport jsonimport osfrom pathlib import Path+import signalimport subprocessimport tempfileimport textwrap+import timeimport unittestfrom unittest.mock import patch@@ -18,6 +20,127 @@ ROOT = Path(__file__).resolve().parents[2]SCRIPT = ROOT / "scripts" / "npm-audit-retry.sh"[email protected](os.name == "posix", "audit process groups require Unix")+class NpmAuditProcessCleanupTest(unittest.TestCase):+ def live(self, pid: int) -> bool:+ result = subprocess.run(+ ["ps", "-o", "stat=", "-p", str(pid)],+ capture_output=True, text=True, check=False,+ )+ state = result.stdout.strip()+ return bool(state) and not state.startswith("Z")++ def run_with_child(self, mode: str, *, finding: bool = False):+ with tempfile.TemporaryDirectory() as directory:+ root = Path(directory)+ pidfile = root / "child.pid"+ npm = root / "npm"+ counts = dict.fromkeys(("info", "low", "moderate", "high", "critical", "total"), 0)+ if finding:+ counts.update(high=1, total=1)+ report = json.dumps({"metadata": {"vulnerabilities": counts}})+ actions = {+ "success": "exit 0",+ "failure": "exit 42",+ "timeout": "wait",+ "timeout-exits-zero": "trap 'exit 0' TERM; wait",+ "interrupted": "wait",+ }+ npm.write_text(textwrap.dedent(f"""\+ #!/bin/sh+ (trap '' TERM; exec sleep 60) &+ printf '%s\\n' "$!" > "$CHILD_PID_FILE"+ printf '%s\\n' '{report}'+ {actions[mode]}+ """), encoding="utf-8")+ npm.chmod(0o755)+ env = os.environ.copy()+ env.update(+ CHILD_PID_FILE=str(pidfile), NPM_AUDIT_CMD=str(npm),+ NPM_AUDIT_ATTEMPTS="1", NPM_AUDIT_ATTEMPT_TIMEOUT="1",+ NPM_AUDIT_MAX_SECONDS="3", NPM_AUDIT_RETRY_DELAY="0",+ NPM_AUDIT_REQUIRE_RESULT="true",+ )+ unrelated = subprocess.Popen(["sleep", "60"], start_new_session=True)+ wrapper = subprocess.Popen(+ ["bash", str(SCRIPT), "all"], cwd=ROOT, env=env,+ stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True,+ start_new_session=True,+ )+ child = None+ started = time.monotonic()+ try:+ if mode == "interrupted":+ deadline = started + 4+ while not pidfile.exists() and time.monotonic() < deadline:+ time.sleep(0.01)+ self.assertTrue(pidfile.exists(), "audit never started")+ # Cancel the caller's actual group. The audit's separate+ # group must be cleaned by its owner, not this signal.+ os.killpg(wrapper.pid, signal.SIGTERM)+ stdout, stderr = wrapper.communicate(timeout=8)+ child = int(pidfile.read_text(encoding="utf-8").strip())+ deadline = time.monotonic() + 1+ while self.live(child) and time.monotonic() < deadline:+ time.sleep(0.01)+ self.assertFalse(self.live(child), f"audit child {child} survived {mode}")+ self.assertIsNone(unrelated.poll(), "audit cleanup killed an unrelated process")+ self.assertLess(time.monotonic() - started, 6, stdout + stderr)+ return wrapper.returncode, stdout, stderr+ finally:+ # These exact fixture children/groups are owned by this test,+ # including on the unfixed parent or an assertion failure.+ if child is None and pidfile.exists():+ child = int(pidfile.read_text(encoding="utf-8").strip())+ if child is not None:+ try:+ os.kill(child, signal.SIGKILL)+ except ProcessLookupError:+ pass+ for process in (wrapper, unrelated):+ try:+ os.killpg(process.pid, signal.SIGKILL)+ except ProcessLookupError:+ pass+ process.wait(timeout=3)+ if wrapper.stdout is not None:+ wrapper.stdout.close()+ if wrapper.stderr is not None:+ wrapper.stderr.close()++ def test_completed_and_failed_audits_clean_children_without_changing_verdict(self):+ for mode, expected in (("success", 0), ("failure", 1)):+ with self.subTest(mode=mode):+ status, stdout, stderr = self.run_with_child(mode)+ self.assertEqual(status, expected, stdout + stderr)+ self.assertEqual("no vulnerabilities" in stdout, mode == "success")+ if mode == "failure":+ self.assertIn("status 42", stdout)++ def test_timeout_cleans_term_resistant_descendants_and_keeps_strict_verdict(self):+ status, stdout, stderr = self.run_with_child("timeout")+ self.assertEqual(status, 1, stdout + stderr)+ self.assertIn("command was stopped", stdout)+ self.assertNotIn("no vulnerabilities", stdout)++ def test_zero_exit_from_timeout_handler_is_not_completed_audit(self):+ status, stdout, stderr = self.run_with_child("timeout-exits-zero")+ self.assertEqual(status, 1, stdout + stderr)+ self.assertIn("status 143", stdout)+ self.assertNotIn("no vulnerabilities", stdout)++ def test_timeout_keeps_captured_positive_findings_without_a_second_query(self):+ status, stdout, stderr = self.run_with_child("timeout", finding=True)+ self.assertEqual(status, 1, stdout + stderr)+ self.assertIn("vulnerabilities present", stdout)+ self.assertNotIn("could not reach the advisory endpoint", stdout)+ self.assertNotIn("retrying", stdout)++ def test_interrupted_caller_cleans_the_separate_audit_group(self):+ status, stdout, stderr = self.run_with_child("interrupted")+ self.assertEqual(status, -signal.SIGTERM, stdout + stderr)++class NpmAuditRetryTest(unittest.TestCase):def run_check(self, mode: str, *arguments: str, require: str = "true", report=None,