Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Race condition in Pipe.Exec #193

Open
nclv opened this issue Oct 3, 2023 · 1 comment
Open

Race condition in Pipe.Exec #193

nclv opened this issue Oct 3, 2023 · 1 comment

Comments

@nclv
Copy link

nclv commented Oct 3, 2023

WARNING: DATA RACE
Read at 0x00c000036110 by goroutine 55:
  go/pkg/mod/github.com/bitfield/[email protected]/script.go:388 +0x1e9
  github.com/bitfield/script.(*Pipe).Filter.func1()
      go/pkg/mod/github.com/bitfield/[email protected]/script.go:484 +0xc7

Previous write at 0x00c000036110 by goroutine 49:
  github.com/bitfield/script.(*Pipe).WithStderr()
      go/pkg/mod/github.com/bitfield/[email protected]/script.go:887 +0x798

There seems to be a data race with stdErr when executing the following piece of code in parallel (test with errgroup).

stdOut := new(bytes.Buffer)
stdErr := new(bytes.Buffer)

if _, err := script.Exec(cmdLine).WithStdout(stdOut).WithStderr(stdErr).Stdout(); err != nil {
	return nil, err
}
@bitfield
Copy link
Owner

bitfield commented Oct 4, 2023

Good spotting, @nclv!

It doesn't really make a lot of sense to set the pipe's stdout or stderr after you've already started some command—you may or may not lose some of the output, depending on timing. The more reliable way to do this would be to set the preferred outputs first:

script.NewPipe().WithStdout(stdOut).WithStderr(stdErr).Exec(cmdLine).Stdout()

A data race still exists, of course: this merely adjusts the odds in your favour. The pipe already has a mutex, so we could use this to protect these fields. I'll have a look.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants