Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion machine/ssh_tunnel/device-tunnel.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
;;
*)
Expand Down
31 changes: 26 additions & 5 deletions tests/unit/test_device_tunnel.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down Expand Up @@ -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"])
Expand Down
Loading