Merge PR #10648: Fix locking during concurrent disk cache access

This commit is contained in:
Kovid Goyal 2026-10-11 12:02:56 +05:30
commit faa50dfdcb
4 changed files with 143 additions and 12 deletions

View file

@ -268,6 +268,8 @@ Detailed list of changes
- quick-access-terminal kitten: Fix :option:`kitty +kitten quick_access_terminal --detached-log` not working
- choose-files kitten: Fix concurrent access to its disk cache
- Fix a non-zero :opt:`input_delay` painting once before delayed program output is parsed and again when it is parsed, so it could use more CPU than :opt:`input_delay` ``0``
- Linux/BSD: Detect MIME types for :doc:`open actions <open_actions>` and

View file

@ -44,13 +44,17 @@ func (dc *DiskCache) ResultsDir() string {
}
func (dc *DiskCache) Get(key string, items ...string) (map[string]string, error) {
dc.lock()
if err := dc.lock(); err != nil {
return nil, err
}
defer dc.unlock()
return dc.get(key, items)
}
func (dc *DiskCache) GetPath(path string, items ...string) (string, map[string]string, error) {
dc.lock()
if err := dc.lock(); err != nil {
return "", nil, err
}
defer dc.unlock()
key, err := key_for_path(path)
if err != nil {
@ -61,26 +65,34 @@ func (dc *DiskCache) GetPath(path string, items ...string) (string, map[string]s
}
func (dc *DiskCache) Remove(key string) (err error) {
dc.lock()
if err = dc.lock(); err != nil {
return
}
defer dc.unlock()
return dc.remove(key)
}
func (dc *DiskCache) Clear() (err error) {
dc.lock()
if err = dc.lock(); err != nil {
return
}
defer dc.unlock()
return dc.clear()
}
func (dc *DiskCache) AddPath(path, key string, items map[string][]byte) (ans map[string]string, err error) {
dc.lock()
if err = dc.lock(); err != nil {
return
}
defer dc.unlock()
ans, err = dc.add_path(path, key, items)
return
}
func (dc *DiskCache) Add(key string, items map[string][]byte) (ans map[string]string, err error) {
dc.lock()
if err = dc.lock(); err != nil {
return
}
defer dc.unlock()
return dc.add(key, items)
}

View file

@ -71,7 +71,9 @@ func new_disk_cache(path string, max_size int64) (dc *DiskCache, err error) {
return
}
ans := &DiskCache{Path: path, MaxSize: max_size}
ans.lock()
if err = ans.lock(); err != nil {
return
}
defer ans.unlock()
if err = ans.ensure_entries(); err != nil {
return
@ -112,10 +114,11 @@ func key_for_path(path string) (key string, err error) {
func (dc *DiskCache) lock() (err error) {
dc.lock_mutex.Lock()
defer dc.lock_mutex.Unlock()
if dc.lock_file != nil {
return
}
defer func() {
if err != nil {
dc.unlock()
}
}()
if dc.lock_file, err = os.OpenFile(filepath.Join(dc.Path, "lockfile"), os.O_RDWR|os.O_CREATE, 0o600); err != nil {
return
}
@ -123,7 +126,6 @@ func (dc *DiskCache) lock() (err error) {
}
func (dc *DiskCache) unlock() {
dc.lock_mutex.Lock()
defer dc.lock_mutex.Unlock()
if dc.lock_file != nil {
utils.UnlockFile(dc.lock_file)

View file

@ -6,6 +6,7 @@ import (
"path/filepath"
"runtime/debug"
"strings"
"sync"
"testing"
"github.com/google/go-cmp/cmp"
@ -149,3 +150,117 @@ func TestDiskCache(t *testing.T) {
}
arc(4, 8) // dc.AddPath() causes dc2 to read
}
func TestDiskCacheLock(t *testing.T) {
dc, err := NewDiskCache(t.TempDir(), 64)
if err != nil {
t.Fatal(err)
}
if err = dc.lock(); err != nil {
t.Fatal(err)
}
defer dc.unlock()
if dc.lock_mutex.TryLock() {
dc.lock_mutex.Unlock()
t.Fatal("cache mutex released before the cache operation finished")
}
}
func TestDiskCacheConcurrentGet(t *testing.T) {
dc, err := NewDiskCache(t.TempDir(), 64)
if err != nil {
t.Fatal(err)
}
if _, err = dc.Add("sharedkey", map[string][]byte{"data": []byte("cached")}); err != nil {
t.Fatal(err)
}
var workers sync.WaitGroup
start := make(chan struct{})
for range 6 {
workers.Go(func() {
<-start
for range 8 {
paths, err := dc.Get("sharedkey", "data")
if err != nil {
t.Error(err)
return
}
if paths["data"] == "" {
t.Error("concurrent Get lost cached data")
return
}
}
})
}
close(start)
workers.Wait()
}
func TestDiskCacheLockErrors(t *testing.T) {
source := filepath.Join(t.TempDir(), "source")
if err := os.WriteFile(source, []byte("source"), 0o600); err != nil {
t.Fatal(err)
}
for _, tc := range []struct {
name string
run func(*DiskCache) error
}{
{"Get", func(dc *DiskCache) error {
_, err := dc.Get("key", "data")
return err
}},
{"GetPath", func(dc *DiskCache) error {
_, _, err := dc.GetPath(source, "data")
return err
}},
{"Remove", func(dc *DiskCache) error { return dc.Remove("key") }},
{"Clear", func(dc *DiskCache) error { return dc.Clear() }},
{"AddPath", func(dc *DiskCache) error {
_, err := dc.AddPath(source, "key", map[string][]byte{"data": []byte("updated")})
return err
}},
{"Add", func(dc *DiskCache) error {
_, err := dc.Add("key", map[string][]byte{"data": []byte("updated")})
return err
}},
{"NewDiskCache", func(dc *DiskCache) error {
_, err := NewDiskCache(dc.Path, dc.MaxSize)
return err
}},
} {
t.Run(tc.name, func(t *testing.T) {
dc, err := NewDiskCache(t.TempDir(), 64)
if err != nil {
t.Fatal(err)
}
if _, err = dc.Add("key", map[string][]byte{"data": []byte("cached")}); err != nil {
t.Fatal(err)
}
lock_path := filepath.Join(dc.Path, "lockfile")
if err = os.Remove(lock_path); err != nil {
t.Fatal(err)
}
if err = os.Mkdir(lock_path, 0o700); err != nil {
t.Fatal(err)
}
if err = tc.run(dc); err == nil {
t.Error("cache operation ignored failure to acquire the lock")
}
if !dc.lock_mutex.TryLock() {
t.Fatal("failed lock attempt did not release the mutex")
}
dc.lock_mutex.Unlock()
if err = os.Remove(lock_path); err != nil {
t.Fatal(err)
}
paths, err := dc.Get("key", "data")
if err != nil {
t.Fatal(err)
}
data, err := os.ReadFile(paths["data"])
if err != nil || string(data) != "cached" {
t.Fatalf("cache data changed after a failed lock attempt: %q, %v", data, err)
}
})
}
}