From ce77cfaf92a5437c36128be3ecbbfa0f3fa6ae10 Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Mon, 2 Nov 2026 20:09:05 +0100 Subject: [PATCH] Lock state.yml while reading or updating it Two lazygit instances can update state.yml at the same moment. Both read the same contents, and the second write discards the change of the first. Also, os.WriteFile empties the file before it writes the new contents. An instance that reads the file at that moment gets an empty file, which reads as the default state, or a partial one, which doesn't parse. This can already happen when several instances start at once, for example when a tmux session is restored, because each of them updates state.yml at startup. Take an exclusive lock on a separate lock file while updating, and a shared one while reading. Each lock call opens the lock file anew, so the lock also keeps goroutines of the same instance apart; the test relies on this. Co-Authored-By: Claude Opus 5.5 --- pkg/config/app_state.go | 64 +++++++++++++++++++++++++++++++++ pkg/config/app_state_test.go | 53 +++++++++++++++++++++++++++ pkg/config/file_lock.go | 33 +++++++++++++++++ pkg/config/file_lock_windows.go | 27 ++++++++++++++ 4 files changed, 177 insertions(+) create mode 100644 pkg/config/file_lock.go create mode 100644 pkg/config/file_lock_windows.go diff --git a/pkg/config/app_state.go b/pkg/config/app_state.go index 7ec4966d8..a8686095f 100644 --- a/pkg/config/app_state.go +++ b/pkg/config/app_state.go @@ -27,6 +27,12 @@ func appStateFilePath() (string, error) { // it returns the defaults together with the error. Changing the returned struct // has no effect; use UpdateAppState for that. func (c *AppConfig) ReadAppState() (*AppState, error) { + unlock, err := c.lockAppState(sharedLock) + if err != nil { + return getDefaultAppState(), err + } + defer unlock() + content, err := c.readAppStateFile() if err != nil { return getDefaultAppState(), err @@ -41,6 +47,12 @@ func (c *AppConfig) ReadAppState() (*AppState, error) { // earlier, because that would undo changes that other instances made in the // meantime. func (c *AppConfig) UpdateAppState(update func(state *AppState)) error { + unlock, err := c.lockAppState(exclusiveLock) + if err != nil { + return err + } + defer unlock() + content, err := c.readAppStateFile() if err != nil { return err @@ -59,6 +71,58 @@ func (c *AppConfig) UpdateAppState(update func(state *AppState)) error { return c.writeAppStateFile(newContent) } +type lockKind int + +const ( + sharedLock lockKind = iota + exclusiveLock +) + +// lockAppState locks state.yml.lock, a file next to state.yml, and returns a +// function that releases the lock. Updates take an exclusive lock and reads a +// shared one, so a read never sees a half-written file, and two updates never +// start from the same contents. +// +// We don't lock state.yml itself, because on Windows a lock keeps other +// processes from reading the locked part of a file. We never delete the lock +// file. Otherwise one process could hold a lock on the deleted file while +// another one locks a new file of the same name. +func (c *AppConfig) lockAppState(kind lockKind) (func(), error) { + if c.appStatePath == "" { + return func() {}, nil + } + + path := c.appStatePath + ".lock" + file, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE, 0o644) + if err != nil && os.IsPermission(err) { + // We can't create the lock file in a read-only folder. Lock it if it + // exists, and do without it otherwise; apparently when people have + // read-only permissions they prefer us to fail silently. + file, err = os.Open(path) + if err != nil { + return func() {}, nil + } + } + if err != nil { + return nil, err + } + + if err := lockFile(file, kind); err != nil { + file.Close() + if isLockingUnsupported(err) { + // Some network file systems don't support locks. Doing without is + // better than never reading or saving the state. + return func() {}, nil + } + return nil, err + } + + return func() { + _ = unlockFile(file) + file.Close() + }, nil +} + func (c *AppConfig) readAppStateFile() ([]byte, error) { if c.appStatePath == "" { return nil, nil diff --git a/pkg/config/app_state_test.go b/pkg/config/app_state_test.go index 78d4af4cc..50d0e6d5e 100644 --- a/pkg/config/app_state_test.go +++ b/pkg/config/app_state_test.go @@ -1,8 +1,11 @@ package config import ( + "fmt" "os" "path/filepath" + "runtime" + "sync" "testing" "time" @@ -99,3 +102,53 @@ func TestAppStateUpdateSkipsWritingUnchangedState(t *testing.T) { assert.NoError(t, err) assert.True(t, info.ModTime().Equal(past)) } + +func TestAppStateConcurrentUpdatesOfSeveralInstances(t *testing.T) { + useTempConfigDir(t) + const instanceCount = 8 + const updatesPerInstance = 10 + + instances := make([]*AppConfig, instanceCount) + for i := range instances { + instance, err := newAppConfig(t) + assert.NoError(t, err) + instances[i] = instance + } + + var waitGroup sync.WaitGroup + for i, instance := range instances { + waitGroup.Go(func() { + for j := range updatesPerInstance { + err := instance.UpdateAppState(addToHistory(fmt.Sprintf("command %d-%d", i, j))) + assert.NoError(t, err) + _, err = instance.ReadAppState() + assert.NoError(t, err) + } + }) + } + waitGroup.Wait() + + appState, err := instances[0].ReadAppState() + assert.NoError(t, err) + assert.Len(t, appState.ShellCommandsHistory, instanceCount*updatesPerInstance) +} + +func TestAppStateInReadOnlyFolder(t *testing.T) { + if runtime.GOOS == "windows" || os.Geteuid() == 0 { + t.Skip("needs file permissions that apply to the current user") + } + + configDir := useTempConfigDir(t) + appConfig, err := newAppConfig(t) + assert.NoError(t, err) + statePath := filepath.Join(configDir, stateFileName) + assert.NoError(t, os.WriteFile(statePath, []byte("customcommandshistory:\n - ls\n"), 0o444)) + assert.NoError(t, os.Chmod(configDir, 0o555)) + t.Cleanup(func() { _ = os.Chmod(configDir, 0o755) }) + + assert.NoError(t, appConfig.UpdateAppState(addToHistory("pwd"))) + + appState, err := appConfig.ReadAppState() + assert.NoError(t, err) + assert.Equal(t, []string{"ls"}, appState.ShellCommandsHistory) +} diff --git a/pkg/config/file_lock.go b/pkg/config/file_lock.go new file mode 100644 index 000000000..d7c988baf --- /dev/null +++ b/pkg/config/file_lock.go @@ -0,0 +1,33 @@ +//go:build !windows + +package config + +import ( + "errors" + "os" + + "golang.org/x/sys/unix" +) + +// lockFile blocks until it has locked file. The lock is released by unlockFile, +// or when the file is closed or the process exits. +func lockFile(file *os.File, kind lockKind) error { + how := unix.LOCK_SH + if kind == exclusiveLock { + how = unix.LOCK_EX + } + for { + err := unix.Flock(int(file.Fd()), how) + if !errors.Is(err, unix.EINTR) { + return err + } + } +} + +func unlockFile(file *os.File) error { + return unix.Flock(int(file.Fd()), unix.LOCK_UN) +} + +func isLockingUnsupported(err error) bool { + return errors.Is(err, unix.ENOTSUP) || errors.Is(err, unix.EOPNOTSUPP) || errors.Is(err, unix.ENOLCK) +} diff --git a/pkg/config/file_lock_windows.go b/pkg/config/file_lock_windows.go new file mode 100644 index 000000000..2bec4fe9d --- /dev/null +++ b/pkg/config/file_lock_windows.go @@ -0,0 +1,27 @@ +package config + +import ( + "errors" + "math" + "os" + + "golang.org/x/sys/windows" +) + +// lockFile blocks until it has locked file. The lock is released by unlockFile, +// or when the file is closed or the process exits. +func lockFile(file *os.File, kind lockKind) error { + var flags uint32 + if kind == exclusiveLock { + flags = windows.LOCKFILE_EXCLUSIVE_LOCK + } + return windows.LockFileEx(windows.Handle(file.Fd()), flags, 0, math.MaxUint32, math.MaxUint32, new(windows.Overlapped)) +} + +func unlockFile(file *os.File) error { + return windows.UnlockFileEx(windows.Handle(file.Fd()), 0, math.MaxUint32, math.MaxUint32, new(windows.Overlapped)) +} + +func isLockingUnsupported(err error) bool { + return errors.Is(err, windows.ERROR_NOT_SUPPORTED) +}