[pbs-devel] [PATCH proxmox v2 2/4] rest-server: close race window when updating worker task count

Fabian Grünbichler f.gruenbichler at proxmox.com
Mon Dec 2 14:04:10 CET 2024


this mimics how the count is updated when spawning a new task - the lock scope
needs to cover the count update itself, else there's a race when multiple
worker's log their result at the same time..

Co-developed-by: Dominik Csapak <d.csapak at proxmox.com>
Signed-off-by: Fabian Grünbichler <f.gruenbichler at proxmox.com>
---
v2: added more comments for current and future readers :)

 proxmox-rest-server/src/worker_task.rs | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/proxmox-rest-server/src/worker_task.rs b/proxmox-rest-server/src/worker_task.rs
index 3ca93965..bbf82ff9 100644
--- a/proxmox-rest-server/src/worker_task.rs
+++ b/proxmox-rest-server/src/worker_task.rs
@@ -923,6 +923,7 @@ impl WorkerTask {
             set_worker_count(hash.len());
         }
 
+        // this wants to access WORKER_TASK_LIST, so we need to drop the lock above
         let res = setup.update_active_workers(Some(&upid));
         if res.is_err() {
             // needed to undo the insertion into WORKER_TASK_LIST above
@@ -1022,8 +1023,11 @@ impl WorkerTask {
         self.log_message(state.result_text());
 
         WORKER_TASK_LIST.lock().unwrap().remove(&self.upid.task_id);
+        // this wants to access WORKER_TASK_LIST, so we need to drop the lock above
         let _ = self.setup.update_active_workers(None);
-        set_worker_count(WORKER_TASK_LIST.lock().unwrap().len());
+        // re-acquire the lock and hold it while updating the count
+        let lock = WORKER_TASK_LIST.lock().unwrap();
+        set_worker_count(lock.len());
     }
 
     /// Log a message.
-- 
2.39.5





More information about the pbs-devel mailing list