Fix race simplify logic#2
Merged
gabriel-samfira merged 2 commits intoJun 16, 2026
Merged
Conversation
On shutdown the daemon returned as soon as the done channel was closed, but the goroutine that terminates the runner (keepRunnerAlive) was still running its cleanup. The process could exit before the runner's process group was killed, leaving the runner behind. The service now tracks its goroutines with a WaitGroup and exposes a Wait method. keepRunnerAlive signals the group only after its cleanup defer has run, so both the Unix daemon and the Windows service handler now wait for the runner to be torn down before they return. Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com>
The service read runnerAlive in determineRunnerState, the cli field in loop and Stop, and the connecting/connected channel fields from multiple goroutines without consistent locking. This could cause stale reads, torn pointer values, and a nil dereference if the websocket connected before the runner command was created. runnerAlive is now snapshotted under the lock and passed into determineRunnerState and the status senders. The cli field is owned exclusively by cliMux: keepAliveLoop uses its local after the write, loop reads through getClient, and Stop reads under cliMux. The connecting/connected handshake channels are guarded by a dedicated connMux with snapshot-before-select and two small helpers (connectionUp and connectionDown). loop was restructured from a goto into a nested for loop so the client snapshot does not tangle with label scoping. A reconnect test (TestServiceReconnect) drives the full connect, drop, reconnect cycle against an httptest websocket server under -race. Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com>
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.
This change fixes a number of potential race conditions and simplifies the loops. The agent (and the machine its running on) are expected to be disposed of once shut down. So I was lazy with cleanup. This fixes that.