From 107117b90d0717d4e9bc928e242f9abb361dbadb Mon Sep 17 00:00:00 2001 From: Daniel Hilst <392820+dhilst@users.noreply.github.com> Date: Thu, 29 Oct 2026 14:46:34 -0300 Subject: [PATCH] fix(xcat-core): a build worker can run before the build lock records it The build lock is recovered from a dead owner when no recorded process group is alive. sh() let the command exec before the parent recorded its group, and attach_build_workers() ignored a failed record, so a parent killed in that window left a running command on a lock that the next build recovers. buildrpms.pl kept its Perl builders only in %MOCK_INFLIGHT, so between two sh() calls a builder had no record and a killed parent freed the lock under it. start_build_worker() forks the worker into its own process group and holds it on a pipe. The parent records the group on every held lock and only then writes to the pipe. A failed record, or a parent that dies first, makes the worker exit with status 127 before it runs. sh() and both ForkManager loops in buildrpms.pl start their workers this way, and run_on_finish detaches a builder once it is reaped. Signed-off-by: Daniel Hilst <392820+dhilst@users.noreply.github.com> --- build-utils/lib/XCAT/BuildUtils.pm | 94 +++++++++++++++++++++++++----- buildrpms.pl | 10 +++- 2 files changed, 85 insertions(+), 19 deletions(-) diff --git a/build-utils/lib/XCAT/BuildUtils.pm b/build-utils/lib/XCAT/BuildUtils.pm index c1430315d1..f2f65d2171 100644 --- a/build-utils/lib/XCAT/BuildUtils.pm +++ b/build-utils/lib/XCAT/BuildUtils.pm @@ -35,7 +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 + attach_build_workers detach_build_workers start_build_worker 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 @@ -142,7 +142,9 @@ sub sh { POSIX::sigprocmask(POSIX::SIG_BLOCK(), $blocked, $oldmask) or die "Cannot block build cancellation signals: $!\n"; - my $pid = fork(); + # The child runs nothing until every held lock records its group, which happens while the + # signal mask is still blocked, so a cancellation cannot land between fork and record. + my ($pid, $registered) = start_build_worker(); unless (defined $pid) { my $error = "$!"; POSIX::sigprocmask(POSIX::SIG_SETMASK(), $oldmask) @@ -152,24 +154,20 @@ sub sh { } unless ($pid) { $SIG{INT} = $SIG{TERM} = 'DEFAULT'; - POSIX::setpgid(0, 0) or POSIX::_exit(127); POSIX::sigprocmask(POSIX::SIG_SETMASK(), $oldmask) or POSIX::_exit(127); exec('/bin/sh', '-c', $cmd) or POSIX::_exit(127); } 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. - unless (POSIX::setpgid($pid, $pid) || $!{EACCES} || $!{ESRCH}) { - warn "FATAL: cannot create build process group $pid: $!; retaining locks\n"; - kill 'KILL', $pid; - POSIX::_exit(127); + unless ($registered) { + warn "FATAL: cannot record build process group $pid on the build lock; not running $cmd\n"; + kill 'KILL', -$pid, $pid; + my $got; + do { $got = waitpid($pid, 0) } while ($got == -1 && $!{EINTR}); + detach_build_workers($pid); + POSIX::sigprocmask(POSIX::SIG_SETMASK(), $oldmask); + return 127; } POSIX::sigprocmask(POSIX::SIG_SETMASK(), $oldmask) or do { warn "FATAL: cannot restore signal mask: $!\n"; @@ -737,8 +735,72 @@ sub release_build_locks { # 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; + my $all = 1; + for my $l (@LIVE_LOCKS) { + next unless defined $l; + $all = 0 unless $l->attach($pgid); + } + return $all; +} + +#------------------------------------------------------------------------------- + +=head3 start_build_worker + +Descriptions: + Fork a process in its own process group, and let it run only after every held build lock + records that group. + + A lock whose owner is dead is recoverable unless it records a live group. A worker that + runs before its record exists can therefore outlive a killed parent while a second build + takes the lock. The child waits on a pipe. It continues only when the parent writes to the + pipe, which the parent does after every attach succeeds. When the parent dies first, the + child reads end-of-file and exits. + +Arguments: + $fork - optional coderef that forks, with the return value of fork(). Default: fork(). + Parallel::ForkManager callers pass sub { $pm->start($ident) }. +Returns: + In the parent, in list context: ($pid, 1) when the worker may run, ($pid, 0) when the record + failed and the worker exits with status 127, and () when the fork failed. In scalar + context: the pid, or undef. + In the child: 0, after the parent released it. The child never returns otherwise. + +=cut + +#------------------------------------------------------------------------------- +sub start_build_worker { + my ($fork) = @_; + require POSIX; + $fork //= sub { fork() }; + pipe(my $gate_r, my $gate_w) or return; + my $pid = $fork->(); + unless (defined $pid) { + my $error = $!; + close($gate_r); + close($gate_w); + $! = $error; + return; + } + unless ($pid) { + close($gate_w); + POSIX::setpgid(0, 0) or POSIX::_exit(127); + my ($got, $go) = (undef, ''); + do { $got = sysread($gate_r, $go, 1) } while (!defined($got) && $!{EINTR}); + POSIX::_exit(127) unless $got && $go eq 'g'; + close($gate_r); + return 0; + } + close($gate_r); + # Both sides set the group, so the record never names a pid that is not yet a group leader. + # ESRCH means the child has already gone away. + my $ok = (POSIX::setpgid($pid, $pid) || $!{EACCES} || $!{ESRCH}) ? 1 : 0; + $ok = attach_build_workers($pid) if $ok; + $ok = 0 if $ok && (syswrite($gate_w, 'g') // 0) != 1; + close($gate_w); + # Scalar context gives the pid, so `start_build_worker(...) and next` keeps the meaning of + # `$pm->start and next` whether or not the record succeeded. + return wantarray ? ($pid, $ok) : $pid; } # detach_build_workers: drop that record. Called once the group is reaped, and from diff --git a/buildrpms.pl b/buildrpms.pl index 39a3fe5e32..b318e19030 100755 --- a/buildrpms.pl +++ b/buildrpms.pl @@ -61,7 +61,8 @@ use XCAT::BuildUtils qw(git_revision source_date_epoch sh sh_or_die usage buildi write_script read_line targetarch_from_target openeuler_build_target openeuler_repo_subdir package_skip_reason - mock_config_text acquire_lock_dir); + mock_config_text acquire_lock_dir + start_build_worker detach_build_workers); use POSIX (); # WNOHANG, for the non-blocking waitpid in abort_builds() use Getopt::Long qw(GetOptions); use POSIX qw(strftime); @@ -1050,6 +1051,9 @@ sub main { $pm->run_on_finish(sub { my ($pid, $exit_code, $ident, $exit_signal, $core_dump) = @_; delete $MOCK_INFLIGHT{$pid}; + # The builder's record stays on the build lock until it is reaped, so a parent killed + # between two sh() calls leaves a lock that a later build refuses. + detach_build_workers($pid); remove_build_sources_dir($SOURCES_BASE, $pid); # A child that die()s exits non-zero; one killed by a SIGNAL (SIGKILL / OOM-killer) is reaped # with $exit_code==0 but $exit_signal!=0 (and maybe $core_dump). Checking $exit_code alone @@ -1062,7 +1066,7 @@ sub main { my ($pkg, $target) = $pair->@*; my $ext = $opts{mock_uniqueext} ? "-$opts{mock_uniqueext}" : ""; my $chroot = "$pkg-$target$ext"; # matches buildspkgs/buildpkgs `-r` - $pm->start($chroot) and next; + start_build_worker(sub { $pm->start($chroot) }) and next; $SIG{INT} = $SIG{TERM} = 'DEFAULT'; # child: die on signal; the parent cleans up buildall($pkg, $target); @@ -1079,7 +1083,7 @@ sub main { } for my $target ($opts{targets}->@*) { - $pm->start and next; # no chroot ident: update_repo runs no mock + start_build_worker(sub { $pm->start }) and next; # no chroot ident: update_repo runs no mock $SIG{INT} = $SIG{TERM} = 'DEFAULT'; update_repo($target);