[PATCH 4/5] lib/worker: Create pqueue event before shipping child proc over to cb

robin.sonefors at op5.com robin.sonefors at op5.com
Mon Nov 5 14:58:32 CET 2012


From: Robin Sonefors <robin.sonefors at op5.com>

It's quite possible to build a worker so that it doesn't have to work
asynchronously (or can work very, very, very fast), so that it will call
finish_job before we've finished setting up the child process, which
will cause random memory access errors that can cause worker crashes.
By finishing setup before handing over control to the worker callback,
we can close that possibility.

Signed-off-by: Robin Sonefors <robin.sonefors at op5.com>
---
 lib/worker.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/lib/worker.c b/lib/worker.c
index c7d6de4..7fb6d59 100644
--- a/lib/worker.c
+++ b/lib/worker.c
@@ -532,14 +532,16 @@ static void spawn_job(struct kvvec *kvv, int(*cb)(child_process *))
 
 	gettimeofday(&cp->ei->start, NULL);
 	cp->request = kvv;
+	cp->ei->sq_event = squeue_add(sq, cp->timeout + time(NULL), cp);
+	started++;
+	running_jobs++;
 	result = cb(cp);
 	if (result < 0) {
 		job_error(cp, kvv, "Failed to start child: %s: %s", runcmd_strerror(result), strerror(errno));
+		squeue_remove(sq, cp->ei->sq_event);
+		running_jobs--;
 		return;
 	}
-	cp->ei->sq_event = squeue_add(sq, cp->timeout + time(NULL), cp);
-	started++;
-	running_jobs++;
 }
 
 static int receive_command(int sd, int events, void *arg)
-- 
1.7.11.7


------------------------------------------------------------------------------
LogMeIn Central: Instant, anywhere, Remote PC access and management.
Stay in control, update software, and manage PCs from one command center
Diagnose problems and improve visibility into emerging IT issues
Automate, monitor and manage. Do more in less time with Central
http://p.sf.net/sfu/logmein12331_d2d




More information about the Developers mailing list