public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Christoph Heiss <c.heiss@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [pve-devel] [PATCH installer v2 3/6] sys: command: handle EINTR in run_command()
Date: Tue, 13 Feb 2024 16:14:00 +0100	[thread overview]
Message-ID: <20240213151405.1282639-4-c.heiss@proxmox.com> (raw)
In-Reply-To: <20240213151405.1282639-1-c.heiss@proxmox.com>

Previously, the I/O loop would continue endlessly until the subprocess
exited.
This explicit handling allows run_command() to be used with e.g.
alarm().

Signed-off-by: Christoph Heiss <c.heiss@proxmox.com>
---
Changes since v1:
  * new patch

 Proxmox/Sys/Command.pm |  9 ++++++++-
 test/run-command.pl    | 11 +++++++++++
 2 files changed, 19 insertions(+), 1 deletion(-)

diff --git a/Proxmox/Sys/Command.pm b/Proxmox/Sys/Command.pm
index e64e0ee..6389b17 100644
--- a/Proxmox/Sys/Command.pm
+++ b/Proxmox/Sys/Command.pm
@@ -134,10 +134,17 @@ sub run_command {
     $select->add($error);

     my ($ostream, $logout) = ('', '', '');
+    my $caught_sig;

     while ($select->count) {
 	my @handles = $select->can_read (0.2);

+	# If we catch a signal, stop processing & clean up
+	if ($!{EINTR}) {
+	    $caught_sig = 1;
+	    last;
+	}
+
 	Proxmox::UI::process_events();

 	next if !scalar (@handles); # timeout
@@ -170,7 +177,7 @@ sub run_command {

     &$func($logout) if $func;

-    my $ec = wait_for_process($pid);
+    my $ec = wait_for_process($pid, kill => $caught_sig);

     # behave like standard system(); returns -1 in case of errors too
     return ($ec // -1) if $noout;
diff --git a/test/run-command.pl b/test/run-command.pl
index 7d5805e..19bdce0 100755
--- a/test/run-command.pl
+++ b/test/run-command.pl
@@ -27,6 +27,17 @@ my $ret = run_command('bash -c "echo test; sleep 1000; echo test"', sub {
 });
 is($ret, '', 'using CMD_FINISHED');

+# https://bugzilla.proxmox.com/show_bug.cgi?id=4872
+my $prev;
+eval {
+    local $SIG{ALRM} = sub { die "timed out!\n" };
+    $prev = alarm(1);
+    $ret = run_command('sleep 5');
+};
+alarm($prev);
+
+is($@, "timed out!\n", 'SIGALRM interaction');
+
 # Check the log for errors/warnings
 my $log = file_read_all($log_file->filename);
 ok($log !~ m/(WARN|ERROR): /, 'no warnings or errors logged');
--
2.43.0





  parent reply	other threads:[~2024-02-13 15:14 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-13 15:13 [pve-devel] [PATCH installer v2 0/6] fix #4872: properly timeout `traceroute` command in country detection Christoph Heiss
2024-02-13 15:13 ` [pve-devel] [PATCH installer v2 1/6] low-level: initialize UI backend for 'dump-env' subcommand too Christoph Heiss
2024-02-13 15:13 ` [pve-devel] [PATCH installer v2 2/6] sys: command: factor out kill() + waitpid() from run_command() Christoph Heiss
2024-02-13 15:14 ` Christoph Heiss [this message]
2024-02-13 15:14 ` [pve-devel] [PATCH installer v2 4/6] sys: command: allow terminating the process early from log subroutine Christoph Heiss
2024-02-13 15:14 ` [pve-devel] [PATCH installer v2 5/6] sys: command: add option to not print process output to stdout Christoph Heiss
2024-02-13 15:14 ` [pve-devel] [PATCH installer v2 6/6] fix #4872: run env: use run_command() for country detection Christoph Heiss
2024-02-23 15:17 ` [pve-devel] applied-esries: [PATCH installer v2 0/6] fix #4872: properly timeout `traceroute` command in " Thomas Lamprecht

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240213151405.1282639-4-c.heiss@proxmox.com \
    --to=c.heiss@proxmox.com \
    --cc=pve-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal