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

fix(race-conditon): fix data race condition when accessing stderr on pipe #209

Merged
merged 11 commits into from
Sep 2, 2024

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.

script.go Outdated Show resolved Hide resolved
script.go Outdated Show resolved Hide resolved
script.go Show resolved Hide resolved
script.go Outdated Show resolved Hide resolved
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!

script_test.go Outdated Show resolved Hide resolved
mahadzaryab1 and others added 2 commits September 1, 2024 11:43
Signed-off-by: Mahad Zaryab <mahadzaryab1@gmail.com>
@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 😄

Signed-off-by: Mahad Zaryab <mahadzaryab1@gmail.com>
@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.

mahadzaryab1 and others added 2 commits September 1, 2024 12:41
Signed-off-by: Mahad Zaryab <mahadzaryab1@gmail.com>
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
8 checks passed
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