filelock

package
v1.2.11 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 11, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package filelock is the one lock on a file across processes: the kernel's lock (flock on unix, LockFileEx on Windows), released by the kernel when its holder dies, with the holder's stamp written inside the file for a refusal to name. Its design and invariants are tla/FileLock.tla. Lock acquisition is exclusive, with waiting or non-blocking forms; release clears the holder note, which names a holder in a refusal but never affects lock decisions.

Its callers, on unix, each the same flock on the same file its earlier binary took, so an old and a new binary exclude each other across an upgrade (each package's *compat*_test.go or TestAnOldBinarysCreateLockKeepsTheNewOneOut): cmd/nova-sandbox (the volume-creation lock, darwin), pkg/swarm (the slot store's lock, behind an in-process turn), internal/tokens (the fold lock) and internal/update (the snapshot lock). Each creates a fresh lock file itself with the mode it always had. On Windows those three packages keep their own locks (an O_EXCL .held file, or byte 0) until a migration in which old and new still exclude each other: this lock is LockFileEx at OffsetHigh 0x80000000. pkg/bus keeps its own lock.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrHeld indicates the lock is currently held by an exclusive holder.
	ErrHeld = errors.New("lock held")
	// ErrBusy indicates the lock could not be acquired because only shared askers were present.
	// It is distinct from ErrHeld and does not wrap it.
	ErrBusy = errors.New("lock busy")
	// ErrTimeout indicates waiting for the lock timed out.
	ErrTimeout = errors.New("lock timeout")
	// ErrNotSupported indicates that filelock is not supported on the current platform.
	ErrNotSupported = errors.New("filelock: locking is not supported on this platform")
)

Functions

This section is empty.

Types

type FileLock

type FileLock struct {
	// contains filtered or unexported fields
}

FileLock represents an acquired, open file lock.

func Lock

func Lock(path string, label string, timeout time.Duration) (*FileLock, error)

Lock acquires the exclusive file lock on path, waiting up to timeout with jittered backoff.

The lockfile path must be dedicated to filelock: taking an exclusive lock on an existing regular file truncates it when writing the holder stamp. The file is created if absent and is never removed.

func TryLock

func TryLock(path string, label string) (*FileLock, error)

TryLock attempts to acquire the exclusive file lock on path without waiting.

The lockfile path must be dedicated to filelock: taking an exclusive lock on an existing regular file truncates it when writing the holder stamp. The file is created if absent and is never removed.

func (*FileLock) Unlock

func (l *FileLock) Unlock() error

Unlock releases the file lock by truncating the file to zero bytes, syncing, releasing the OS lock, and closing the descriptor. The file is NEVER deleted. Calling Unlock multiple times is safe and returns nil on subsequent calls.

type HeldError

type HeldError struct {
	Path     string
	Holder   Stamp
	Wait     time.Duration
	TimedOut bool
}

HeldError describes a refusal because another process holds the lock, or a timeout occurred while waiting for the lock.

func AsHeldError

func AsHeldError(err error) (*HeldError, bool)

AsHeldError reports whether err is a *HeldError.

func (*HeldError) Error

func (e *HeldError) Error() string

func (*HeldError) Unwrap

func (e *HeldError) Unwrap() []error

Unwrap returns []error so that errors.Is(err, ErrHeld) and errors.Is(err, ErrTimeout) (if TimedOut) both match.

type Stamp

type Stamp struct {
	PID     int
	Host    string
	Started time.Time
	Label   string
}

Stamp records diagnostic details of who holds the lock: PID, host, start time, and label.

func ParseStamp

func ParseStamp(s string) (Stamp, error)

ParseStamp parses a Stamp from raw text.

func ReadStamp

func ReadStamp(path string) (Stamp, error)

ReadStamp reads and parses the Stamp from path without acquiring the lock. It opens with O_RDONLY and does not block on FIFOs.

func (Stamp) Format

func (s Stamp) Format() string

Format returns the canonical multi-line representation of the stamp.

func (Stamp) IsZero

func (s Stamp) IsZero() bool

IsZero reports whether the stamp is empty.

func (Stamp) String

func (s Stamp) String() string

String returns a single-line summary of the stamp.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL