Skip to content

Commit

Permalink
Use LoadAndDelete whenever possible to avoid race conditions when usi…
Browse files Browse the repository at this point in the history
…ng a sync map.
  • Loading branch information
foodprocessor committed Sep 20, 2024
1 parent daf0d7a commit 28fcc00
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
5 changes: 2 additions & 3 deletions component/file_cache/file_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -1211,11 +1211,10 @@ func (fc *FileCache) FlushFile(options internal.FlushFileOptions) error {

// If chmod was done on the file before it was uploaded to container then setting up mode would have been missed
// Such file names are added to this map and here post upload we try to set the mode correctly
_, found := fc.missedChmodList.Load(options.Handle.Path)
// Delete the entry from map so that any further flush do not try to update the mode again
_, found := fc.missedChmodList.LoadAndDelete(options.Handle.Path)
if found {
// If file is found in map it means last chmod was missed on this
// Delete the entry from map so that any further flush do not try to update the mode again
fc.missedChmodList.Delete(options.Handle.Path)

// When chmod on container was missed, local file was updated with correct mode
// Here take the mode from local cache and update the container accordingly
Expand Down
3 changes: 1 addition & 2 deletions component/libfuse/libfuse2_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ func (cf *CgofuseFS) Opendir(path string) (int, uint64) {
// Releasedir opens the handle for the directory at the path.
func (cf *CgofuseFS) Releasedir(path string, fh uint64) int {
// Get the filehandle
handle, exists := handlemap.Load(handlemap.HandleID(fh))
handle, exists := handlemap.LoadAndDelete(handlemap.HandleID(fh))
if !exists {
log.Trace("Libfuse::Releasedir : Failed to release %s, handle: %d", path, fh)
return -fuse.EBADF
Expand All @@ -435,7 +435,6 @@ func (cf *CgofuseFS) Releasedir(path string, fh uint64) int {
log.Trace("Libfuse::Releasedir : %s, handle: %d", handle.Path, handle.ID)

handle.Cleanup()
handlemap.Delete(handle.ID)
return 0
}

Expand Down
9 changes: 9 additions & 0 deletions internal/handlemap/handle_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,15 @@ func Delete(key HandleID) {
defaultHandleMap.Delete(key)
}

// Delete : Remove handle object from map, and return the entry (if any)
func LoadAndDelete(key HandleID) (*Handle, bool) {
val, found := defaultHandleMap.LoadAndDelete(key)
if !found {
return nil, false
}
return val.(*Handle), true
}

func CreateCacheObject(capacity int64, handle *Handle) {
handle.CacheObj = &Cache{
sync.RWMutex{},
Expand Down

0 comments on commit 28fcc00

Please sign in to comment.