Skip to content

fix(herdr): keep one pane-tag watcher per pane - #197

Open
ZviBaratz wants to merge 1 commit into
uwuclxdy:mommyfrom
ZviBaratz:fix/watcher-pidfile-race
Open

ZviBaratz wants to merge 1 commit into
uwuclxdy:mommyfrom
ZviBaratz:fix/watcher-pidfile-race

Conversation

@ZviBaratz

Copy link
Copy Markdown

Summary

This PR keeps the herdr plugin's pane-tag watcher at one per pane.

report-profile.sh claims watch-<pane>.pid with its own pid, starts watch-profile.sh in the background, and exits. The watcher writes its own pid only after clauth herdr config get tag_watch_secs returns. A hook run that lands in that window reads a dead pid, so kill -0 fails and it starts a second watcher. That watcher then polls for as long as the pane lives. herdr starts each event hook on its own thread without waiting for it (src/app/api/plugins/runtime.rs), so the hooks for events that arrive close together, as when an agent starts, can overlap that window. I saw 38 live watchers against at most 29 panes. Whichever duplicate exits first also deletes the pidfile its sibling owns, which invites another spawn.

A second gap: trap 'rm -f "$pidfile"' EXIT does not run when the shell is killed by SIGTERM or SIGHUP, so the pidfile outlives the watcher. Its pid can later belong to an unrelated process, and then kill -0 suppresses the pane's watcher.

Changes:

  • report-profile.sh writes the watcher's pid ($!) before it exits. A failed write does not fail the hook.
  • watch-profile.sh claims the pidfile before anything else.
  • It releases the pidfile only while the file still names it.
  • It ends on its next tick when the pidfile names another watcher. Two hook runs that both find a stale pidfile can still each start one, and this bounds the duplicate to a single tick.
  • It traps HUP and TERM (and INT, which a background spawn ignores, when run by hand), installed before the pidfile write, so a signalled watcher runs its EXIT trap. A trapped signal acts once the current sleep returns, so stopping a watcher now takes up to one interval.

Tests (in tests/inline/herdr.rs, on the existing chain harness):

  • a_hook_while_the_watcher_starts_spawns_no_second_watcher slows the tag_watch_secs answer to hold the window open, then runs two reports back to back.
  • a_watcher_the_pidfile_does_not_name_ends_and_leaves_the_pidfile
  • a_watcher_ended_by_sigterm_releases_its_pidfile

All three fail on the previous scripts and pass with this change. The herdr:: tests passed three runs in a row with --test-threads=16.

Not covered: a pidfile can still be read empty mid-write, or recreated naming a dead pid if report-profile.sh is delayed past a watcher that exits on its first tick. Both heal on the next hook; rex-plugin/start-watch.sh's mkdir lock would close them, and I kept this change smaller instead. Also unchanged: outside herdr the state dir falls back to ${TMPDIR:-/tmp}/clauth, where the pidfile writes (the watcher's, as before, and now this one) would follow a link another local user planted. Under herdr HERDR_PLUGIN_STATE_DIR is a private directory; the fallback is worth its own change.

Checklist

  • Secrets redacted from any pasted output (no sk-ant-*, no ~/.claude/~/.clauth contents)
  • cargo fmt --all -- --check passes locally, and cargo test --locked --all-features passes in full (13,570 tests). cargo clippy --locked --all-targets --all-features -- -D warnings on rustc 1.98.0 reports one drop_non_drop at tests/inline/runtime.rs:25835, which this PR does not touch and which mommy's CI does not hit. With that lint allowed, clippy is clean.

AI

  • AI did most of it under my direction

🤖 Generated with Claude Code

report-profile.sh claimed the pidfile with its own pid, started the watcher
and exited. The watcher wrote its own pid only after asking clauth for its
interval. A hook run that landed in that window read a dead pid and started
a second watcher, which then polled for as long as the pane lived. herdr
starts each event hook on its own thread without waiting for it, so the
hooks for events close together (an agent starting) can overlap that window.

- report-profile.sh writes the watcher's pid ($!) before it exits; a failed
  write does not fail the hook.
- watch-profile.sh claims the pidfile before anything else. It releases the
  pidfile only while it still names itself, and it ends on its next tick when
  the pidfile names another watcher. Two hook runs that both find a stale
  pidfile can still each start one, and this bounds the duplicate to a tick.
- watch-profile.sh traps HUP and TERM (and INT, when run by hand), installed
  before the pidfile write, so a signalled watcher runs its EXIT trap and no
  longer leaves its pidfile behind.

Three tests drive the real scripts. All three fail on the previous scripts.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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