Skip to content

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

Merged
bitfield merged 11 commits into
bitfield:masterfrom
mahadzaryab1:fix-data-race
Sep 2, 2024
Merged

bitfield merged 11 commits into
bitfield:masterfrom
mahadzaryab1:fix-data-race

Conversation

@mahadzaryab1

@mahadzaryab1 mahadzaryab1 commented Aug 16, 2024 •

Copy link
Copy Markdown
Contributor

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
Copy Markdown
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
Copy Markdown
Owner

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

@mahadzaryab1

Copy link
Copy Markdown
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.

Comment thread script.go Resolved
Comment thread script.go Resolved
Comment thread script.go Resolved
Comment thread script.go Resolved

@bitfield bitfield left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Looking good so far!

Comment thread script_test.go Resolved
mahadzaryab1 and others added 2 commits September 1, 2024 11:43
Signed-off-by: Mahad Zaryab <mahadzaryab1@gmail.com>
@bitfield

bitfield commented Sep 1, 2024

Copy link
Copy Markdown
Owner

@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
Copy Markdown
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>

@bitfield bitfield left a comment

Copy link
Copy Markdown
Owner

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