Skip to content
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

Merged
merged 3 commits into from
Mar 27, 2020
Merged

fix missing initial sync.RWMutex #2305

merged 3 commits into from
Mar 27, 2020

Conversation

appleboy
Copy link
Member

fix #2301

@codecov
Copy link

codecov bot commented Mar 27, 2020

Codecov Report

Merging #2305 into master will decrease coverage by 0.08%.
The diff coverage is 50.00%.

Impacted file tree graph

@@            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     
Impacted Files Coverage Δ
context.go 97.65% <50.00%> (-0.41%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 57f99ca...1bb0456. Read the comment docs.

Signed-off-by: Bo-Yi Wu <[email protected]>
@appleboy appleboy added the bug label Mar 27, 2020
Copy link
Member

@thinkerou thinkerou left a comment

Choose a reason for hiding this comment

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

LGTM

@appleboy appleboy merged commit 298ebca into master Mar 27, 2020
@appleboy appleboy deleted the bugs2 branch March 27, 2020 02:57
@bbiao
Copy link
Contributor

bbiao commented Mar 27, 2020

If two go routine concurrently access a context which has no mutex, maybe lead to problem.

        if c.KeysMutex == nil {
		c.KeysMutex = &sync.RWMutex{}
	}
......
	c.KeysMutex.RLock()
	value, exists = c.Keys[key]
	c.KeysMutex.RUnlock()

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
mutex in c.KeysMutex.RUnlock() maybe different from the one used in c.KeysMutex.RLock()

@@ -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{}
}
Copy link

@felix-h felix-h Mar 30, 2020

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?

Copy link
Member Author

Choose a reason for hiding this comment

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

Good catch.

Copy link
Member Author

Choose a reason for hiding this comment

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

@felix-h fix in #2351

@appleboy
Copy link
Member Author

appleboy commented May 3, 2020

The PR trigger the performance issue in #2350

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

got panic with RWMutex on 1.6.0 version
4 participants