Skip to content

Conversation

@mahadzaryab1
Copy link
Contributor

@mahadzaryab1 mahadzaryab1 commented Aug 16, 2024

There was a race condition that existed in Pipe.Exec, which spins up a goroutine that access Pipe.stderr without a lock. This PR fixes that by protecting Pipe.stderr with Pipe.mu. This PR fixes #193.

Testing
Created a simple unit test as follows:

func TestDataRaceStdErr(t *testing.T) {
	stdOut := new(bytes.Buffer)
	stdErr := new(bytes.Buffer)

	_, err := script.Exec("echo").WithStdout(stdOut).WithStderr(stdErr).Stdout()
	if err != nil {
		t.Fatal(err)
	}
}

Running go test -race before the changes in this PR outputted the same warning seen in the issue. Running it again with the changes in the PR cause the tests to pass.

@mahadzaryab1
Copy link
Contributor Author

question: we currently do not run go test in the CI with the -race flag so I removed the regression test. Do we want to run the tests with the -race flag? If so, should I add the regression test back in?

@bitfield
Copy link
Owner

If we can have race conditions, we should definitely run CI tests with the race detector on!

@mahadzaryab1
Copy link
Contributor Author

If we can have race conditions, we should definitely run CI tests with the race detector on!

Added the -race flag to ci.yml and added a regression test.

Copy link
Owner

@bitfield bitfield left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good so far!

@bitfield
Copy link
Owner

bitfield commented Sep 1, 2024

@mahadzaryab1 I'm happy if you're happy—do you want to go ahead and do a final quality pass, checking documentation comments, alphabetical order, gotestdox-friendly test names, and so on? You know the house style now 😄

@mahadzaryab1
Copy link
Contributor Author

@mahadzaryab1 I'm happy if you're happy—do you want to go ahead and do a final quality pass, checking documentation comments, alphabetical order, gotestdox-friendly test names, and so on? You know the house style now 😄

@bitfield Done! Let me know if there's anything I missed.

Copy link
Owner

@bitfield bitfield left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Super nice, @mahadzaryab1! Thanks a lot. I'll wait till you're done on #208, and then we can roll all these changes up into a new release.

@bitfield bitfield merged commit 0edd895 into bitfield:master Sep 2, 2024
bcho added a commit to b4fun/script-contextual that referenced this pull request Oct 3, 2024
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

Successfully merging this pull request may close these issues.

Race condition in Pipe.Exec

3 participants