From a6aa0ae9453190e029dad43dfe3d19e830c1543a Mon Sep 17 00:00:00 2001 From: Thomas Lamprecht Date: Sat, 1 Jul 2023 17:00:12 +0200 Subject: [PATCH 1/1] run with timeout: return if timeout happened in list context This can be relevant info do differentiate if an undef return value happened due to the closure returning it or if it happened due to a timeout. While for quite a few cases this could be handled by a variable captured by the passed closure code reference, acting as messenger, that might often require needless wrapping. Also run_fork_with_timeout warned errors of execution, but any such error handling for an actual timeout is better handled at the call site, as a context-less "got timeout" at STDERR or journal is really not helpful. I checked all call sites of both, run_fork_with_timeout and run_with_timeout most do not use the result at all, and the ones that do are in scalar context. Signed-off-by: Thomas Lamprecht --- src/PVE/Tools.pm | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/PVE/Tools.pm b/src/PVE/Tools.pm index 2d728f8..9ffac12 100644 --- a/src/PVE/Tools.pm +++ b/src/PVE/Tools.pm @@ -118,11 +118,12 @@ sub run_with_timeout { my $prev_alarm = alarm 0; # suspend outer alarm early my $sigcount = 0; + my $got_timeout = 0; my $res; eval { - local $SIG{ALRM} = sub { $sigcount++; die "got timeout\n"; }; + local $SIG{ALRM} = sub { $sigcount++; $got_timeout = 1; die "got timeout\n"; }; local $SIG{PIPE} = sub { $sigcount++; die "broken pipe\n" }; local $SIG{__DIE__}; # see SA bug 4631 @@ -142,9 +143,9 @@ sub run_with_timeout { # this shouldn't happen anymore? die "unknown error" if $sigcount && !$err; # seems to happen sometimes - die $err if $err; + die $err if $err && !wantarray; # assume that user handles timeout err if called in list context - return $res; + return wantarray ? ($res, $got_timeout) : $res; } # flock: we use one file handle per process, so lock file @@ -1015,9 +1016,10 @@ sub run_fork_with_timeout { $res = $child_res->{result}; $error = $child_res->{error}; }; + my $got_timeout = 0; eval { if (defined($timeout)) { - run_with_timeout($timeout, $readvalues); + (undef, $got_timeout) = run_with_timeout($timeout, $readvalues); } else { $readvalues->(); } @@ -1032,7 +1034,7 @@ sub run_fork_with_timeout { die "interrupted by unexpected signal\n" if $sig_received; die $error if $error; - return $res; + return wantarray ? ($res, $got_timeout) : $res; } sub run_fork { -- 2.39.2