Skip to content
This repository was archived by the owner on Sep 11, 2020. It is now read-only.

fix race condition on ObjectLRU #544

Merged
merged 1 commit into from
Aug 12, 2017

Conversation

erizocosmico
Copy link
Contributor

With concurrent usage, it could lead to a concurrent map access panic on runtime.

Sadly, the test only adds value with -race enabled. If you have any idea how to get that panic on the test, it would improve its robustness.

@codecov
Copy link

codecov bot commented Aug 11, 2017

Codecov Report

Merging #544 into master will decrease coverage by 0.58%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #544      +/-   ##
==========================================
- Coverage   78.07%   77.48%   -0.59%     
==========================================
  Files         129      129              
  Lines        9792     9798       +6     
==========================================
- Hits         7645     7592      -53     
- Misses       1316     1388      +72     
+ Partials      831      818      -13
Impacted Files Coverage Δ
plumbing/cache/object_lru.go 95.34% <100%> (+0.75%) ⬆️
plumbing/transport/ssh/common.go 20.54% <0%> (-45.21%) ⬇️
plumbing/transport/ssh/auth_method.go 31.57% <0%> (-22.81%) ⬇️

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 7738417...3fd3988. Read the comment docs.

for i := 0; i < 1000; i++ {
wg.Add(1)
go func(i int) {
s.c.Put(newObject(fmt.Sprint(i), FileSize(i)))
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we also want to exercise the other operations that require synchronization? Like Get and Clear?

Signed-off-by: Miguel Molina <[email protected]>
@erizocosmico erizocosmico force-pushed the fix/race-condition-object-lru branch from ef35f91 to 3fd3988 Compare August 11, 2017 12:30
@erizocosmico
Copy link
Contributor Author

@orirawlings amended with usage of Clear and Get as well

@@ -67,6 +72,9 @@ func (c *ObjectLRU) Put(obj plumbing.EncodedObject) {
// Get returns an object by its hash. It marks the object as used. If the object
// is not in the cache, (nil, false) will be returned.
func (c *ObjectLRU) Get(k plumbing.Hash) (plumbing.EncodedObject, bool) {
c.mut.Lock()
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe we can use a RWLock? as suggested at: https://blog.golang.org/go-maps-in-action

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Get mutates the state in c.ll.MoveToFront(ee), that's why I used a Mutex instead of a RWMutex

@mcuadros mcuadros merged commit b51e316 into src-d:master Aug 12, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants