Unverified Commit 5110f0bf authored by Cristian Maglie's avatar Cristian Maglie Committed by GitHub

Fixed a couple of (very rare) race conditions (#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.
parent 183d5e9f
...@@ -74,13 +74,13 @@ func TestDummyMonitor(t *testing.T) { ...@@ -74,13 +74,13 @@ func TestDummyMonitor(t *testing.T) {
go func() { go func() {
buff := [1024]byte{} buff := [1024]byte{}
// Receive "TEST" echoed back // Receive "TEST" echoed back
n, err = rw.Read(buff[:]) _, err := rw.Read(buff[:])
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, 4, n) require.Equal(t, 4, n)
require.Equal(t, "TEST", string(buff[:4])) require.Equal(t, "TEST", string(buff[:4]))
// Block on read until the port is closed // Block on read until the port is closed
n, err = rw.Read(buff[:]) _, err = rw.Read(buff[:])
require.ErrorIs(t, err, io.EOF) require.ErrorIs(t, err, io.EOF)
atomic.StoreInt32(&completed, 1) // notify completion atomic.StoreInt32(&completed, 1) // notify completion
}() }()
......
...@@ -160,6 +160,9 @@ func (p *Process) SetEnvironment(values []string) { ...@@ -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 // 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. // is canceled before the normal process termination, the process is killed.
func (p *Process) RunWithinContext(ctx context.Context) error { func (p *Process) RunWithinContext(ctx context.Context) error {
if err := p.Start(); err != nil {
return err
}
completed := make(chan struct{}) completed := make(chan struct{})
defer close(completed) defer close(completed)
go func() { go func() {
...@@ -169,6 +172,5 @@ func (p *Process) RunWithinContext(ctx context.Context) error { ...@@ -169,6 +172,5 @@ func (p *Process) RunWithinContext(ctx context.Context) error {
case <-completed: case <-completed:
} }
}() }()
res := p.cmd.Run() return p.Wait()
return res
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment