From 57c8242dc2792f9e6ad267d22ed0aae9595998aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabian=20Gr=C3=BCnbichler?= Date: Mon, 2 Dec 2024 14:04:10 +0100 Subject: [PATCH] rest-server: close race window when updating worker task count MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Signed-off-by: Fabian Grünbichler --- 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.