Fix runner Stop-before-Start hang and reconcile persist path#19
Merged
Conversation
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 Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Two independent fixes in the policy runtime.
runner.go — Stop() before Start() deadlock
PolicyRunner.Stop()unconditionally waited onpr.done, which is only closed bycycleLoop— a goroutine thatStart()launches. IfStop()ran beforeStart()(or without it), the wait blocked forever. Start/Stop are now serialized by a mutex withstarted/stoppedflags: 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
NewPolicyRunnerresolved its state path viaPILOT_HOME(falling back to$HOME/.pilot), butService.LoadPersistedscannedos.UserHomeDir()/.pilotdirectly. WithPILOT_HOMEset, persisted state was written to one directory and scanned for in another, so it was silently ignored. Both now share a singlestateDir()helper.LoadPersistedalso now actually parses each discoveredpolicy_<netID>.jsonsnapshot and records the persisted network IDs (exposed viaPersistedNetworks()) 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.os.UserHomeDirpath.Validation
GOWORK=off go build/vet ./...clean;GOWORK=off go test -race ./...green.