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 DeadLock in Kernel List Cache Feature #2100

Merged
merged 9 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions internal/fs/fs.go
charith87 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -2154,10 +2154,16 @@ func (fs *fileSystem) OpenDir(

// Enables kernel list-cache in case of non-zero kernelListCacheTTL.
if fs.kernelListCacheTTL > 0 {
// Following lock ordering rules: first taking dirInode.RLock() and then fs.lock().
fs.mu.Unlock()
raj-prince marked this conversation as resolved.
Show resolved Hide resolved

// Taking RLock() since ShouldInvalidateKernelListCache only reads the DirInode
// properties, no modification.
in.RLock()
raj-prince marked this conversation as resolved.
Show resolved Hide resolved

// Acquiring fs lock to use common defer function.
fs.mu.Lock()

// Invalidates the kernel list-cache once the last cached response is out of
// kernelListCacheTTL.
op.KeepCache = !in.ShouldInvalidateKernelListCache(fs.kernelListCacheTTL)
Expand Down
40 changes: 40 additions & 0 deletions internal/fs/kernel_list_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ package fs_test
import (
"os"
"path"
"sync"
"testing"
"time"

Expand Down Expand Up @@ -120,6 +121,45 @@ func TestKernelListCacheTestWithPositiveTtlSuite(t *testing.T) {
suite.Run(t, new(KernelListCacheTestWithPositiveTtl))
}

// Test_Parallel_OpenDirAndLookUpInode helps in detecting the deadlock when
// OpenDir() and LookUpInode() request for same directory comes in parallel.
func (t *KernelListCacheTestWithPositiveTtl) Test_Parallel_OpenDirAndLookUpInode() {
var wg sync.WaitGroup
wg.Add(2)
// Fail if the operation takes more than timeout.
timeout := 5 * time.Second

go func() {
defer wg.Done()
for i := 0; i < 100; i++ {
f, err := os.Open(path.Join(mntDir, "explicitDir"))
assert.Nil(t.T(), err)

err = f.Close()
assert.Nil(t.T(), err)
}
}()
go func() {
defer wg.Done()
for i := 0; i < 100; i++ {
_, err := os.Stat(path.Join(mntDir, "explicitDir"))
assert.Nil(t.T(), err)
}
}()

done := make(chan bool, 1)
go func() {
wg.Wait()
done <- true
}()
select {
case <-done:
// Operation completed successfully before timeout.
case <-time.After(timeout):
assert.FailNow(t.T(), "Possible deadlock")
}
}

// (a) First ReadDir() will be served from GCSFuse filesystem.
// (b) Second ReadDir() within ttl will be served from kernel page cache.
func (t *KernelListCacheTestWithPositiveTtl) TestKernelListCache_CacheHit() {
Expand Down
Loading