From: Thomas Lamprecht Date: Fri, 15 Dec 2017 16:00:30 +0000 (+0100) Subject: fork_worker: use separate pipe for status messages X-Git-Url: https://git.proxmox.com/?p=pve-common.git;a=commitdiff_plain;h=ed52a8435a6dcdf77568280679355eba6c5857b6;ds=sidebyside fork_worker: use separate pipe for status messages We forced line wise flushing of the workers STDOUT and STDERR to capture the final status (TASK OK/TASK ERROR). Thus, if the code executed in the worker wanted to flush explicitly, e.g., when the last output wasn't new line terminated but needed to reach the users eyes, the parent just ignored that. This leads to confusing results in CLI handlers using fork_workers. So remove the buffering logic completely and introduce a separate pipe for sending the final status. Said pipe gets once read after the child closes (EOF) its STDOUT. Signed-off-by: Thomas Lamprecht --- diff --git a/src/PVE/RESTEnvironment.pm b/src/PVE/RESTEnvironment.pm index 0ad6dba..3b1142d 100644 --- a/src/PVE/RESTEnvironment.pm +++ b/src/PVE/RESTEnvironment.pm @@ -396,6 +396,7 @@ sub fork_worker { my @psync = POSIX::pipe(); my @csync = POSIX::pipe(); + my @ctrlfd = POSIX::pipe() if $sync; my $node = $self->{nodename}; @@ -427,9 +428,11 @@ sub fork_worker { POSIX::setsid(); POSIX::close ($psync[0]); + POSIX::close ($ctrlfd[0]) if $sync; POSIX::close ($csync[1]); $outfh = $sync ? $psync[1] : undef; + my $resfh = $sync ? $ctrlfd[1] : undef; eval { PVE::INotify::inotify_close(); @@ -450,6 +453,7 @@ sub fork_worker { if !open(STDIN, "flush(); - } - } + print $readbuf; + select->flush(); + + print $outfh $readbuf; + $outfh->flush(); } else { # some commands daemonize without closing stdout last if !PVE::ProcFSTools::check_process_running($cpid); } } + + # get status (error or OK) + POSIX::read($ctrlfd[0], $readbuf, 4096); + if ($readbuf =~ m/^TASK OK\n?$/) { + print $outfh $readbuf; + } elsif ($readbuf =~ m/^TASK ERROR: (.*)\n?$/) { + print STDERR "$1\n"; + print $outfh "\n$readbuf"; + } else { + die "got unexpected control message: $readbuf\n"; + } + $outfh->flush(); }; my $err = $@; POSIX::close($psync[0]); - - if ($outbuf) { # just to be sure - print $outbuf; - if ($outfh) { - print $outfh $outbuf; - } - } + POSIX::close($ctrlfd[0]); if ($err) { $err =~ s/\n/ /mg;