Fix check-then-set race in Host.Start()#11
Merged
Conversation
Agent-Logs-Url: https://github.com/danielsobrado/P2P-Market-Data/sessions/7bf703f4-b278-4a64-aaeb-cf26942878b5 Co-authored-by: danielsobrado <35833752+danielsobrado@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix remaining lifecycle race in Host.Start() method
Fix check-then-set race in Host.Start()
Mar 27, 2026
danielsobrado
approved these changes
Mar 28, 2026
There was a problem hiding this comment.
Pull request overview
Fixes a lifecycle race in Host.Start() where concurrent callers could both pass the running == false guard before running was set, potentially allowing double-start.
Changes:
- Set
h.running = trueinside the same critical section as theh.runningguard check inHost.Start(). - Remove the late
h.running = trueassignment at the end ofStart(). - Add focused lifecycle tests for
Start/Stopguard behavior andIsRunning()thread-safety.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
pkg/p2p/host/host.go |
Eliminates the check-then-set window by setting running under the same lock as the guard check. |
pkg/p2p/host/host_lifecycle_test.go |
Adds tests for already-running start rejection, idempotent stop when not running, and concurrent IsRunning() reads. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
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.
Host.Start()checkedh.runningunder the lock but released it before settingh.running = true, leaving a window where concurrent callers could both observerunning == falseand both proceed through startup.Changes
pkg/p2p/host/host.go— seth.running = trueatomically within the same critical section as the guard check, eliminating the window entirely:pkg/p2p/host/host_lifecycle_test.go— adds focused tests covering the guard:TestHostStart_GuardAlreadyRunning— single call on a running host is rejectedTestHostStop_IdempotentWhenNotRunning— Stop on a stopped host is a no-opTestHostStart_ConcurrentGuard— 20 concurrent callers on a running host all receive errors (run with-race)TestHostIsRunning_ThreadSafe— 50 concurrentIsRunning()reads produce no data raceOriginal prompt
Create a small follow-up implementation pull request in repository
danielsobrado/P2P-Market-Datato fix a remaining lifecycle race after PR #8.Reference PR: #8
Problem
pkg/p2p/host/host.goimprovedStart()by checkingh.running, but the current implementation still appears to have a race window:h.running == false, unlockh.running = trueThis means double-start may still be possible under concurrent calls.
Goal
Make the host lifecycle transition around
Start()atomic enough to prevent concurrent double-start.Scope
Keep this PR intentionally small and focused.
Likely files:
pkg/p2p/host/host.goRequired implementation work
Host.Start()so concurrent callers cannot both proceed through startup.Acceptance criteria
Host.Start()no longer has the obvious check-then-set race window.The following is the prior conversation context from the user's chat exploration (may be truncated):
Assistant: The pull request to implement the detailed fixes described in PR #6 is being worked on now.
You can track progress directly at PR #6.
Once complete, it will include implementation updates matching its description. Let me know if you need anything else!
User: Check what are the next natural steps
Assistant: Next natural steps for PR #6:
Finish implementation review
mergeable_state: clean.Review the actual diff against the PR description
Run validation before merge
Natural checks to run:
Mark ready for review
Review and merge
Important note
I could confirm:
I could not confirm check run results from the data returned here, so verifying CI/test status is still a natural next step.
Short version
The next natural step is:
review diff → run tests/checks → mark ready for review → merge
If you want, I can next help you inspect whether the diff actually matches the PR description.
User: Do that
Assistant: PR #6’s diff mostly matches its description.
Files changed
The changed files I could confirm are:
pkg/scripts/executor.gopkg/scripts/manager.gopkg/scripts/scripts_test.gopkg/peer/connection.gopkg/peer/discovery/validator.gopkg/peer/discovery/validator_test.gopkg/p2p/message/message.goscripts/proto_to_sql.pyfrontend/src/components/data/utils/handleScripts.tsfrontend/src/App.tsxWhat the diff clearly covers
1. Script lifecycle hardening
Covered well.
Evidence in diff:
pkg/scripts/executor.goscriptRun{cmd, done}Wait()patternStopScriptnow does graceful stop → timeout → killExecuteScriptnow returns result+error togetherExecuteScriptWithOutputCapturenow trims output and returns parsing errors with raw outputexec.LookPathpkg/scripts/manager.goos.CreateTemppkg/scripts/scripts_test.goAssessment: this part strongly matches the PR description.
2. P2P correctness fixes
Mostly covered.
Evidence in diff:
pkg/peer/connection.goDisconnectPeerpkg/peer/discovery/validator.goThis pull request was created from Copilot chat.
⌨️ Start Copilot coding agent tasks without leaving your editor — available in VS Code, Visual Studio, JetBrains IDEs and Eclipse.