2
0
mirror of https://github.com/xcat2/xcat-core.git synced 2026-11-25 03:42:14 +00:00

fix(xcat-core): a lock directory left by a killed build fails every later build of that target

SIGKILL and the OOM-killer run no handler, so a build killed that way leaves its
lock directory behind. Every later build of the same target then fails at mkdir
until an operator removes the directory by hand.
/var/lock/buildrpms.openeuler-20.03sp4-x86_64.lock.d on one build host was
created on 2026-10-14, held no owner file and no live process, and still failed
that target's dep build on 2026-10-21, one second into the genesis step. Every
other openEuler job in that run reported NOT_BUILT.

buildrpms.pl held its own lock, a bare mkdir that recorded no owner, and `use
autodie` wrapped that mkdir, so the EEXIST branch below it was dead code and the
run died on autodie's raw "Can't mkdir(...): File exists". That is why it never
refused anything and never named the build holding the lock.
XCAT::BuildUtils::take_build_lock kept a second hand-rolled lock with the same
gap.

Both now take the lock through XCAT::NFSLock, whose protocol recovers a lock
whose owner is provably dead. The lock stays in /var/lock, which is local,
because what it guards is host-local -- /etc/mock/<cfg>.cfg, /var/lib/mock/<cfg>/
and its buildroot.lock -- so a lock on the shared checkout would serialise the
per-arch builds of one checkout that run on two hosts and share nothing.

Depends on the matching xcat-dep change: that copy of NFSLock carries the
recovery of a borrow whose taker died, and a DESTROY, without which a guard
never releases at scope exit.

