From 82b3c04a73d7b2ae049fcf55c533274c3b29961f Mon Sep 17 00:00:00 2001 From: Daniel Hilst <392820+dhilst@users.noreply.github.com> Date: Thu, 22 Oct 2026 07:21:13 -0300 Subject: [PATCH 01/11] test(xcat-dep): nothing proves a lock recovers from a taker that died holding lock.borrow Assumption 5 of the protocol says a process cannot die while it holds lock.borrow. Under the CI it does: Jenkins sends SIGTERM and the OOM-killer sends SIGKILL. Step 3 is mkdir(lock.borrow), so a borrow left by a killed take-over skips steps 4 to 7 for every later acquire. The lock then names a dead owner that is never evicted, and acquire ends by telling the operator to remove the lock by hand. A dead OWNER recovered in milliseconds while a dead TAKER never did, and no test covered the second case. These checks stage a lock whose owner is dead beside a borrow whose taker is dead, and assert the lock is recovered. Three more hold the boundary: a borrow whose taker is alive is left alone, a borrow with no metadata is not assumed stale, and the borrow a take-over creates carries metadata that names it. A second group covers sweep_stranded against the lock names this project uses, including a live owner and another machine that must survive the sweep. A third asserts that a guard releases when it goes out of scope. Two red proofs are the narrow forms, not a missing subroutine: with sweep_stranded present and its matcher reading '.lock' alone, the checks for .lock.d, .lock.d.borrow and .nfslock fail; with DESTROY removed, the two scope checks fail while their control passes. Signed-off-by: Daniel Hilst <392820+dhilst@users.noreply.github.com> --- t/nfslock_borrow_recovery.t | 179 ++++++++++++++++++++++++++++++++++++ 1 file changed, 179 insertions(+) create mode 100644 t/nfslock_borrow_recovery.t diff --git a/t/nfslock_borrow_recovery.t b/t/nfslock_borrow_recovery.t new file mode 100644 index 0000000..cb775d7 --- /dev/null +++ b/t/nfslock_borrow_recovery.t @@ -0,0 +1,179 @@ +#!/usr/bin/perl +# Assumption 5 of the protocol at the top of lib/XCAT/NFSLock.pm says a process cannot die while +# it holds lock.borrow. Under the CI it does: Jenkins sends SIGTERM and the OOM-killer sends +# SIGKILL. Step 3 is mkdir(lock.borrow), so a borrow left by a killed take-over made the lock +# PERMANENTLY unacquirable -- acquire skipped steps 4 to 7 for ever and told the operator to +# remove the lock by hand. A dead owner recovered in milliseconds while a dead TAKER never did. +# +# The borrow now carries the same metadata as the lock, so the same rule recovers both: a borrow +# whose owner is provably dead is removed, and one that cannot be proven dead is left alone. +use strict; +use warnings; +use Test::More; +use FindBin qw($RealBin); +use lib "$RealBin/../lib"; +use File::Temp qw(tempdir); +use File::Slurper qw(read_text write_text); +use XCAT::NFSLock qw(this_process format_metadata parse_metadata process_start); + +my $dir = tempdir(CLEANUP => 1); +my $me = this_process(); + +# No process can have a pid above the kernel's pid_max, which is 2**22 at most. Asserted rather +# than assumed: every recovery check below depends on this pid really being dead. +my $gone = 2**22 + 7; +is(process_start($gone), undef, 'control: a pid above pid_max names no process'); + +my $live = getppid(); +my $live_start = process_start($live); +ok(defined($live_start), 'control: the parent pid does name a live process'); + +sub metadata { return format_metadata({ %$me, token => 'ab' x 16, @_ }) } + +# Stage a lock and, optionally, a borrow beside it. $borrow is the metadata text for the borrow, +# or the string 'none' to stage a borrow with no metadata at all, as the old protocol left. +sub stage { + my ($name, %o) = @_; + my $path = "$dir/$name"; + mkdir($path) or die "Cannot stage $path: $!"; + write_text("$path/metadata", $o{lock}) if defined($o{lock}); + if (defined($o{borrow})) { + mkdir("$path.borrow") or die "Cannot stage $path.borrow: $!"; + write_text("$path.borrow/metadata", $o{borrow}) if $o{borrow} ne 'none'; + } + return $path; +} + +# Record the waits instead of sleeping, so a lock that cannot be recovered fails at once. +my @slept; +{ + no warnings 'redefine'; + *XCAT::NFSLock::_sleep = sub { push(@slept, $_[0]) }; +} + +# Keep the lock's own log out of the TAP stream. +open(my $log_fh, '>', \my $logged) or die "Cannot capture the log: $!"; +select($log_fh); + +sub take { + my ($path) = @_; + @slept = (); + $logged = ''; + my $lock = eval { XCAT::NFSLock->acquire($path, retries => 2, label => 'probe') }; + return ($lock, $@, $logged); +} + +# ---- 1. the taker died, so the borrow is stale ------------------------------------------- +# The defect. Both the owner and the taker are dead, and before this fix acquire never recovered. +{ + my $path = stage('dead-taker.lock', + lock => metadata(pid => $gone, pstart => '1'), + borrow => metadata(pid => $gone, pstart => '2')); + my ($lock, $err, $log) = take($path); + ok($lock, 'a borrow whose owner is provably dead is recovered') + or diag "acquire died: $err"; + ok(!-d "$path.borrow", 'the stale borrow is gone once the lock is held'); + like($log, qr/took-over/, 'and the lock reports a take-over, not a fresh acquisition'); + undef $lock; +} + +# ---- 2. the taker is alive, so the borrow is a real transition --------------------------- +# Removing this borrow would let two takers rewrite one metadata file. Asserting WHICH outcome: +# a refusal naming the owner, not merely that something died. +{ + my $path = stage('live-taker.lock', + lock => metadata(pid => $gone, pstart => '1'), + borrow => metadata(pid => $live, pstart => $live_start)); + my ($lock, $err) = take($path); + ok(!$lock, 'a borrow held by a live process is left alone'); + like($err, qr/failed after 2 retries/, 'acquire waits out its retries rather than stealing it'); + ok(-d "$path.borrow", 'the live borrow still exists'); +} + +# ---- 3. a borrow with no metadata is never assumed stale -------------------------------- +# What the old protocol left behind, and what the sweeper exists for. The module's own rule is +# that staleness is never inferred from metadata it cannot validate, so this must NOT recover. +{ + my $path = stage('legacy-borrow.lock', + lock => metadata(pid => $gone, pstart => '1'), + borrow => 'none'); + my ($lock, $err) = take($path); + ok(!$lock, 'a borrow with no metadata is not treated as stale'); + like($err, qr/failed after 2 retries/, 'it waits out its retries instead'); + ok(-d "$path.borrow", 'and the borrow is left for the sweeper'); +} + +# ---- 4. a borrow this module creates carries metadata ----------------------------------- +# Scenario 1 can only work if the taker records who it is. Without this the next acquirer has +# nothing to validate and falls into scenario 3. +{ + my $path = stage('records-taker.lock', lock => metadata(pid => $gone, pstart => '1')); + my $seen; + { + no warnings 'redefine'; + my $real = \&XCAT::NFSLock::owner_is_dead; + # Read the borrow's metadata at the moment the take-over holds it. + local *XCAT::NFSLock::owner_is_dead = sub { + $seen //= eval { read_text("$path.borrow/metadata") }; + return $real->(@_); + }; + my $lock = eval { XCAT::NFSLock->acquire($path, retries => 2, label => 'probe') }; + undef $lock; + } + ok(defined($seen) && length($seen), 'the borrow holds metadata while the take-over runs'); + my $id = parse_metadata($seen); + ok($id, 'that metadata is valid and hashes correctly'); + is($id && $id->{pid}, $$, 'and it names the process doing the take-over'); +} + +# ---- 5. the sweeper covers the lock names this project actually uses -------------------- +# sweep_stranded removes state a killed process left under one root. Its matcher read '.lock' +# and '.lock.borrow' only, so it swept mockbuild-all.pl's cell locks and silently missed +# cluster-test.pl's /.build.lock.d and sbuild-all.pl's .sbuild-all..nfslock -- which +# is where the permanent strand this file is about would land. +{ + my $root = "$dir/sweep"; + mkdir $root or die $!; + my %made; + for my $n (qw(.x86_64.lock .build.lock.d .sbuild-all.amd64.nfslock)) { + mkdir "$root/$n" or die $!; + write_text("$root/$n/metadata", metadata(pid => $gone, pstart => '1')); + $made{$n} = "$root/$n"; + } + mkdir "$root/.build.lock.d.borrow" or die $!; + $made{'.build.lock.d.borrow'} = "$root/.build.lock.d.borrow"; + # A live owner and another machine must survive the sweep, so the matcher widening cannot be + # read as "remove more". + mkdir "$root/.live.lock.d" or die $!; + write_text("$root/.live.lock.d/metadata", metadata(pid => $live, pstart => $live_start)); + mkdir "$root/.other.lock.d" or die $!; + write_text("$root/.other.lock.d/metadata", + metadata(pid => $gone, pstart => '1', 'machine-id' => 'f' x 32)); + + my %act = map { $_->{path} => $_->{action} } + XCAT::NFSLock::sweep_stranded($root, undef, exclusive => 1); + is($act{$made{'.x86_64.lock'}}, 'removed', 'a stale .lock is swept, as before'); + is($act{$made{'.build.lock.d'}}, 'removed', 'a stale .lock.d is swept'); + is($act{$made{'.build.lock.d.borrow'}}, 'removed', 'a stranded .lock.d.borrow is swept'); + is($act{$made{'.sbuild-all.amd64.nfslock'}}, 'removed', 'a stale .nfslock is swept'); + is($act{"$root/.live.lock.d"}, 'kept', 'a lock whose owner is alive is kept'); + is($act{"$root/.other.lock.d"}, 'kept', 'a lock owned by another machine is kept'); +} + +# ---- 6. a guard releases when the caller stops holding it ------------------------------ +# Without DESTROY the lock is held to the end of the process, and no take-over recovers it +# either, because its owner is alive. Every caller here keeps its guards in an array for that +# reason, so the leak was invisible. +{ + my $path = "$dir/scope.lock.d"; + { + my $l = XCAT::NFSLock->acquire($path, retries => 1, label => 'scope', quiet => 1); + ok(-d $path, 'control: the lock directory exists while the guard is held'); + } + ok(!-d $path, 'the lock is released when the guard goes out of scope'); + my $again = eval { XCAT::NFSLock->acquire($path, retries => 1, label => 'scope', quiet => 1) }; + ok($again, 'so the same process can take it again') or diag "acquire died: $@"; +} + +select(STDOUT); +done_testing(); From ba87367a7bc49a920558f201c1c6eb20b80f0184 Mon Sep 17 00:00:00 2001 From: Daniel Hilst <392820+dhilst@users.noreply.github.com> Date: Thu, 22 Oct 2026 07:21:14 -0300 Subject: [PATCH 02/11] fix(xcat-dep): a taker that dies holding lock.borrow blocks the lock for ever Step 3 of the NFS lock protocol is mkdir(lock.borrow) and nothing recovers that directory. A take-over killed between step 3 and step 7 leaves it behind, so every later acquire fails step 3, skips steps 4 to 7, never evicts the dead owner, and ends by telling the operator to remove the lock by hand. Assumption 5 made this impossible by fiat; Jenkins SIGTERM and the OOM-killer make it routine. The borrow now carries the same metadata as the lock, written before the death check, which is where a signal lands. A borrow whose metadata proves its taker dead is removed and the acquire retries; one that names a live process, or that cannot be validated, is left alone. So the rule that recovers a dead owner now recovers a dead taker, and staleness is still never inferred from metadata that cannot be validated. release takes the same two steps at step 9, where a killed taker blocked the release instead. A guard also never released at scope exit, because the module had no DESTROY. The lock was then held to the end of the process and no take-over could recover it either, its owner being alive. Every caller here keeps its guards in an array for that reason, which hid the leak. release now returns early unless this acquisition holds the lock, because acquire blesses the object before it holds anything and an acquire that dies would otherwise enter the wait loop in release and never leave it. sweep_stranded moves here from the xcat-core-ci-cd copy, which had it alone, so the hole it covers stayed open in this copy and in xcat-core's. Its matcher read '.lock' and '.lock.borrow', which covered mockbuild-all.pl's cell locks and missed cluster-test.pl's .build.lock.d and sbuild-all.pl's .sbuild-all..nfslock. It now matches all three spellings. A borrow the earlier protocol left carries no metadata, so the sweeper remains the only way to clear those. The header records that three repos vendor this file and that the copies must stay identical. Signed-off-by: Daniel Hilst <392820+dhilst@users.noreply.github.com> --- lib/XCAT/NFSLock.pm | 228 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 200 insertions(+), 28 deletions(-) diff --git a/lib/XCAT/NFSLock.pm b/lib/XCAT/NFSLock.pm index ed2eff2..6bb7629 100644 --- a/lib/XCAT/NFSLock.pm +++ b/lib/XCAT/NFSLock.pm @@ -2,6 +2,14 @@ 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. @@ -19,9 +27,10 @@ package XCAT::NFSLock; # 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; -# - while it holds lock.borrow, until it removes it. +# 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. @@ -57,8 +66,9 @@ package XCAT::NFSLock; # - invalid or missing: retry # - different machine-id: retry # - same host: save the observed ownership identity -# 3. mkdir lock.borrow -# - failure: retry +# 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. @@ -67,7 +77,8 @@ package XCAT::NFSLock; # 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 +# 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) @@ -78,7 +89,8 @@ package XCAT::NFSLock; # lock.d absent => acquirable # invalid metadata => retry only # different machine-id => never recover here -# lock.borrow exists => ownership transition or release in progress +# 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. @@ -86,12 +98,13 @@ package XCAT::NFSLock; # Log # Unless quiet => 1, each lock event prints one line to the selected output handle: # [nfslock] pid=