WayfinderFileLock class final

An advisory exclusive lock on a file that holds across processes and across the isolates of one process.

On macOS, Linux and other POSIX systems it is flock(2) on a descriptor of its own, through dart:ffi. A flock lock belongs to that open file description, so another isolate's descriptor conflicts with it and closing any other descriptor of the file leaves it held. On Windows it is RandomAccessFile.lock, whose LockFileEx lock belongs to its handle and has the same properties; it is also mandatory, so while it is held no other handle reads the file's writeNote.

Dart's own RandomAccessFile.lock is a fcntl record lock on POSIX: it belongs to the whole process, so isolates do not exclude each other, and closing any descriptor of the file releases it. Wayfinder's lock files use this class instead. flock and fcntl locks do not exclude each other.

Its descriptor is close-on-exec, and release unlocks before closing, so a child process another isolate starts never keeps the lock. The lock file stays in place on release. A cleanup that deletes a lock file deletes it while holding the lock, and an acquirer that opened the file just before checks, once it has the lock, that the path still names the file it locked, and opens the path again when it does not; so two holders never lock different files. Always release in a finally; an abandoned lock is held until its process exits.

Properties

file → File
The lock file.
final
hashCode → int
The hash code for this object.
no setterinherited
isReleased → bool
Whether release ran.
no setter
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
release() → Future<void>
Releases the lock and closes its descriptor. Later calls do nothing.
toString() → String
A string representation of this object.
inherited
writeNote(String text) → Future<void>
Replaces the lock file's contents with text, such as the path the lock protects, so a later cleanup can tell what it belonged to. Only the holder writes. Read the note only once isHeld is false: on Windows the lock is a mandatory byte-range lock, so no other handle can read the file while it is held.

Operators

operator ==(Object other) → bool
The equality operator.
inherited

Static Methods

exclusive(File file, {Duration timeout = const Duration(seconds: 120), WayfinderCancellation? cancel}) → Future<WayfinderFileLock>
Takes the lock, polling until it is free. Throws a TimeoutException after timeout, and WayfinderException.cancelled() when cancel fires first.
isHeld(File file) → Future<bool>
Whether any holder, in this process or another, has the lock on file. Tries a shared lock on a descriptor of its own and releases it at once, so it never disturbs a holder, and tryExclusive does not mistake it for one. False when file is missing.
tryExclusive(File file) → Future<WayfinderFileLock?>
Takes the lock without waiting for a holder; null while another holder has it. Creates file and its directory when missing.