Skip to content

device-tunnel: a start right after start no longer runs a second loop - #2

Open
ditschi wants to merge 1 commit into
mainfrom
claude/dazzling-torvalds-052238
Open

ditschi wants to merge 1 commit into
mainfrom
claude/dazzling-torvalds-052238

Conversation

@ditschi

@ditschi ditschi commented Nov 23, 2026

Copy link
Copy Markdown
Owner

Problem

pytest tests/unit/test_device_tunnel.py failed on main (5 of 6) on this host. A test run had leaked a loop (device-tunnel.sh run) that nothing stopped, and loops(tmp_path) counted every loop of the script on the host, not only the test's own.

The leak came from the script. start released its lock right after writing $! 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. A start in that window took the PID for a stale one, started a second loop and overwrote the PID file. stop then ended only the second loop. Back-to-back starts reproduced this in 7 of 40 rounds. It breaks the ssh-tunnel requirement that running the script again while a tunnel runs does not start a second one.

Change

  • machine/ssh_tunnel/device-tunnel.sh: start keeps the lock. The loop writes its own PID to the PID file and then removes the lock, so a start in between exits 0. If the loop dies earlier, the existing stale-lock handling (one minute) clears the lock.
  • tests/unit/test_device_tunnel.py:
    • loops() counts only loops with the test's own TUNNEL_CONF.
    • The fixture asserts that stop ended the loop.
    • New test_start_right_after_start_runs_one_loop repeats start + start 15 times. The race is timing-dependent; it failed one of three runs against the old script.

Validation

  • Back-to-back start stress test: 0 of 80 rounds leaked after the fix, 7 of 40 before.
  • pytest tests/unit/test_device_tunnel.py -q: 7 passed, three runs in a row.
  • pre-commit run --files machine/ssh_tunnel/device-tunnel.sh tests/unit/test_device_tunnel.py: passed.

Not changed: stop does not take the lock, so a stop right after start could still miss a loop that is just starting. This does not affect the boot task and watchdog, which only run start.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant