From 49bf7e38999b26435b1ad1d4a341906ac1dbbdbe Mon Sep 17 00:00:00 2001 From: Christian Ditscher Date: Mon, 23 Nov 2026 22:25:55 +0100 Subject: [PATCH] device-tunnel: a start right after start no longer runs a second loop start released its lock as soon as it had written $! to the PID file. During the loop's exec chain (nohup, TUNNEL_SHELL, sh) /proc/PID/cmdline is still the start script's or empty, so a start in that window took the PID for a stale one and started a second loop over the PID file; stop then ended only the second, and the first ran on. The loop now records itself and removes the lock. The tests count only the loops of their own config, check after every test that stop ended the loop, and repeat start right after start. --- machine/ssh_tunnel/device-tunnel.sh | 7 ++++++- tests/unit/test_device_tunnel.py | 31 ++++++++++++++++++++++++----- 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/machine/ssh_tunnel/device-tunnel.sh b/machine/ssh_tunnel/device-tunnel.sh index 4942ea5..3962813 100755 --- a/machine/ssh_tunnel/device-tunnel.sh +++ b/machine/ssh_tunnel/device-tunnel.sh @@ -102,9 +102,10 @@ case "${1:-}" in rmdir "$lock" exit 0 fi + # The loop removes the lock once it runs, so a start in between does not + # take the not yet recognisable PID for a stale one and start a second loop. nohup "${TUNNEL_SHELL:-/bin/sh}" "$0" run >/dev/null 2>&1 & echo $! >"$PIDFILE" - rmdir "$lock" log "started (127.0.0.1:$PORT on $JUMP_HOST -> localhost:$LOCAL_PORT)" ;; stop) @@ -126,6 +127,10 @@ case "${1:-}" in ;; run) load_config + # Only now is this PID recognisable as the loop (during its exec chain + # /proc/PID/cmdline is the start script's or empty), so start's lock ends here. + echo $$ >"$PIDFILE" + rmdir "$PIDFILE.lock" 2>/dev/null run_loop ;; *) diff --git a/tests/unit/test_device_tunnel.py b/tests/unit/test_device_tunnel.py index 1c2b1c6..6caa500 100644 --- a/tests/unit/test_device_tunnel.py +++ b/tests/unit/test_device_tunnel.py @@ -49,22 +49,29 @@ def run(action): yield run, calls, tmp_path run("stop") + time.sleep(0.5) + # A loop that outlives stop keeps running after the test, without its config. + assert loops(tmp_path) == [] def loops(tmp_path): - """Loop processes, without their children: BusyBox runs a backgrounded applet - (sleep) in a fork that keeps the loop's command line.""" + """Loop processes of this test's config, without their children: BusyBox runs + a backgrounded applet (sleep) in a fork that keeps the loop's command line. + Loops of other tests or test runs do not count.""" out = subprocess.run( ["pgrep", "-f", f"sh {SCRIPT} run"], capture_output=True, text=True ) - pids = set(out.stdout.split()) + conf = f"TUNNEL_CONF={tmp_path / 'tunnel.conf'}".encode() parents = {} - for pid in pids: + for pid in out.stdout.split(): try: stat = open(f"/proc/{pid}/stat").read() + environ = open(f"/proc/{pid}/environ", "rb").read().split(b"\0") except OSError: continue - parents[pid] = stat.rsplit(")", 1)[1].split()[1] + if conf in environ: + parents[pid] = stat.rsplit(")", 1)[1].split()[1] + pids = set(parents) return sorted(pid for pid, parent in parents.items() if parent not in pids) @@ -132,6 +139,20 @@ def test_parallel_starts_run_one_loop(device): assert len(loops(tmp_path)) == 1 +def test_start_right_after_start_runs_one_loop(device): + """The second start must not take the first loop's PID, still in its exec chain + (empty or start's command line), for a stale one.""" + run, _calls, tmp_path = device + for _ in range(15): + run("start") + run("start") + time.sleep(0.2) + assert len(loops(tmp_path)) == 1 + run("stop") + time.sleep(0.2) + assert loops(tmp_path) == [] + + def test_stale_pid_of_another_program_is_not_the_loop(device): run, _calls, tmp_path = device other = subprocess.Popen(["sleep", "30"])