-
Notifications
You must be signed in to change notification settings - Fork 7.9k
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 detects DATA RACE
with Go1.21, Go1.22
#3931
Comments
DATA RACE
with Go1.21, Go1.22DATA RACE
with Go1.21, Go1.22
actually, this |
Just a question, in what scenarios is it necessary to pass the Gin context into Gorm's? After all, Gorm is primarily used for controlling database-related operations, such as querying with timeouts. The context in Gin seems fundamentally different from the context in Gorm |
yes, but sometimes developer will directly passes the gin.Context to db interface, it will becomes a issue. (I always pass like this) |
Wow, Good findings @georgehao If I understand correctly, the race is happen between So, there are 2 goroutines run parallel. The first one is from The second goroutine is from a new request comes in and got a same reference of *gin.Context (previous request already marked as finished by gin and gin put the *gin.Context back to the pool) trying to write the new *http.Request to the *gin.Context |
Almost correct, only tweak a little. owing to |
If I read your data race stack trace, the race comes from |
Oh my bad I didn't notice that the stacktrace shows the race comes from |
Just pass
Also consider not passing request's context to a DBMS transaction when performing INSERT/UPDATE/DELETE. Otherwise your transaction may rollback if request is canceled by client (by accidental page refresh, for example, if client is a web application). Create separate context with timeout instead. |
Description
How to reproduce
go run -race main.go
you will see the
DATA RACE
The reason
gin
server starting, will callServeHTTP
. This a goroutine, allocates from go lib httphttps://github.com/gin-gonic/gin/blob/master/gin.go#L583C1-L592C2
can see that
gin.Context
allocate fromsync.Pool
gorm will call
initContextClose
finally.https://github.com/golang/go/blob/go1.20.14/src/database/sql/sql.go#L2916-L2941
can see here
initContextClose
will start a goroutine to dealctx
, this ctx isgin.Context
.think one case:
if
go rs.awaitDone(ctx, txctx)
run over afterServeHTTP
goroutine. Thegin.Context
allocated fromsync.Pool
will be recovered and allocates for next request.This really a data race
DATA RACE
happens when go version large thango1.20
?I think go upgrade the race detector, the new race detector detects this.
Environment
The text was updated successfully, but these errors were encountered: