1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-11-08 11:16:47 +00:00
webvirtcloud/webvirtcloud/test_child_processes.py
catborise b150a9dd36 fix: stop ignoring SIGCHLD process-wide
The web app (wsgi.py), novncd and socketiod set SIGCHLD to SIG_IGN at
import. The kernel then reaps every child at once, so subprocess loses
the exit status and a failing command reports 0, in the server and in
anything else run in that process: the test suite had to reset the
signal in two tests.

It was added in 2023 against one defunct bash per gunicorn worker and
per novncd. That bash came from the settings template of the time,
which ran .dec_ldap_pwd.sh with Popen and never waited for it; the
template stopped doing that in 561fedf. A settings.py still holding
that line leaves at most one zombie per process, not a growing number.

- wsgi.py, novncd, socketiod: the global SIG_IGN is removed. subprocess
  reaps its own children, websockify installs its own SIGCHLD reaper
  for novncd's connection processes, and novncd's SSH tunnels wait for
  their ssh process.
- socketiod: the pty child execs consolecallback instead of running it
  and then returning into the server's code; disconnect reaps that
  child (waitpid) instead of any child (wait).
- The settings template drops its unused subprocess import; the two
  test workarounds are removed.

New tests load the web app and novncd in a fresh interpreter and check
that SIGCHLD is untouched and a failing command still returns 1; both
fail on the previous code. A patched novncd in the test container
served five connections and left no child behind.
2026-10-04 13:40:53 +03:00

39 lines
1.4 KiB
Python

"""Loading the web app or novncd must not change how child processes are reaped.
With SIGCHLD ignored process-wide, the kernel reaps every child at once and
subprocess loses its exit status: a failing command reports 0.
"""
import subprocess
import sys
import unittest
from pathlib import Path
ROOT = Path(__file__).resolve().parent.parent
CHECK = """
import signal, subprocess
assert signal.getsignal(signal.SIGCHLD) == signal.SIG_DFL, signal.getsignal(signal.SIGCHLD)
print(subprocess.run(["false"]).returncode)
"""
LOAD_NOVNCD = """
import importlib.machinery, importlib.util
loader = importlib.machinery.SourceFileLoader("novncd", "console/novncd")
loader.exec_module(importlib.util.module_from_spec(importlib.util.spec_from_loader("novncd", loader)))
"""
class ChildProcessTestCase(unittest.TestCase):
def run_python(self, code):
result = subprocess.run(
[sys.executable, "-c", code], cwd=ROOT, capture_output=True, text=True, timeout=60
)
self.assertEqual(result.returncode, 0, result.stderr)
return result.stdout.strip().splitlines()[-1]
def test_failing_command_is_nonzero_after_loading_the_web_app(self):
self.assertEqual(self.run_python("import webvirtcloud.wsgi\n" + CHECK), "1")
def test_failing_command_is_nonzero_after_loading_novncd(self):
self.assertEqual(self.run_python(LOAD_NOVNCD + CHECK), "1")