Skip to content

Commit 22ef6ec

Browse files
committed
Ensure output and monitoring goroutines complete before finalising job.
1 parent 842ea6a commit 22ef6ec

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

buildbox/script.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"path/filepath"
1414
"errors"
1515
"syscall"
16+
"sync"
1617
)
1718

1819
type Process struct {
@@ -66,6 +67,8 @@ func RunScript(dir string, script string, env []string, callback func(Process))
6667
process.Running = true
6768

6869
var buffer bytes.Buffer
70+
var w sync.WaitGroup
71+
w.Add(2)
6972

7073
go func() {
7174
// Copy the pty to our buffer. This will block until it EOF's
@@ -74,6 +77,7 @@ func RunScript(dir string, script string, env []string, callback func(Process))
7477
if err != nil {
7578
log.Printf("io.Copy failed with error: %s\n", err)
7679
}
80+
w.Done()
7781
}()
7882

7983
go func(){
@@ -87,6 +91,7 @@ func RunScript(dir string, script string, env []string, callback func(Process))
8791
// Sleep for 1 second
8892
time.Sleep(1000 * time.Millisecond)
8993
}
94+
w.Done()
9095
}()
9196

9297
// Wait until the process has finished
@@ -96,6 +101,9 @@ func RunScript(dir string, script string, env []string, callback func(Process))
96101
// of the script
97102
process.Running = false
98103
process.ExitStatus = getExitStatus(waitResult)
104+
105+
// wait for the Copy and incremental output to finish first
106+
w.Wait()
99107
process.Output = buffer.String()
100108

101109
// No error occured so we can return nil

0 commit comments

Comments
 (0)