﻿# Fence a former etcd lease holder at the resource it modifies

An expired lease releases an etcd lock, but a paused process may resume; a resource-side fencing protocol must reject superseded ownership.

Author: Retrivium team
Updated: 2026-11-06
Canonical URL: https://retrivium.ai/journal/etcd-lease-fencing-token/
Language: en

## The former owner can still execute

A worker acquires an etcd-backed lock, pauses during a long computation and stops refreshing its lease. Another worker later acquires ownership and publishes a new result. The first process can resume with its old instructions and attempt another write. There need not be two valid lock owners inside etcd at that moment. The failure occurs because an external resource still accepts requests from a process whose earlier ownership has been superseded. Killing stale processes promptly is useful operationally, but does not by itself close this correctness boundary.

## Locate the guarantee that etcd actually supplies

The [Lock and Election reference](https://etcd.io/docs/v3.6/dev-guide/api_concurrency_reference_v3/) describes ownership keys tied to leases and their use in transactions protecting etcd updates. The [API design](https://etcd.io/docs/v3.6/learning/api/) describes revisions that order changes within the keyspace. Neither mechanism makes an unrelated filesystem, database or job runner automatically reject a stale worker. Distinguish checking a lease and then writing elsewhere from performing an atomic condition at the destination. A delay between a successful check and the external write can reopen the same race.

## Define a resource-side ownership generation

A proposed fencing protocol gives each accepted ownership generation an ordered token. The protected resource retains the highest generation it has accepted and rejects older generations as part of the same atomic decision as the mutation. A lease identifier should not be presumed to be an ordered generation merely because it is unique. If an etcd revision supplies ordering, preserve the cluster or recovery epoch in the design: monotonic order within one keyspace is not a universal promise across rebuilding or restoring coordination state. The destination's update protocol must define the actual comparison.

## Preserve the boundary before the new owner writes

Fencing rejects an older writer after the destination has accepted newer ownership. It does not magically inform that resource of lease expiry before any newer generation reaches it. If the application requires immediate prohibition at expiry, it needs a stronger integrated authority protocol, not a broader claim about fencing. The same generation can also issue duplicate operations, so operation idempotency remains separate. [Retry ledgers](/journal/provider-post-retry-idempotency-ledger/) address repeated business intentions; they do not replace the ownership comparison that prevents a superseded worker from overwriting current state.

## Test the stale write where it would be accepted

Use an isolated destination implementing the intended conditional update. Pause the first worker, allow a successor to establish a newer accepted generation, then resume the first worker's delayed write. Retain task identity, generation, destination state and rejection outcome. Include a repeated request from the current generation to check the separately declared idempotency behavior. Acceptance requires the stale write to leave the successor's state unchanged. A test showing only that the old lock key disappeared proves lease behavior, not enforcement at the external resource.

## Make recovery preserve the order

The [etcd guarantees](https://etcd.io/docs/v3.6/learning/api_guarantees/) describe revisions as a logical clock for keyspace changes. Preserve the scope of that clock and the destination's durable fencing record during recovery. Recreating a destination without its accepted generation can remove protection while the coordination service appears healthy. [Stable local lock identity](/journal/guide-storage-file-lock/) illustrates a different ownership-lifetime problem; it does not provide distributed fencing. An accepted design names the ordered identity, atomic enforcement point, recovery epoch and remaining duplicate-operation policy. That is stronger than assuming a short lease or a clean election prevents every stale external action.
