From 2468253ad54b4604c8303cd1b7a78ae4bc865fc5 Mon Sep 17 00:00:00 2001 From: Daniel Hilst <392820+dhilst@users.noreply.github.com> Date: Thu, 22 Oct 2026 07:25:43 -0300 Subject: [PATCH 01/18] test(xcat-core): nothing proves the build lock is free after its holder is killed The build lock is a directory created with mkdir, which cannot release itself. A build killed with SIGKILL or by the OOM-killer leaves it behind, and every later build of the same target fails until an operator removes it by hand. No test covered that case. These checks take the lock in a child, confirm a second build is refused by name while the child lives, kill the child with SIGKILL, and assert the lock is recovered. One more holds the boundary that recovery must not widen what a lock excludes: a build of a different checkout still runs beside it. XCAT::NFSLock arrives with them, vendored at build-utils/lib/XCAT/NFSLock.pm beside BuildUtils.pm, because xcat-core builds with no xcat-dep checkout. Its header names the other two copies and records that the three must stay identical. Nothing calls it yet, so these checks fail here. Signed-off-by: Daniel Hilst <392820+dhilst@users.noreply.github.com> --- build-utils/lib/XCAT/NFSLock.pm | 666 ++++++++++++++++++++++++ xCAT-test/unit/build_lock_stale_owner.t | 71 +++ 2 files changed, 737 insertions(+) create mode 100644 build-utils/lib/XCAT/NFSLock.pm create mode 100644 xCAT-test/unit/build_lock_stale_owner.t diff --git a/build-utils/lib/XCAT/NFSLock.pm b/build-utils/lib/XCAT/NFSLock.pm new file mode 100644 index 0000000000..6bb7629456 --- /dev/null +++ b/build-utils/lib/XCAT/NFSLock.pm @@ -0,0 +1,666 @@ +package XCAT::NFSLock; + +# NFS lock protocol +# +# Copies +# This module is vendored three times, because no repo installs another's build tooling: +# xcat-dep lib/XCAT/NFSLock.pm (this file, the original) +# xcat-core build-utils/lib/XCAT/NFSLock.pm +# xcat-core-ci-cd ci/lib/XCAT/NFSLock.pm +# The three must stay identical. They have diverged once already: sweep_stranded was added to +# the ci-cd copy alone, so the protocol hole it covers stayed open in the other two. +# +# A lock on a shared, possibly re-exported, NFS tree. flock and fcntl are not +# available there. The protocol uses mkdir, rmdir, unlink and plain file +# writes. It uses no rename. +# +# Names in this module +# lock.d the lock path given to acquire +# lock.d/metadata the metadata of the owner +# lock.borrow .borrow, beside lock.d +# R, T, δ the options retries, delay and jitter +# +# Assumptions +# 1. mkdir(path) is atomic and exclusive among contenders, and successful +# namespace changes eventually become visible. +# 2. machine-id is unique among participating hosts. +# Cloned VMs and images can share it by accident unless it is regenerated. +# 3. Metadata writes eventually become readable completely and consistently. +# 4. A host never declares one of its own live process incarnations dead. +# 5. A process cannot die after it creates lock.d, until it publishes valid metadata. +# It MAY die while it holds lock.borrow: the borrow carries the same metadata as the lock, +# so a borrow whose taker is provably dead is recovered by the same rule. A borrow left by +# the earlier protocol carries none, and sweep_stranded removes those. +# 6. A crashed worker leaves recoverable state. The owning host eventually +# returns and retries. Eventually one worker and its release complete. +# 7. All participants follow the protocol. +# +# Metadata +# lock.d/metadata contains: +# machine-id, boot-id, pid, pstart, token, hash +# +# Ownership identity: (machine-id, boot-id, pid, pstart, token) +# token is random per acquisition. +# hash = HASH(canonical(SORT(k, v))) over all fields except hash. +# +# If the metadata is missing, cannot be parsed or hashed, or the hash does not +# match, assume a partial or inconsistent read and retry. Never infer stale +# ownership from invalid metadata. +# +# Retry +# R = max retries, T = base delay, δ = jitter +# R > 0, δ >= 0, T >= 3, T > 2δ +# +# Generic retry: +# if retries >= R: fail +# sleep(T + rand(-δ, δ)) +# retries++ +# goto 1 +# +# Protocol +# 1. mkdir lock.d +# - success: write valid metadata, go to 8 +# - EEXIST: continue +# - other error: fail +# 2. Read and validate the metadata. +# - invalid or missing: retry +# - different machine-id: retry +# - same host: save the observed ownership identity +# 3. mkdir lock.borrow, then write valid metadata into it +# - EEXIST: recover the borrow if its metadata proves its taker dead, then retry +# - other failure: retry +# 4. Read and validate the metadata again. +# - invalid, missing, or ownership identity changed: rmdir lock.borrow, retry +# 5. Prove that the recorded (boot-id, pid, pstart) is dead. +# - not provably dead: rmdir lock.borrow, retry +# - dead: continue +# 6. Replace the metadata with the identity of this process and a fresh token. +# 7. rmdir lock.borrow +# 8. Call the worker. +# 9. mkdir lock.borrow, then write valid metadata into it +# - EEXIST: recover the borrow if its metadata proves its taker dead +# - failure: sleep(T + rand(-δ, δ)), retry step 9 +# 10. unlink lock.d/metadata +# 11. rmdir lock.d (the actual unlock) +# 12. rmdir lock.borrow +# +# Core invariants +# lock.d exists => locked +# lock.d absent => acquirable +# invalid metadata => retry only +# different machine-id => never recover here +# lock.borrow exists => ownership transition or release in progress, +# unless its metadata proves its taker dead +# +# Steps 1 to 7 are acquire, step 8 is the caller, steps 9 to 12 are release. +# release does steps 10 and 11 only when the metadata names this acquisition. +# +# Log +# Unless quiet => 1, each lock event prints one line to the selected output handle: +# [nfslock] pid=