-
Notifications
You must be signed in to change notification settings - Fork 8k
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
fix missing initial sync.RWMutex #2305
Conversation
Signed-off-by: Bo-Yi Wu <[email protected]>
Codecov Report
@@ Coverage Diff @@
## master #2305 +/- ##
==========================================
- Coverage 98.47% 98.39% -0.09%
==========================================
Files 41 41
Lines 2300 2304 +4
==========================================
+ Hits 2265 2267 +2
- Misses 20 21 +1
- Partials 15 16 +1
Continue to review full report at Codecov.
|
Signed-off-by: Bo-Yi Wu <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
If two go routine concurrently access a context which has no mutex, maybe lead to problem.
If two go routine both check c.KeysMutex == nil,and get a true result, one of them create a new sync.RWMutext and run to c.KeysMutex.RLock(), and another create a new sync.RWMutext, then |
@@ -228,6 +228,10 @@ func (c *Context) Error(err error) *Error { | |||
// Set is used to store a new key/value pair exclusively for this context. | |||
// It also lazy initializes c.Keys if it was not used previously. | |||
func (c *Context) Set(key string, value interface{}) { | |||
if c.KeysMutex == nil { | |||
c.KeysMutex = &sync.RWMutex{} | |||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@appleboy @thinkerou Isn't the mutex creation still a race condition? For two threads that are both calling "Set" at the same time, the mutex could get created twice -- once for each thread. What about using "sync.once" here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The PR trigger the performance issue in #2350 |
fix #2301