Signed-off-by: Daniel Hilst <392820+dhilst@users.noreply.github.com>
This commit is contained in:
Daniel Hilst
2026-10-22 07:25:44 -03:00
parent 2468253ad5
commit 783c46bbf1
2 changed files with 64 additions and 62 deletions
+40 -37
View File
@@ -34,7 +34,7 @@ our @EXPORT_OK = qw(
orig_tarball_name upstream_version resolve_dest
pin_control_version rewrite_changelog_header
reprepro_distributions reprepro_options
lock_id_for take_build_lock
lock_id_for take_build_lock acquire_lock_dir
build_sources_base build_sources_dir prepare_build_sources_dir
remove_build_sources_dir sweep_build_sources_dirs share_build_sources_dir
sh_quote clean_debian_residue git_revision
@@ -125,7 +125,7 @@ sub rewrite_file {
our $CURRENT_CHILD;
# Locks this process holds, weakly. Released by a signal handler or at exit, because
# _BuildLock::DESTROY does not run when a signal ends the process.
# DESTROY does not run when a signal ends the process.
our @LIVE_LOCKS;
END { release_build_locks() }
@@ -646,34 +646,52 @@ sub lock_path_for {
return "$dir/xcatbld-" . lock_id_for($path) . ".lock";
}
# take_build_lock: take the checkout's lock, or die.
# Returns the open handle -- the lock is held for as long as the caller keeps it.
sub take_build_lock {
my ($path, $dir) = @_;
require POSIX;
# A directory, not an flock. A build tree can live on an NFS re-export, where the kernel
# refuses locks outright: every attempt answers errno 524. mkdir(2) is arbitrated by the
# server and needs no lock daemon.
my $lockdir = lock_path_for($path, $dir) . '.d';
unless (mkdir $lockdir) {
die "FATAL: cannot take $lockdir: $!\n" unless $! == POSIX::EEXIST();
my $who = ''; if (open(my $h, '<', "$lockdir/owner")) { local $/; $who = <$h> // ''; close $h }
chomp $who;
die "FATAL: another build of $path already holds $lockdir"
. ($who ? " (held by [$who])" : "") . "\n";
# acquire_lock_dir: take one named lock, or die.
#
# XCAT::NFSLock, not an flock and not a bare mkdir. Its protocol recovers a lock whose owner is
# provably dead, which is the whole point here: a build killed by SIGKILL or the OOM-killer runs
# no handler, and a lock with no recovery then fails every later build of the same target until
# an operator removes it by hand.
#
# The lock lives in /var/lock, which is local. What it guards is host-local too --
# /etc/mock/<cfg>.cfg, /var/lib/mock/<cfg>/ and its buildroot.lock -- so the scope is one host
# and one target. A lock on the shared checkout would serialise the per-arch builds of one
# checkout that run on two hosts, which share nothing.
#
# $held is the text appended when a live build holds the lock. $what names what contends, so the
# two callers keep their own wording -- a checkout for builddebs.pl, a mock target for
# buildrpms.pl -- while the protocol lives in one place.
sub acquire_lock_dir {
my ($lockdir, $what, $held) = @_;
require XCAT::NFSLock;
$what = $lockdir unless defined $what;
$held = '' unless defined $held;
# Two attempts, because recovering a stranded borrow takes one turn and the take-over the
# next. Builds must fail fast rather than queue, so the budget stops there.
my $lock = eval {
XCAT::NFSLock->acquire($lockdir, retries => 2, label => $what, quiet => 1);
};
unless ($lock) {
my $why = $@ // '';
$why =~ s/\s+\z//;
die "FATAL: another build of $what already holds $lockdir\n$held$why\n";
}
if (open(my $ow, '>', "$lockdir/owner")) { print {$ow} "pid=$$\n"; close $ow }
# The caller keeps the returned value; release is by pid so a fork cannot free the parent's.
my $owner = $$;
my $lock = XCAT::BuildUtils::_BuildLock->new($lockdir, $owner);
push @LIVE_LOCKS, $lock;
# Registered weakly, so holding it here does not keep the lock alive past its caller's
# scope. The registry exists only so a signal can release what DESTROY will not.
require Scalar::Util;
push @LIVE_LOCKS, $lock;
Scalar::Util::weaken($LIVE_LOCKS[-1]);
return $lock;
}
# take_build_lock: take the checkout's lock, or die.
# Returns the guard object -- the lock is held for as long as the caller keeps it.
sub take_build_lock {
my ($path, $dir) = @_;
return acquire_lock_dir(lock_path_for($path, $dir) . '.d', $path);
}
#-------------------------------------------------------------------------------
=head3 release_build_locks
@@ -790,21 +808,6 @@ sub cancel_build {
return;
}
{ package XCAT::BuildUtils::_BuildLock;
sub new { my ($c,$d,$p)=@_; return bless { dir=>$d, pid=>$p, released=>0 }, $c }
# Idempotent: a signal handler and then DESTROY both reach here, and the second must not
# remove a directory a LATER build has since taken.
sub release {
my $s = shift;
return if $s->{released};
$s->{released} = 1;
return unless $$ == $s->{pid};
unlink "$s->{dir}/owner";
rmdir $s->{dir};
return;
}
sub DESTROY { shift->release }
}
sub openeuler_build_target {
my ($os, $arch) = @_;
+24 -25
View File
@@ -60,7 +60,7 @@ use XCAT::BuildUtils qw(git_revision source_date_epoch sh sh_or_die usage buildi
stage_genesis_base_sources
write_script read_line targetarch_from_target
openeuler_build_target openeuler_repo_subdir
mock_config_text);
mock_config_text acquire_lock_dir);
use POSIX (); # EEXIST, for the directory build lock (see main())
use Getopt::Long qw(GetOptions);
use POSIX qw(strftime);
@@ -890,16 +890,12 @@ sub merge_core_repos {
# file and the cached chroot dirs left behind are normal mock state, not leaks.)
my %MOCK_INFLIGHT; # ForkManager child pid => mock chroot (-r) name it is building
my $ABORTING = 0;
my $BUILD_LOCK_DIR; # per-target build lock, an atomic mkdir rather than an flock. The build
# tree can live on an NFS re-export, where the kernel refuses locks
# outright ("Clients are not allowed to get file locks or delegations from
# a reexport server"), so every flock there fails with errno 524. mkdir(2)
# is arbitrated by the server and needs no lock daemon. File-scoped so the
# release runs once, from END, for the whole process.
my $BUILD_LOCK_PID; # the pid that took it. ForkManager children inherit $BUILD_LOCK_DIR, and
# their END would release the PARENT's lock -- the flock this replaces
# could not be released by a child, and neither may this.
END { rmdir $BUILD_LOCK_DIR if defined $BUILD_LOCK_DIR and defined $BUILD_LOCK_PID and $$ == $BUILD_LOCK_PID }
my $BUILD_LOCK; # per-target build lock, an XCAT::NFSLock through
# XCAT::BuildUtils::acquire_lock_dir. Held in a file-scoped variable
# because the guard releases on DESTROY: discarding it would free the
# lock at the end of the statement that took it. release checks the pid,
# so a ForkManager child cannot free the PARENT's lock.
END { $BUILD_LOCK->release if defined $BUILD_LOCK }
my @CHILD_FAILURES; # idents (chroot names) of ForkManager children that exited non-zero
@@ -971,7 +967,7 @@ sub abort_builds {
warn "[buildrpms] abort cleanup done\n";
# The re-raise below kills this process by signal, and END blocks do not run then. Release the
# build lock here or a killed build strands it for every later run.
rmdir $BUILD_LOCK_DIR if defined $BUILD_LOCK_DIR and defined $BUILD_LOCK_PID and $$ == $BUILD_LOCK_PID;
$BUILD_LOCK->release if defined $BUILD_LOCK;
$SIG{$sig} = 'DEFAULT';
kill $sig, $$; # re-raise for the correct exit status
}
@@ -1004,24 +1000,27 @@ sub main {
# and race the shared chroot -- and abort_builds' fallback lazy-unmounts this build's own chroot
# binds, which for the SAME target(+ext) are the very dirs a peer uses. So refuse to run a second
# build of the same target(+ext) concurrently. Distinct targets / --mock-uniqueext are independent
# and never conflict. (Held for the process lifetime via $BUILD_LOCK_FH, a file-scoped handle.)
# and never conflict. (Held for the process lifetime via $BUILD_LOCK, a file-scoped guard.)
{
my $key = join('-', $opts{targets}->@*)
. ($opts{mock_uniqueext} ? "-$opts{mock_uniqueext}" : "");
$key =~ s/[^A-Za-z0-9._-]/-/g;
my $lock = "/var/lock/buildrpms.$key.lock.d";
{
unless (mkdir $lock) {
die "FATAL: cannot take the build lock $lock: $!\n" unless $! == POSIX::EEXIST();
die "FATAL: another buildrpms.pl is already building target '@{$opts{targets}}'"
. ($opts{mock_uniqueext} ? " (uniqueext=$opts{mock_uniqueext})" : "") . ".\n"
. " ($lock is held). Concurrent builds of the same target collide on the shared\n"
. " /etc/mock + /var/lib/mock chroot namespace (wrong NVR + a killed peer's\n"
. " cleanup unmounts this build's chroot). Serialize them, or pass a distinct\n"
. " --mock-uniqueext per build.\n";
}
$BUILD_LOCK_DIR = $lock; $BUILD_LOCK_PID = $$;
}
# /var/lock, not the checkout: what this lock guards is host-local, so a lock on the
# shared tree would serialise the per-arch builds of one checkout that run on two hosts.
#
# acquire_lock_dir, not a bare mkdir: NFSLock recovers a lock whose owner is provably
# dead. The copy that used to live here recorded no owner at all, and `use autodie`
# above turned its EEXIST branch into dead code, so a build killed with SIGKILL left the
# directory and failed every later build of the target with autodie's raw
# "Can't mkdir(...): File exists".
$BUILD_LOCK = acquire_lock_dir($lock,
"target '@{$opts{targets}}'"
. ($opts{mock_uniqueext} ? " (uniqueext=$opts{mock_uniqueext})" : ""),
" Concurrent builds of the same target collide on the shared /etc/mock +\n"
. " /var/lib/mock chroot namespace (wrong NVR + a killed peer's cleanup\n"
. " unmounts this build's chroot). Serialize them, or pass a distinct\n"
. " --mock-uniqueext per build.\n");
}
# A builder killed with SIGKILL together with this parent leaves its directory behind.