From c92e004cf1545b01f7451b7e03533e931d1fc029 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Wed, 15 Mar 2023 11:27:46 +0100 Subject: [PATCH 1/2] Fixed concurrent write to variables `err` and `n` --- arduino/monitor/monitor_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/arduino/monitor/monitor_test.go b/arduino/monitor/monitor_test.go index 0ccae7f7fe0..cf45771c4b1 100644 --- a/arduino/monitor/monitor_test.go +++ b/arduino/monitor/monitor_test.go @@ -74,13 +74,13 @@ func TestDummyMonitor(t *testing.T) { go func() { buff := [1024]byte{} // Receive "TEST" echoed back - n, err = rw.Read(buff[:]) + _, err := rw.Read(buff[:]) require.NoError(t, err) require.Equal(t, 4, n) require.Equal(t, "TEST", string(buff[:4])) // Block on read until the port is closed - n, err = rw.Read(buff[:]) + _, err = rw.Read(buff[:]) require.ErrorIs(t, err, io.EOF) atomic.StoreInt32(&completed, 1) // notify completion }() From 2285176f0d534f765407c69e0b600534a9a345d9 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Wed, 15 Mar 2023 11:28:10 +0100 Subject: [PATCH 2/2] Fix concurrent access to p.cmd.Process The go-routine was spawned before the process was started. This means that p.Kill may read p.cmd.Process before it is written in p.Run. --- executils/process.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/executils/process.go b/executils/process.go index 08e5e626052..6044275af85 100644 --- a/executils/process.go +++ b/executils/process.go @@ -160,6 +160,9 @@ func (p *Process) SetEnvironment(values []string) { // RunWithinContext starts the specified command and waits for it to complete. If the given context // is canceled before the normal process termination, the process is killed. func (p *Process) RunWithinContext(ctx context.Context) error { + if err := p.Start(); err != nil { + return err + } completed := make(chan struct{}) defer close(completed) go func() { @@ -169,6 +172,5 @@ func (p *Process) RunWithinContext(ctx context.Context) error { case <-completed: } }() - res := p.cmd.Run() - return res + return p.Wait() }