-
Notifications
You must be signed in to change notification settings - Fork 382
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
data race in various middlewares #217
Comments
Hi @gsparx ! Thanks for your bug report! And sorry for my extremely slow response. I didn't have much time for this project in a past few months. I'm not very familiar with the But I noticed that your test is instantiating the API handler twice. One in each go routine. I think this is where the problem is. A single API object with a single stack of middlewares, but with MakeHandler() called multiple times in a concurrent way. The middlewares are "initialized" by calling MiddlewareFunc for each of them. They are not supposed to run for each request in a concurrent way because the state of each middleware objects would be shared between the go routines. I think that a more realistic test is to instantiate the API stack of middlewares once, and to run concurrent requests. In that case the test passes.
Now, I dug a bit further and found something else. The following code instantiates a full new API object in each go routine (which in practice doesn't make much sense), and fails
This is because the stack of middlewares is actually instantiated as a global variable Replacing the global variable by the actual instantiations of the middlewares works:
I guess the fix would be to replace these set of global variables: That is obviously a breaking API change. Any thought ? |
I'm able to consistently induce the golang race detector to detect data race in several middleware
In the
DefaultDevStack
(likely not expected to be used in production code)and in the
DefaultCommonStack
(seems like this should be viable for production)Just based on the naming, it seems like
RecoverMiddleware
is non-optional. If it is optional, is there a middleware stack that you would suggest running in production that is not susceptible to data races?Code is attached (it's basically just the HelloWorld example plus a test). Runnable with
go test -race
Thanks!
go_json_rest_race_test.tar.gz
The text was updated successfully, but these errors were encountered: