public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Hannes Laimer <h.laimer@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [PATCH pve-common] RESTEnvironment: fix possible race in `register_worker`
Date: Tue,  3 Mar 2026 08:15:26 +0100	[thread overview]
Message-ID: <20260303071526.1150-1-h.laimer@proxmox.com> (raw)

If the worker finishes right after we `waitpid` but before we add it to
`WORKER_PIDS` the `worker_reaper` won't `waitpid` it cause it iterates
over `WORKER_PIDS`. So
 - the clean-up triggered by the SIGCHLD won't catch it cause it needs it to
   be in `WORKER_PIDS`
 - and, `register_worker` won't because it was still running when it
   `waitpid`'ed it

Moving the insertion into `WORKER_PIDS` before the `waitpid` solves
this by making sure it is
 - always in the var for `worker_reaper`
 - and, if SIGCHILD should trigger `worker_reaper` before we add it to
   `WORKER_PIDS`, the `waitpid` in `register_worker` itself will catch
   it

Signed-off-by: Hannes Laimer <h.laimer@proxmox.com>
---
 src/PVE/RESTEnvironment.pm | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/src/PVE/RESTEnvironment.pm b/src/PVE/RESTEnvironment.pm
index 4ed5c05..4677687 100644
--- a/src/PVE/RESTEnvironment.pm
+++ b/src/PVE/RESTEnvironment.pm
@@ -99,17 +99,18 @@ my $register_worker = sub {
 
     return if !$pid;
 
-    # do not register if already finished
+    $WORKER_PIDS->{$pid} = {
+        user => $user,
+        upid => $upid,
+    };
+
+    # remove immediately if already finished
     my $waitpid = waitpid($pid, WNOHANG);
     if (defined($waitpid) && ($waitpid == $pid)) {
         delete($WORKER_PIDS->{$pid});
         return;
     }
 
-    $WORKER_PIDS->{$pid} = {
-        user => $user,
-        upid => $upid,
-    };
 };
 
 # initialize environment - must be called once at program startup
-- 
2.47.3





             reply	other threads:[~2026-03-03  7:15 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-03-03  7:15 Hannes Laimer [this message]
2026-03-03  8:24 ` Fabian Grünbichler
2026-03-03  8:37   ` Hannes Laimer
2026-03-04  9:57     ` Fabian Grünbichler

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=20260303071526.1150-1-h.laimer@proxmox.com \
    --to=h.laimer@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