Skip to content

Commit

Permalink
Merge pull request #34 from aaomidi/amir/fix_race_condition
Browse files Browse the repository at this point in the history
Fix race condition in the runner
  • Loading branch information
dzbarsky authored Oct 16, 2024
2 parents 3ef4e9b + 4d45faa commit 45a4eb7
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ jobs:

- name: Test
working-directory: tests
run: bazel test //...
run: bazel test --@rules_go//go/config:race --@rules_go//go/config:pure=false //...
6 changes: 5 additions & 1 deletion runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,11 @@ func initializeServiceCmd(ctx context.Context, instance *ServiceInstance) error
instance.cmd = cmd
instance.killed = false
instance.startErrFn = sync.OnceValue(cmd.Start)
instance.waitErrFn = sync.OnceValue(cmd.Wait)
instance.waitErrFn = sync.OnceValue(func() error {
res := cmd.Wait()
instance.SetDone()
return res
})

if s.HotReloadable {
stdin, err := cmd.StdinPipe()
Expand Down
16 changes: 15 additions & 1 deletion runner/service_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type ServiceInstance struct {
runErr error
killed bool
healthcheckAttempted bool
done bool
}

func (s *ServiceInstance) Start(ctx context.Context) error {
Expand Down Expand Up @@ -212,9 +213,10 @@ func (s *ServiceInstance) Stop(sig syscall.Signal) error {
return err
}

for s.cmd.ProcessState == nil {
for !s.isDone() {
time.Sleep(5 * time.Millisecond)
}

return nil
}

Expand All @@ -236,3 +238,15 @@ func (s *ServiceInstance) Killed() bool {
defer s.mu.Unlock()
return s.killed
}

func (s *ServiceInstance) isDone() bool {
s.mu.Lock()
defer s.mu.Unlock()
return s.done && s.cmd.ProcessState != nil
}

func (s *ServiceInstance) SetDone() {
s.mu.Lock()
defer s.mu.Unlock()
s.done = true
}

0 comments on commit 45a4eb7

Please sign in to comment.