Skip to content

Commit 247ee96

Browse files
cmaglieAkos Kitta
authored and
Akos Kitta
committed
Fixed a couple of (very rare) race conditions (arduino#2112)
* Fixed concurrent write to variables `err` and `n` * 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.
1 parent 94e8073 commit 247ee96

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

Diff for: arduino/monitor/monitor_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,13 @@ func TestDummyMonitor(t *testing.T) {
7474
go func() {
7575
buff := [1024]byte{}
7676
// Receive "TEST" echoed back
77-
n, err = rw.Read(buff[:])
77+
_, err := rw.Read(buff[:])
7878
require.NoError(t, err)
7979
require.Equal(t, 4, n)
8080
require.Equal(t, "TEST", string(buff[:4]))
8181

8282
// Block on read until the port is closed
83-
n, err = rw.Read(buff[:])
83+
_, err = rw.Read(buff[:])
8484
require.ErrorIs(t, err, io.EOF)
8585
atomic.StoreInt32(&completed, 1) // notify completion
8686
}()

Diff for: executils/process.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,9 @@ func (p *Process) SetEnvironment(values []string) {
160160
// RunWithinContext starts the specified command and waits for it to complete. If the given context
161161
// is canceled before the normal process termination, the process is killed.
162162
func (p *Process) RunWithinContext(ctx context.Context) error {
163+
if err := p.Start(); err != nil {
164+
return err
165+
}
163166
completed := make(chan struct{})
164167
defer close(completed)
165168
go func() {
@@ -169,6 +172,5 @@ func (p *Process) RunWithinContext(ctx context.Context) error {
169172
case <-completed:
170173
}
171174
}()
172-
res := p.cmd.Run()
173-
return res
175+
return p.Wait()
174176
}

0 commit comments

Comments
 (0)