Definition
Exclusive ownership of a resource means that at most one party, the owner, may act on the resource at any moment. A lease is a grant of ownership that expires: an authority, such as a lock service, gives one party the right to act on a resource until a stated time, and that party must renew the grant before then or lose it. Afterwards the authority may grant the resource to another party. Expiry ends the right to act; it does not stop a former holder that has not noticed.
The idea
Two controllers, A and B, share one test device, and only one may send it commands at a time. At time 0 s, A takes a 10-second lease from a lock service, a program that hands out leases and records who holds each one. At time 1 s, A stalls for 15 seconds, for example because its computer suspends it or a disk read blocks. A cannot renew while stalled, so at time 10 s the lease expires, and the service grants a new lease to B, which starts sending commands. At time 16 s, A wakes up. Its own memory still says that it owns the device, and it sends a command. Now two owners act on one device.
A lease solves one problem and leaves another. It solves the problem of an owner that dies: a lock without expiry, held by a party that crashed, would never be released. Cary Gray and David Cheriton introduced leases in 1989, to keep cached copies of files consistent [407]. A lease leaves the problem of an owner that is only paused. Seen from outside, a stopped, a paused and a slow but working holder look the same: nothing arrives from any of them.
The repair is a check where the action happens. Each grant carries a number, called a fencing token, that grows with every grant. The resource remembers the highest token it has accepted and refuses any request with a lower one. A door lock that remembers the highest key-card number it has accepted, and refuses every lower number, works the same way. The picture is exact in one more respect: the lock learns of a new card only when that card is first used, so until then an old card still opens the door.
How it works
A lease with fencing works in five steps.
Grant. The authority records the holder, the expiry time and a fencing token larger than every token it has issued before, and sends the token and the lease duration to the holder. Google’s Chubby lock service calls such a token a sequencer.
Act and renew. The holder attaches its token to every request it sends to the resource and renews the lease well before expiry. A holder that cannot renew must stop acting before its own clock shows the lease as expired, with a margin for the difference between its clock’s rate and the authority’s.
Expire and grant again. Once the expiry time has passed by the authority’s clock, plus a margin for clock differences, the authority may grant the lease to a new holder, with a larger token.
Fence at the resource. The resource stores the highest token it has accepted, called here the floor. It accepts a request only if the request’s token is at least the floor, and then raises the floor to that token. The comparison and the update form one indivisible step, so that two requests cannot both pass the comparison before either one updates the floor. The floor survives a restart of the resource.
Tell stale from running. Two separate facts decide what a silent holder is. Whether it is still entitled to act, the authority’s records establish. Whether it is still running, only an end record establishes: a report, from the system that runs the holder, that the holder has ended, such as the exit status §13.24 of its process. A holder can be stale, meaning its lease has ended, and still be running; that combination is the dangerous one.
Worked example
A takes a 10-second lease at time 0 s with token 41, and stalls from time 1 s to time 16 s.
At 10 s the lease expires by the authority’s clock. The authority waits a margin of 2 s for clock differences and, at 12 s, grants the lease to B with token 42.
At 12.5 s, B’s first command reaches the device. The device checks , accepts the command and raises its floor from 41 to 42.
At 16 s, A wakes up and sends a command with token 41. The device checks , which is false, and refuses the command. A learns from the refusal that it no longer owns the device.
Without fencing, the device would accept A’s command at 16 s, and A could, for example, reset the device in the middle of B’s run. With fencing, A’s command is refused whether A was paused for 15 seconds or 15 minutes: the protection does not depend on the length of the pause.
The order of arrival matters in one case. Had A woken at 12.2 s, after B’s grant but before B’s first command reached the device, the device would still hold floor 41 and would accept A’s command. Fencing still keeps its promise: A’s command is applied before any of B’s, and once B’s first command has raised the floor to 42, nothing from A is accepted. B must therefore not assume the state of the device; B’s first request should read that state, with token 42.
Stated precisely
Let be the token on a request and the floor stored at the resource. The resource applies, as one indivisible step, the rule The guarantee: once the resource has accepted a request with token , it never again accepts a request with a smaller token. Requests from different holders therefore never interleave at the resource: after the first accepted request of a newer holder, nothing from an older holder is accepted.
The guarantee rests on five assumptions.
Increasing tokens. The authority gives every grant a token larger than all earlier ones, including across its own restarts.
Authentic tokens. Only the authority can produce a valid token, for example because the resource checks a digital signature §5.14 or a message authentication code §5.7 on it. Otherwise any party could send token 999 and take over the resource or lock out the owner.
Complete mediation §1.16. Every path by which an action reaches the resource carries a token and passes the check.
A durable floor. The resource keeps across its own restarts. A floor that returns to 0 accepts every old token again.
An indivisible check. The comparison and the update happen as one step.
A lease without fencing is safe only under timing assumptions. Let be the lease duration; let , a fraction, bound how much faster or slower the holder’s clock runs than the authority’s; and let bound the time from the holder’s check of its lease to the moment its action takes effect at the resource, including any pause and any network delay. Counting from the moment it sent its lease request, the holder may start an action only while its own clock shows less than elapsed. The holder counts from its request because the lease cannot have started earlier; the factor covers a holder clock that runs slow; and subtracting leaves time for the action to take effect before the lease ends. With s, and s, that is s. If has no bound, as for a process that can be suspended for any length of time, no margin is enough, and fencing is needed.
Limits and common mistakes
Expiry is not a stop. A lease that expires at the authority changes nothing inside the former holder, which may act until something refuses it. Nor is silence, or an observer’s timeout §9.34, an expiry. An observer that has heard nothing from a holder for 30 s does not know whether the holder stopped, stalled or is working quietly. If the lease is still valid, taking the resource away on the observer’s own timeout creates two owners.
Fencing protects only the paths that check the token. A second way into the resource, such as a debug connection that ignores tokens, bypasses it. A resource that cannot check tokens at all, such as a simple device, is shared safely only if the old holder’s end is confirmed, or the resource is reset, before the new holder starts. Fencing also undoes nothing: what the old holder completed earlier remains.
A larger number is not authority. A resource that accepts any larger number from anyone lets an attacker take the resource over, or lock the owner out, which is a denial of service §1.28.
Tokens are finite. A counter stored in a fixed number of bits eventually wraps around to 0, so the design needs a rule for that, such as a counter too wide to wrap during the system’s life. A 64-bit counter that grows by one per grant, at one million grants per second, lasts s, about 585,000 years.
An advisory lock §12.41 is a different thing. An advisory lock, such as one taken with fcntl on a system that follows POSIX (Portable Operating System Interface), binds only programs that ask for it, has no expiry, is released when its holder closes the file or exits, and fences nothing.
Questions answered
How is stale ownership distinguished from a still-running operation?
Stale ownership and a still-running operation are distinguished by asking two separate questions, each answered by its own evidence. The first question is whether the holder is still entitled to act. The authority that granted the lease, a grant of exclusive ownership that expires, answers it from its records. If the lease expired and the authority granted it again, with a higher fencing token (a number that grows with every grant), the old holder is stale, whatever the old holder itself believes. The second question is whether the holder is still running. Only an end record answers it: a report from the system that runs the holder that the holder has ended, such as the exit status of its process. Silence answers neither question, because a holder that sends nothing may be stopped, paused or merely slow.
Every combination of answers is possible. A stale holder that is still running is the dangerous one, because it can wake and act. Two defenses make it harmless. Either the resource checks the fencing token on every action and refuses any token lower than the highest it has accepted, or the new holder starts only after an end record shows that the old holder has stopped. A holder that is still running under a valid lease is not stale, and it must not be displaced because an observer stopped hearing from it.
Related entries: Version high-water mark (§8.27), Event epoch and sequence identity (§15.36), Timeout versus cancellation (§13.25), fcntl and advisory locking (§12.41), Epoch fencing and stale authority (§19.3.2), Counter wraparound (§6.21), Exit status (§13.24). Further reading: [407].