Skip to content

monitor: deregister after side effects; never join holding the lock - #153

Merged
joshschmelzle merged 2 commits into
devfrom
fix-monitor-lifecycle
Jul 19, 2026
Merged

monitor: deregister after side effects; never join holding the lock#153
joshschmelzle merged 2 commits into
devfrom
fix-monitor-lifecycle

Conversation

@joshschmelzle

Copy link
Copy Markdown
Member

Product-code follow-up from the #152 adversarial review. Two ConnectionMonitor lifecycle defects:

  1. Deregistration before side effects. monitor_loop popped its registry entries before running the dhcp/route/autostart phase, and the early-return paths (stop event set, missing stop event) skipped cleanup entirely. A thread could be invisible to stop_* helpers while its side effects were still pending, which is what let patch teardown race the action phase in the test flake. Cleanup now runs in a finally around the whole body: deregistration happens after all side effects, on every exit path.

  2. Join while holding _monitor_lock. Both stop helpers joined threads while holding the lock that the monitor thread's own deregistration needs, guaranteeing a 2s join timeout in exactly the window where the thread is finishing. Joins now happen outside the lock; registry mutation reacquires it afterward.

No API changes. The #152 test harness (Event-synchronized waits, liveness-checking idle helper) exercises these paths.

Two lifecycle defects found during adversarial review of the delayed-connect
test flake (see #152):

- monitor_loop removed its registry entries BEFORE running the dhcp/route/
  autostart phase (and early-return paths skipped cleanup entirely), so a
  thread could be invisible to stop helpers while side effects were still
  pending. Cleanup now runs in a finally after the body, on every path.
- stop_connection_monitor and stop_all_connection_monitors held _monitor_lock
  across join(), but the monitor thread's own deregistration needs that lock,
  guaranteeing the join times out in exactly the window it matters. Joins now
  happen outside the lock.
@joshschmelzle joshschmelzle self-assigned this Jul 19, 2026
@joshschmelzle
joshschmelzle merged commit 84477d0 into dev Jul 19, 2026
4 of 5 checks passed
@joshschmelzle
joshschmelzle deleted the fix-monitor-lifecycle branch July 19, 2026 13:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant