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 racy code in Get #14

Merged
merged 1 commit into from Jun 4, 2014

Conversation

Projects
None yet
3 participants
@LK4D4
Contributor

LK4D4 commented Jun 4, 2014

I was looking for races in docker and found this :)

@kisielk

View changes

Show outdated Hide outdated context.go
@@ -31,8 +31,9 @@ func Set(r *http.Request, key, val interface{}) {
func Get(r *http.Request, key interface{}) interface{} {
mutex.RLock()
if data[r] != nil {

This comment has been minimized.

@kisielk

kisielk Jun 4, 2014

Member

Good catch 👍 While you're here, can you change this to:

if ctx := data[r]; ctx != nil {
    value := ctx[key]
...

to avoid having to look up the key twice.

@kisielk

kisielk Jun 4, 2014

Member

Good catch 👍 While you're here, can you change this to:

if ctx := data[r]; ctx != nil {
    value := ctx[key]
...

to avoid having to look up the key twice.

This comment has been minimized.

@LK4D4

LK4D4 Jun 4, 2014

Contributor

Done!

@LK4D4

LK4D4 Jun 4, 2014

Contributor

Done!

kisielk added a commit that referenced this pull request Jun 4, 2014

@kisielk kisielk merged commit 14f550f into gorilla:master Jun 4, 2014

1 check passed

continuous-integration/travis-ci The Travis CI build passed
Details
@kisielk

This comment has been minimized.

Show comment
Hide comment
@kisielk

kisielk Jun 4, 2014

Member

Thanks 👍

Member

kisielk commented Jun 4, 2014

Thanks 👍

@LK4D4 LK4D4 deleted the LK4D4:fix_race_in_get branch Jun 4, 2014

@mperham

This comment has been minimized.

Show comment
Hide comment
@mperham

mperham Jul 29, 2014

Out of curiousity, why not use defer so you know the mutex will be unlocked only once the method is finished? This bug would not happen with defer.

Out of curiousity, why not use defer so you know the mutex will be unlocked only once the method is finished? This bug would not happen with defer.

This comment has been minimized.

Show comment
Hide comment
@LK4D4

LK4D4 Jul 29, 2014

Contributor

@mperham defer has huge overhead for such small functions.
http://lk4d4.darth.io/posts/defer/

Contributor

LK4D4 replied Jul 29, 2014

@mperham defer has huge overhead for such small functions.
http://lk4d4.darth.io/posts/defer/

This comment has been minimized.

Show comment
Hide comment
@mperham

mperham Jul 29, 2014

@LK4D4 Wow, TIL. Thanks!

@LK4D4 Wow, TIL. Thanks!

This comment has been minimized.

Show comment
Hide comment
@kisielk

kisielk Jul 29, 2014

Member

Yeah actually it was introduced as a result of a PR to not use defer :/

Member

kisielk replied Jul 29, 2014

Yeah actually it was introduced as a result of a PR to not use defer :/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment