Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@juliaogris
Copy link
Member

Fix data race in go tests detected by Go's race detector. When investigating a
spurious CI failure in the tests, we found that NewTestServer() in combination
with test server method SetHTTPHandler causes a race as reported by the
detector:

WARNING: DATA RACE
Read at 0x00c000354220 by goroutine 10:
  foxygo.at/jig/serve.(*Server).Serve()
      /Users/julia/Development/jig/serve/server.go:102 +0x208
  foxygo.at/jig/serve.(*TestServer).Start.func1()
      /Users/julia/Development/jig/serve/server.go:269 +0x50

Previous write at 0x00c000354220 by goroutine 7:
  foxygo.at/jig/serve.(*Server).SetHTTPHandler()
      /Users/julia/Development/jig/serve/server.go:96 +0x1ec
  foxygo.at/jig/serve/httprule.TestHTTP()
      /Users/julia/Development/jig/serve/httprule/handler_test.go:29 +0x1dc

Fix by using NewUnstartedTestServer and explicitly and synchronously calling
Start method after calling SetHTTPHandler.

Add the -race flag in Makefile to Go test execution.

Fix data race in go tests detected by Go's race detector. When investigating a
spurious CI failure in the tests, we found that `NewTestServer()` in combination
with test server method `SetHTTPHandler` causes a race as reported by the
detector:

	WARNING: DATA RACE
	Read at 0x00c000354220 by goroutine 10:
	  foxygo.at/jig/serve.(*Server).Serve()
	      /Users/julia/Development/jig/serve/server.go:102 +0x208
	  foxygo.at/jig/serve.(*TestServer).Start.func1()
	      /Users/julia/Development/jig/serve/server.go:269 +0x50

	Previous write at 0x00c000354220 by goroutine 7:
	  foxygo.at/jig/serve.(*Server).SetHTTPHandler()
	      /Users/julia/Development/jig/serve/server.go:96 +0x1ec
	  foxygo.at/jig/serve/httprule.TestHTTP()
	      /Users/julia/Development/jig/serve/httprule/handler_test.go:29 +0x1dc

Fix by using `NewUnstartedTestServer` and explicitly and synchronously calling
`Start` method after calling `SetHTTPHandler`.

Add the `-race` flag in Makefile to Go test execution.
Copy link
Member

@camh- camh- left a comment

Choose a reason for hiding this comment

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

🍏 Very nice. Thank you

@juliaogris juliaogris merged commit 3abeb72 into master Feb 6, 2025
2 checks passed
@juliaogris juliaogris deleted the fix-race branch February 6, 2025 01:37
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.

3 participants