public inbox for pbs-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Dominik Csapak <d.csapak@proxmox.com>
To: pbs-devel@lists.proxmox.com
Subject: [pbs-devel] [PATCH proxmox-backup v2] api2/node/termproxy: fix zombies on worker abort
Date: Wed, 29 Jul 2020 13:50:27 +0200	[thread overview]
Message-ID: <20200729115027.23553-1-d.csapak@proxmox.com> (raw)

tokios kill_on_drop sometimes leaves zombies around, especially
when there is not another tokio::process::Command spawned after

so instead of relying on the 'kill_on_drop' feature, we explicitly
kill the child on a worker abort. to be able to do this
we have to use 'tokio::select' instead of 'futures::select' since
the latter requires the future to be fused, which consumes the
child handle, leaving us no possibility to kill it after fusing.
(tokio::select does not need the futures to be fused, so we
can reuse the child future after the select again)

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
---
changes from v1:
* if the worker future is ok, bubble errors up from child kill/await
  if the worker future is err, log errors from kill/await and return
  the error from the abort
 src/api2/node.rs | 36 ++++++++++++++++++++++++++----------
 1 file changed, 26 insertions(+), 10 deletions(-)

diff --git a/src/api2/node.rs b/src/api2/node.rs
index 9be5fe6d..5a3ea0ff 100644
--- a/src/api2/node.rs
+++ b/src/api2/node.rs
@@ -2,10 +2,7 @@ use std::net::TcpListener;
 use std::os::unix::io::AsRawFd;
 
 use anyhow::{bail, format_err, Error};
-use futures::{
-    future::{FutureExt, TryFutureExt},
-    select,
-};
+use futures::future::{FutureExt, TryFutureExt};
 use hyper::body::Body;
 use hyper::http::request::Parts;
 use hyper::upgrade::Upgraded;
@@ -172,7 +169,6 @@ async fn termproxy(
             let mut cmd = tokio::process::Command::new("/usr/bin/termproxy");
 
             cmd.args(&arguments)
-                .kill_on_drop(true)
                 .stdout(std::process::Stdio::piped())
                 .stderr(std::process::Stdio::piped());
 
@@ -199,8 +195,9 @@ async fn termproxy(
                 Ok::<(), Error>(())
             };
 
-            select!{
-                res = child.fuse() => {
+            let mut needs_kill = false;
+            let res = tokio::select!{
+                res = &mut child => {
                     let exit_code = res?;
                     if !exit_code.success() {
                         match exit_code.code() {
@@ -210,10 +207,29 @@ async fn termproxy(
                     }
                     Ok(())
                 },
-                res = stdout_fut.fuse() => res,
-                res = stderr_fut.fuse() => res,
-                res = worker.abort_future().fuse() => res.map_err(Error::from),
+                res = stdout_fut => res,
+                res = stderr_fut => res,
+                res = worker.abort_future() => {
+                    needs_kill = true;
+                    res.map_err(Error::from)
+                }
+            };
+
+            if needs_kill {
+                if res.is_ok() {
+                    child.kill()?;
+                    child.await?;
+                    return Ok(());
+                }
+
+                if let Err(err) = child.kill() {
+                    worker.warn(format!("error killing termproxy: {}", err));
+                } else if let Err(err) = child.await {
+                    worker.warn(format!("error awaiting termproxy: {}", err));
+                }
             }
+
+            res
         },
     )?;
 
-- 
2.20.1





             reply	other threads:[~2020-07-29 11:50 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-07-29 11:50 Dominik Csapak [this message]
2020-07-30  8:39 ` [pbs-devel] applied: " Wolfgang Bumiller

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=20200729115027.23553-1-d.csapak@proxmox.com \
    --to=d.csapak@proxmox.com \
    --cc=pbs-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