Skip to content

Implement proper file locking for Windows #58

@dapi

Description

@dapi

Problem

Currently, Windows builds have no file locking:

// lock_windows.go
func (l *FileLock) Lock() error {
    once.Do(func() {
        fmt.Fprintln(os.Stderr, "warning: file locking not available on Windows...")
    })
    return nil
}

This means concurrent port-selector invocations on Windows can corrupt the allocations file.

Impact

  • Data corruption risk with parallel agents on Windows
  • Warning message on every run (via sync.Once per process)
  • Windows is a second-class platform

Proposed Solution

Implement Windows file locking using LockFileEx:

// lock_windows.go
import "golang.org/x/sys/windows"

func (l *FileLock) Lock() error {
    var overlapped windows.Overlapped
    err := windows.LockFileEx(
        windows.Handle(l.file.Fd()),
        windows.LOCKFILE_EXCLUSIVE_LOCK,
        0,
        1, 0,
        &overlapped,
    )
    return err
}

func (l *FileLock) Unlock() error {
    var overlapped windows.Overlapped
    return windows.UnlockFileEx(
        windows.Handle(l.file.Fd()),
        0,
        1, 0,
        &overlapped,
    )
}

Trade-offs

Pros:

  • Full Windows support
  • No more warning messages
  • Safe concurrent access

Cons:

  • New dependency: golang.org/x/sys/windows
  • Need Windows environment for testing
  • Increases complexity

Alternative

If Windows is not a priority platform, document this limitation clearly in README and keep current behavior.

Acceptance Criteria

  • Decide: implement or document limitation
  • If implement: add golang.org/x/sys/windows dependency
  • If implement: write Windows-specific locking code
  • If implement: test on Windows (CI or manual)
  • Update README with Windows status

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions