File Lock Guard

Take an exclusive non-blocking flock around a critical section, reject a second holder, and reacquire after release.

GoSecurity Boundaries

Code

gosnippet
65 lines
package main

import (
	"fmt"
	"os"
	"path/filepath"
	"syscall"
)

type FileLock struct {
	file *os.File
	path string
}

func AcquireFileLock(path string) (*FileLock, error) {
	file, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR, 0o600)
	if err != nil {
		return nil, fmt.Errorf("open lock file: %w", err)
	}
	if err := syscall.Flock(int(file.Fd()), syscall.LOCK_EX|syscall.LOCK_NB); err != nil {
		file.Close()
		return nil, fmt.Errorf("lock held elsewhere: %w", err)
	}
	return &FileLock{file: file, path: path}, nil
}

func (l *FileLock) Release() error {
	if err := syscall.Flock(int(l.file.Fd()), syscall.LOCK_UN); err != nil {
		l.file.Close()
		return fmt.Errorf("unlock: %w", err)
	}
	return l.file.Close()
}

func withLock(path string, work func() error) error {
	lock, err := AcquireFileLock(path)
	if err != nil {
		return err
	}
	defer lock.Release()
	return work()
}

func main() {
	path := filepath.Join(os.TempDir(), "devexamples-file-lock.lock")
	lock, err := AcquireFileLock(path)
	if err != nil {
		fmt.Println("first acquire failed:", err)
		return
	}
	fmt.Println("locked:", filepath.Base(lock.path))

	if _, err := AcquireFileLock(path); err != nil {
		fmt.Println("second acquire rejected:", err != nil)
	}

	if err := lock.Release(); err != nil {
		fmt.Println("release failed:", err)
	}
	reopened, err := AcquireFileLock(path)
	fmt.Println("reacquired:", err == nil)
	if err == nil {
		_ = reopened.Release()
	}
}

What the code does

The callable surface is the functions AcquireFileLock, Release, withLock and main — everything else is wiring.

Nothing falls through silently — 7 checkpoints test err != nil on the way through. defer schedules the cleanup, and the scheduled call fires no matter how the function exits.

What to watch for

  • The runtime is all that is required — nothing to fetch or install first.
  • The function reports failure by returning error — callers branch on it before touching other results.

Applying it

The main signature reads func AcquireFileLock(path string) (*FileLock, error)

The demo output is labeled (first acquire failed:, locked:, second acquire rejected:), so the output doubles as a step-by-step trace of what the file did.

main sequences AcquireFileLock and Release; executing the file runs that flow once.

Statements that decide the outcome

return nil, fmt.Errorf("lock held elsewhere: %w", err)
return nil, fmt.Errorf("open lock file: %w", err)
return fmt.Errorf("unlock: %w", err)

Interactive Tools for This Topic

FAQ

Is there exception handling?
Through `error` values, not panics — the snippet checks `err != nil` at 7 sites.
What does File Lock Guard print when run?
Invoke the file as-is — the `first acquire failed:`, `locked:`, `second acquire rejected:` prefixes identify which step printed each line.