beads/internal/lockfile/lock_shared_windows.go
beads/crew/emma 1269368593 fix: advisory flock to prevent zombie bd processes from dolt lock contention
Replace ProcessSemaphore (counting semaphore with N slots) with advisory flock
on .beads/dolt-access.lock. Readers acquire shared locks (concurrent), writers
acquire exclusive locks (serialized). This prevents multiple bd processes from
competing for dolt internal LOCK file, which caused zombie hangs.

Key changes:
- Add FlockSharedNonBlock/FlockExclusiveNonBlock to lockfile package (unix/windows/wasm)
- Add AccessLock with poll-based acquisition and configurable timeout
- Integrate lock acquire/release in DoltStore.New()/Close() and error paths
- Replace ProcessSemaphore in main.go with OpenTimeout (5s read, 15s write)
- Remove dead ProcessSemaphore code

Co-Authored-By: beads/crew/lydia <steve.yegge@gmail.com>
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

Executed-By: beads/crew/emma
Rig: beads
Role: crew
2026-02-02 22:41:18 -07:00

54 lines
1.2 KiB
Go

//go:build windows
package lockfile
import (
"os"
"syscall"
"golang.org/x/sys/windows"
)
// FlockSharedNonBlock acquires a shared non-blocking lock on the file.
// Multiple processes can hold shared locks concurrently.
// Returns ErrLockBusy if an exclusive lock is already held.
func FlockSharedNonBlock(f *os.File) error {
// Shared + fail immediately (no LOCKFILE_EXCLUSIVE_LOCK)
const flags = windows.LOCKFILE_FAIL_IMMEDIATELY
ol := &windows.Overlapped{}
err := windows.LockFileEx(
windows.Handle(f.Fd()),
flags,
0,
0xFFFFFFFF,
0xFFFFFFFF,
ol,
)
if err == windows.ERROR_LOCK_VIOLATION || err == syscall.EWOULDBLOCK {
return ErrLockBusy
}
return err
}
// FlockExclusiveNonBlock acquires an exclusive non-blocking lock on the file.
// Returns ErrLockBusy if any lock (shared or exclusive) is already held.
func FlockExclusiveNonBlock(f *os.File) error {
const flags = windows.LOCKFILE_EXCLUSIVE_LOCK | windows.LOCKFILE_FAIL_IMMEDIATELY
ol := &windows.Overlapped{}
err := windows.LockFileEx(
windows.Handle(f.Fd()),
flags,
0,
0xFFFFFFFF,
0xFFFFFFFF,
ol,
)
if err == windows.ERROR_LOCK_VIOLATION || err == syscall.EWOULDBLOCK {
return ErrLockBusy
}
return err
}