From 8af1389bc2b032ca249c228f28dc2de569c88dc8 Mon Sep 17 00:00:00 2001 From: Major-333 Date: Mon, 9 Dec 2024 11:46:53 +0800 Subject: [PATCH] fix: prevent timer callback race condition --- internal/core/runner/output_capture.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/internal/core/runner/output_capture.go b/internal/core/runner/output_capture.go index 78211de..d5b1a51 100644 --- a/internal/core/runner/output_capture.go +++ b/internal/core/runner/output_capture.go @@ -1,3 +1,4 @@ + package runner import ( @@ -55,8 +56,11 @@ func (s *OutputCaptureRunner) CaptureOutput(cmd *exec.Cmd) error { if timeout == 0 { timeout = 5 * time.Second } + wgTimer := sync.WaitGroup{} + wgTimer.Add(1) timer := time.AfterFunc(timeout, func() { + defer wgTimer.Done() if cmd != nil && cmd.Process != nil { // write the error s.WriteError([]byte("error: timeout\n")) @@ -154,7 +158,10 @@ func (s *OutputCaptureRunner) CaptureOutput(cmd *exec.Cmd) error { } // stop the timer - timer.Stop() + if !timer.Stop(){ + // timer already triggered + wgTimer.Wait() + } s.done <- true }()