Skip to content

Commit

Permalink
Add defensive copy of objects in InMemBucket (#42)
Browse files Browse the repository at this point in the history
Add locking and return a copy of the internally stored objects in InMemBucket.
This fixes a race condition that can be triggered if the result of `.Objects()`
is read while another goroutine modifies the objects (such as uploading a new
file).

Noticed in grafana/mimir#4021

Signed-off-by: Nick Pillitteri <[email protected]>
Co-authored-by: Matej Gera <[email protected]>
  • Loading branch information
56quarters and matej-g authored Jan 31, 2023
1 parent 3cac994 commit ae1c52a
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions inmem.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,18 @@ func NewInMemBucket() *InMemBucket {
}
}

// Objects returns internally stored objects.
// Objects returns a copy of the internally stored objects.
// NOTE: For assert purposes.
func (b *InMemBucket) Objects() map[string][]byte {
return b.objects
b.mtx.RLock()
defer b.mtx.RUnlock()

objs := make(map[string][]byte)
for k, v := range b.objects {
objs[k] = v
}

return objs
}

// Iter calls f for each entry in the given directory. The argument to f is the full
Expand Down

0 comments on commit ae1c52a

Please sign in to comment.