-
Notifications
You must be signed in to change notification settings - Fork 8.1k
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
gin.Context data race in most trivial situation #4117
Comments
So the only way to avoid this - not using Pool when ContextWithFallback is enabled. In any other situation Context may leak. |
We've encountered crash in prod env because of this stuff( |
Description
gin.Context
implementscontext.Context
. In my opinion, it should follows context.Context package's thread safety guaranties.However gin doc states, that
gin.Context
is valid only in a request scope. And one should callCopy
to avoid pitfalls.But consider following example.
This should work according to the documentation and general expectations. However, with
ContextWithFallback
we would get following result on context cancellation:I won't dive deeply into reasoning. Default http library leaks context to other gorouting outside of request scope. The main problem is that programmer shouldn't know implementation details of http lib! It should just work!
Consider other example in the code below. Most of the time we can't even call
Copy
in some deep nested code.So, now the only way of safe usage of
gin.Context
is to callCopy
on every conversion to thecontext.Context
. But this makes the whole point of implementingcontext.Context
useless.But even this is not always possible in middleware/generated code world. So we are abandoning usage of gin in out tech stack.
But I hope this will help to improve gin, as it is truly good http framework.
My proposals:
context.Context
and provide explicitGetContext/MakeContext
methods, that will return threadsafe context. As this is the breaking change, consider it for gin/v2How to reproduce
go test -race -bench=.
Expectations
Just works
Actual result
DATA RACE as above
Environment
The text was updated successfully, but these errors were encountered: