Files
magpie/internal/filelock/filelock.go
T
yetone 3d55ff473f filelock: a lock makes its folder only when the folder is missing
#1665: in fy1q2's Windows trace, the lock folder took ~193 each of
Create, QueryInfo and Cleanup in a start's first 25s. Each one is work
for an antivirus filter driver. filelock.Lock ran os.MkdirAll on the
lock file's folder before every take, even though the folder is
nearly always there. Now Lock opens the file first and makes the folder
only when OpenFile fails with ErrNotExist, then opens it again.

TestLockMakesFolderOnlyWhenMissing counts mkdirAll calls. It expects
one for a missing folder and none for 20 takes in a folder that is
there. With MkdirAll put back before OpenFile by hand, it goes red:
  --- FAIL: TestLockMakesFolderOnlyWhenMissing (0.25s)
      filelock_test.go:96: 20 takes in a folder that is there made it 20 more times

What was run (worktree at 9fd80b212 plus this change):
  gofmt -l internal/: nothing
  go vet ./internal/filelock (and the other touched packages): exit 0
  GOOS=linux go vet -tags nogui ./...: exit 0
  GOOS=windows go vet -tags nogui ./...: exit 0
  go test -timeout 45m -count=1 ./internal/filelock: ok 7.207s
Not tried on the Windows box; the count is measured on macOS.
2026-10-20 20:23:54 +08:00

95 lines
2.9 KiB
Go

// Package filelock keeps a change to a file one at a time across the
// magpies on a computer: the gateway that runs all day, and each magpie
// run from a terminal or a script meanwhile.
package filelock
import (
"errors"
"io/fs"
"log"
"os"
"path/filepath"
"sync"
"time"
)
// Wait is how long Mutex waits for another magpie to let go, before it
// goes on without the lock, as it did before there was one: a magpie
// stuck holding it (or two magpies each waiting on a lock the other
// holds) never hangs every other.
const Wait = 10 * time.Second
// mkdirAll makes a lock file's folder; a test counts the calls.
var mkdirAll = os.MkdirAll
// Lock takes the lock on the file at path, made if it isn't there, waiting
// up to wait for another process to let it go. The system holds it on the
// file and lets it go when the process holding it ends, however it ends,
// so none is ever left behind; the file stays, since taken away one
// process could hold the lock on it while another took one on a new file.
// held is false when it went on without the lock: on a file system
// without locks, or after wait.
func Lock(path string, wait time.Duration) (unlock func(), held bool) {
// the folder is made only when the file can't be opened for want of
// it: made first, each lock asked the system for the folder too, on
// every take (#1665: ~190 times in a Windows start's first 25s, each
// an antivirus filter driver's work)
f, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR, 0o600)
if errors.Is(err, fs.ErrNotExist) {
mkdirAll(filepath.Dir(path), 0o700)
f, err = os.OpenFile(path, os.O_CREATE|os.O_RDWR, 0o600)
}
if err != nil {
return func() {}, false // no lock to be had here: no file to change either
}
start := time.Now()
for {
got, err := TryLock(f)
if err != nil { // a file system without locks, as an NFS home with no lockd
f.Close()
return func() {}, false
}
if got {
return func() { Unlock(f); f.Close() }, true
}
if time.Since(start) > wait {
f.Close()
log.Printf("%s: another magpie held it for %v; going on without it", path, wait)
return func() {}, false
}
time.Sleep(5 * time.Millisecond)
}
}
// Mutex is a sync.Mutex that also holds the lock on the file Path names
// while it is held, so a read, change and write of a file under it is
// one at a time across magpies too, not only within this one. Every
// read of the file a change is made from is to be made after Lock.
type Mutex struct {
mu sync.Mutex
// Path is the lock file: the file guarded, with ".lock".
Path func() string
// Locked, when set, is called once both locks are held: to have the
// file guarded read afresh, not from what was parsed of it before
// another magpie wrote it.
Locked func()
unlock func()
}
func (m *Mutex) Lock() {
m.mu.Lock()
m.unlock, _ = Lock(m.Path(), Wait)
if m.Locked != nil {
m.Locked()
}
}
func (m *Mutex) Unlock() {
unlock := m.unlock
m.unlock = nil
if unlock != nil {
unlock()
}
m.mu.Unlock()
}