mirror of
https://github.com/xcat2/xcat-core.git
synced 2026-11-23 02:42:17 +00:00
fix(xcat-core): the build lock is recovered while the previous build still runs
BuildUtils::sh() runs each build command in its own process group, so SIGKILL on the wrapper leaves that group running. The lock records the wrapper, so the next build proved the wrapper dead and took the lock, and two builds then wrote one checkout and one mock target. cancel_build keeps the lock on purpose when a group survives its cleanup, and that recovery undid the retention. sh() now records the group on every lock this process holds, before it restores the signal mask, so a cancellation cannot land between the fork and the record. It drops the record after the group is reaped. cancel_build drops it only when kill(0, -pgid) answers ESRCH, which is the test it already used to decide it may release; on the retention path the record stays, and a later acquire reads it and refuses the lock. The vendored XCAT::NFSLock carries the rule: an owner is recoverable only when it is provably dead and every attached group is gone. detach refuses while any process is in the group, so the retention does not depend on which call site remembered to run. Depends on xcat-dep/#93, which carries the same change to the original. Signed-off-by: Daniel Hilst <392820+dhilst@users.noreply.github.com>
This commit is contained in:
@@ -35,6 +35,7 @@ our @EXPORT_OK = qw(
|
||||
pin_control_version rewrite_changelog_header
|
||||
reprepro_distributions reprepro_options
|
||||
lock_id_for take_build_lock acquire_lock_dir
|
||||
attach_build_workers detach_build_workers
|
||||
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
|
||||
@@ -157,6 +158,10 @@ sub sh {
|
||||
}
|
||||
|
||||
local $CURRENT_CHILD = $pid; # also the command's process-group ID
|
||||
# Recorded on every held lock BEFORE the signal mask comes back, so a cancellation cannot
|
||||
# land between the fork and the record. A lock whose owner dies is recoverable; a lock with
|
||||
# a group still in it is not, and this is what tells the two apart.
|
||||
attach_build_workers($pid);
|
||||
# Both sides set the group, so neither depends on which side runs first.
|
||||
# EACCES means the child already exec'd, after setting its group; ESRCH
|
||||
# means it has already gone away.
|
||||
@@ -174,6 +179,7 @@ sub sh {
|
||||
my $got;
|
||||
do { $got = waitpid($pid, 0) } while ($got == -1 && $!{EINTR});
|
||||
my $status = $?;
|
||||
detach_build_workers($pid);
|
||||
return 127 if $got == -1;
|
||||
return ($status & 127) ? 128 + ($status & 127) : $status >> 8;
|
||||
}
|
||||
@@ -725,6 +731,23 @@ sub release_build_locks {
|
||||
return;
|
||||
}
|
||||
|
||||
# attach_build_workers: record a process group on every lock this process holds. A build lock
|
||||
# protects a checkout and a mock target, and sh() runs each command in its own group, so the
|
||||
# lock has to name the group rather than only the wrapper that started it.
|
||||
sub attach_build_workers {
|
||||
my ($pgid) = @_;
|
||||
for my $l (@LIVE_LOCKS) { $l->attach($pgid) if defined $l }
|
||||
return;
|
||||
}
|
||||
|
||||
# detach_build_workers: drop that record. Called once the group is reaped, and from
|
||||
# cancel_build once kill(0, -pgid) answers ESRCH.
|
||||
sub detach_build_workers {
|
||||
my ($pgid) = @_;
|
||||
for my $l (@LIVE_LOCKS) { $l->detach($pgid) if defined $l }
|
||||
return;
|
||||
}
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
|
||||
=head3 install_build_cancellation
|
||||
@@ -802,13 +825,16 @@ sub cancel_build {
|
||||
# The shell exiting is not enough: its workers may still exist.
|
||||
if (!kill(0, -$pgid) && $!{ESRCH}) {
|
||||
$CURRENT_CHILD = undef;
|
||||
detach_build_workers($pgid);
|
||||
release_build_locks();
|
||||
return;
|
||||
}
|
||||
select undef, undef, undef, 0.1;
|
||||
}
|
||||
}
|
||||
# Never let END/DESTROY unlock a checkout whose workers may still run.
|
||||
# Never let END/DESTROY unlock a checkout whose workers may still run. The group is
|
||||
# still recorded on the lock, because it was not detached, so a later build reads that
|
||||
# record and refuses the lock instead of recovering it from a dead owner.
|
||||
warn "FATAL: build process group $pgid has not disappeared; retaining locks\n";
|
||||
POSIX::_exit(1);
|
||||
}
|
||||
|
||||
@@ -268,6 +268,9 @@ sub acquire {
|
||||
my $again = _read_metadata($abs);
|
||||
return 0 unless defined($again) && _key($again) eq _key($observed);
|
||||
return 0 unless owner_is_dead($again, $here);
|
||||
# The owner is dead. The work it started may not be.
|
||||
my @live = live_workers($abs);
|
||||
if (@live) { $workers = $live[0]{pgid}; return 0 }
|
||||
my $mine = this_process();
|
||||
_at('before-commit');
|
||||
_write_metadata($abs, $mine);
|
||||
@@ -721,6 +724,7 @@ sub attach {
|
||||
sub detach {
|
||||
my ($self, $pgid) = @_;
|
||||
return 0 unless defined($pgid) && $pgid =~ /\A[1-9][0-9]*\z/;
|
||||
return 0 unless !kill(0, -$pgid) && $!{ESRCH};
|
||||
delete($self->{workers}{$pgid});
|
||||
my $dir = _worker_dir($self->{path});
|
||||
opendir(my $dh, $dir) or return 0;
|
||||
@@ -884,6 +888,12 @@ sub sweep_stranded {
|
||||
my $lock_of = sub { my $p = shift; $p =~ s/\.borrow(?:\.[0-9]+)?\z//; return $p };
|
||||
for my $d (sort { ($b =~ /\.borrow(?:\.[0-9]+)?\z/ ? 1 : 0) <=> ($a =~ /\.borrow(?:\.[0-9]+)?\z/ ? 1 : 0)
|
||||
|| $a cmp $b } @found) {
|
||||
my @busy = live_workers($lock_of->($d));
|
||||
if (@busy) {
|
||||
push @report, { path => $d, action => 'kept',
|
||||
why => "build workers of process group $busy[0]{pgid} still exist" };
|
||||
next;
|
||||
}
|
||||
if ($d =~ /\.borrow(?:\.[0-9]+)?\z/) {
|
||||
unless ($opt{exclusive}) {
|
||||
push @report, { path => $d, action => 'kept',
|
||||
|
||||
Reference in New Issue
Block a user