Skip to content

Fix runner Stop-before-Start hang and reconcile persist path#19

Merged
TeoSlayer merged 1 commit into
mainfrom
fix/runner-stop-and-persist-path
Jun 22, 2026
Merged

Fix runner Stop-before-Start hang and reconcile persist path#19
TeoSlayer merged 1 commit into
mainfrom
fix/runner-stop-and-persist-path

Conversation

@TeoSlayer

Copy link
Copy Markdown
Contributor

Summary

Two independent fixes in the policy runtime.

runner.go — Stop() before Start() deadlock

PolicyRunner.Stop() unconditionally waited on pr.done, which is only closed by cycleLoop — a goroutine that Start() launches. If Stop() ran before Start() (or without it), the wait blocked forever. Start/Stop are now serialized by a mutex with started/stopped flags: Stop before Start is a safe no-op, Start after Stop is a no-op, and both are idempotent.

runner.go + service.go — persistence path mismatch

NewPolicyRunner resolved its state path via PILOT_HOME (falling back to $HOME/.pilot), but Service.LoadPersisted scanned os.UserHomeDir()/.pilot directly. With PILOT_HOME set, persisted state was written to one directory and scanned for in another, so it was silently ignored. Both now share a single stateDir() helper. LoadPersisted also now actually parses each discovered policy_<netID>.json snapshot and records the persisted network IDs (exposed via PersistedNetworks()) instead of discarding the filename.

Tests

  • TestStopBeforeStartIsNoOp, TestStartThenStop — lifecycle / no-deadlock.
  • TestPersistLoadRoundTripPilotHome — persist+load round-trip under PILOT_HOME.
  • TestLoadPersistedHonorsPilotHome — scan honors the same path.
  • Updated two coverage tests that asserted the old os.UserHomeDir path.

Validation

GOWORK=off go build/vet ./... clean; GOWORK=off go test -race ./... green.

Stop() before Start() waited on the done channel that cycleLoop only
closes after Start() launches it, deadlocking the caller. Guard
Start/Stop with a mutex and started/stopped flags so Stop before Start
is a safe no-op and the lifecycle is idempotent.

NewPolicyRunner honored PILOT_HOME for its state path while
Service.LoadPersisted scanned os.UserHomeDir() directly, so state
written under PILOT_HOME was never discovered. Factor a shared
stateDir() helper used by both, and make LoadPersisted actually parse
each discovered snapshot and record the persisted network IDs
(exposed via PersistedNetworks) instead of silently ignoring them.

Add a persist+load round-trip test plus Stop-before-Start coverage.
@codecov

codecov Bot commented Jun 22, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@TeoSlayer TeoSlayer merged commit 547b631 into main Jun 22, 2026
5 checks passed
@matthew-pilot matthew-pilot deleted the fix/runner-stop-and-persist-path branch June 22, 2026 15:48
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.

2 participants