integration: lock buffer for concurrent writes

Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
v0.8
Tonis Tiigi 2020-07-23 12:04:14 -07:00
parent 754bcfbe51
commit 9cb0654ce4
1 changed files with 16 additions and 2 deletions

View File

@ -4,10 +4,12 @@ import (
"bytes"
"context"
"fmt"
"io"
"net"
"os"
"os/exec"
"strings"
"sync"
"syscall"
"time"
@ -19,10 +21,10 @@ func startCmd(cmd *exec.Cmd, logs map[string]*bytes.Buffer) (func() error, error
if logs != nil {
b := new(bytes.Buffer)
logs["stdout: "+cmd.Path] = b
cmd.Stdout = b
cmd.Stdout = &lockingWriter{Writer: b}
b = new(bytes.Buffer)
logs["stderr: "+cmd.Path] = b
cmd.Stderr = b
cmd.Stderr = &lockingWriter{Writer: b}
}
fmt.Fprintf(cmd.Stderr, "> startCmd %v %+v\n", time.Now(), cmd.Args)
@ -130,3 +132,15 @@ func requireRoot() error {
}
return nil
}
type lockingWriter struct {
mu sync.Mutex
io.Writer
}
func (w *lockingWriter) Write(dt []byte) (int, error) {
w.mu.Lock()
n, err := w.Writer.Write(dt)
w.mu.Unlock()
return n, err
}