Skip to content

Commit

Permalink
Address pipe-read race condition
Browse files Browse the repository at this point in the history
cmd.Wait() must not be called before reading completes; it was previously being called too early.
  • Loading branch information
jefferbrecht committed Nov 21, 2024
1 parent 19a24c5 commit 6a0d81b
Showing 1 changed file with 16 additions and 15 deletions.
31 changes: 16 additions & 15 deletions transformation_test/transformation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -444,9 +444,24 @@ func (transformationConfig transformationTest) runOTelTestInner(t *testing.T, na
}

var errors []any
var exitErr error

// Read from stderr until EOF and put any errors in `errors`.
eg.Go(func() error {
// Wait for the process to exit.
defer eg.Go(func() error {
if err := cmd.Wait(); err != nil {
if _, ok := err.(*exec.ExitError); ok {
exitErr = err
t.Logf("process terminated with error: %v", err)
} else {
return fmt.Errorf("process failed: %w", err)
}
}
cancel()
return nil
})

consumingCount := 0
r := bufio.NewReader(stderr)
d := json.NewDecoder(r)
Expand Down Expand Up @@ -498,6 +513,7 @@ func (transformationConfig transformationTest) runOTelTestInner(t *testing.T, na
}
}
})

// Read and sanitize requests.
eg.Go(func() error {
for r := range requestCh {
Expand All @@ -506,21 +522,6 @@ func (transformationConfig transformationTest) runOTelTestInner(t *testing.T, na
return nil
})

var exitErr error
// Wait for the process to exit.
eg.Go(func() error {
if err := cmd.Wait(); err != nil {
if _, ok := err.(*exec.ExitError); ok {
exitErr = err
t.Logf("process terminated with error: %v", err)
} else {
return fmt.Errorf("process failed: %w", err)
}
}
cancel()
return nil
})

if err := eg.Wait(); err != nil {
t.Errorf("errgroup failed: %v", err)
}
Expand Down

0 comments on commit 6a0d81b

Please sign in to comment.