Skip to content

fix(daemon): stop counting zombie sidecar leaders as alive - #3302

Closed
github-actions[bot] wants to merge 1 commit into
mainfrom
autofix/issue-3300
Closed

github-actions[bot] wants to merge 1 commit into
mainfrom
autofix/issue-3300

Conversation

@github-actions

@github-actions github-actions Bot commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

If the daemon was killed while a sidecar's leader process had exited but not yet been reaped, the next daemon start left the sidecar's orphaned child running and holding its port. The startup reaper counted that zombie leader as "alive", took the wrong branch, logged "pid reused, leaving it alone", and skipped the group-kill recovery that exists for exactly this situation — so the port stayed occupied and the next spawn had to work around it. Zombies now correctly read as dead and get cleaned up.

Closes #3300

Test plan

  • python -m pytest tests/unit/test_daemon_agents_routes.py -x passes (includes two new tests that fail against the old implementation)
  • python -m pytest tests/unit/ -x passes
  • python util/lint.py --all passes
🔍 Technical details

Root cause. _pid_alive in src/gaia/daemon/sidecars/ledger.py was a bare psutil.pid_exists(), which returns True for a zombie. In reap_stale the zombie then took the live-pid branch, failed _cmdline_matches (a zombie's cmdline is empty on Linux), and fell through to the "pid reused" log — never reaching the _group_kill(pid) branch designed for "leader died, re-parented child survives".

Fix. _pid_alive now delegates to gaia.daemon.instance.pid_alive, the same helper #3229 adopted in sidecars/registry.py. That includes its AccessDenied → pid_exists fallback, which is the right call here too: a pid we can't inspect is one we also can't safely kill, and the cmdline gate below already fails closed for it. _pid_alive stays a function so it remains the monkeypatch seam the existing reap tests use. The reap_stale docstring now says a zombie counts as dead.

Tests (tests/unit/test_daemon_agents_routes.py): test_pid_alive_excludes_zombies pins the helper directly; test_reap_stale_zombie_leader_takes_the_group_kill_path runs the real _pid_alive against a faked psutil and asserts the group-kill path. Both fail on the pre-fix implementation — verified by stashing the source change and re-running.

Out of scope, as the issue notes: the copies in eval/sidecar_harness.py and eval/runner.py are lock-holder checks where treating an uncertain pid as alive is the deliberate safe direction.

The ledger's startup reaper treated an unreaped (zombie) sidecar leader as
a live process, so it took the cmdline-identity branch, failed to match a
zombie's empty cmdline, and logged "pid reused, leaving it alone" — the
re-parented child still holding the port was never cleaned up. Zombies now
route to the group-kill branch that exists for exactly that case.

_pid_alive now delegates to gaia.daemon.instance.pid_alive, the same helper
#3229 adopted in sidecars/registry.py, including its AccessDenied ->
pid_exists fallback: a pid we cannot inspect is one we also cannot safely
kill, and the cmdline gate below already fails closed for it.

Closes #3300
itomek added a commit that referenced this pull request Sep 2, 2026
#3302 fixed #3300 independently while this branch was in flight. Its version
is better in three ways, so take it wholesale rather than keep mine:

- it pins os.name to "posix", without which the assertion would fail on
  Windows, where the dead-leader branch returns before the group-kill
- it tests _pid_alive directly in both directions, not only through the
  reaper, which is the tighter regression anchor
- it updates the reap_stale docstring, which the behaviour change had
  otherwise left describing the old classification

#3302 is superseded by this branch and closed.
@itomek

itomek commented Sep 2, 2026

Copy link
Copy Markdown
Collaborator

Superseded by #3229, which now carries this fix — closing.

Your implementation was better than the one #3229 had, so I took yours wholesale rather than keep mine: the os.name pin (without which the assertion fails on Windows, where the dead-leader branch returns before the group-kill), the direct two-directional _pid_alive test, and the reap_stale docstring update.

The two landed together in #3229 because they're the same bug in the same subsystem — the stop path and the reap path both counted a zombie as alive.

@itomek itomek closed this Sep 2, 2026
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.

fix: ledger._pid_alive promises liveness but counts zombies as alive

1 participant