summaryrefslogtreecommitdiff
path: root/gear/__init__.py
diff options
context:
space:
mode:
authorJames E. Blair <jeblair@hp.com>2015-02-27 07:53:19 -0800
committerJames E. Blair <jeblair@hp.com>2015-02-27 07:53:19 -0800
commit4c8fc8cd1dcba5c933235b18b0edfe8f099cffd1 (patch)
treeb98a8a5e63cfd4ca0b7caf64a1bab384ef37de83 /gear/__init__.py
parent77ba48998f1f1dd881bff852b7c07ef87998e359 (diff)
downloadgear-4c8fc8cd1dcba5c933235b18b0edfe8f099cffd1.tar.gz
Move running check in getJob
The previous change correctly fixed a race condition, however, if that code path is used, then the accounting in self.waiting_for_jobs could be incorrect. That's not very important because at the moment restarting a worker is not supported, so that variable will not be used again. However, if we move the check a little earlier, we can keep the accounting accurate while still handling the race condition. Change-Id: Ide6f1cd16f72ce7e92dbf2f78e370195afb8a334
Diffstat (limited to 'gear/__init__.py')
-rw-r--r--gear/__init__.py15
1 files changed, 8 insertions, 7 deletions
diff --git a/gear/__init__.py b/gear/__init__.py
index b266b31..494d91a 100644
--- a/gear/__init__.py
+++ b/gear/__init__.py
@@ -1780,6 +1780,14 @@ class Worker(BaseClient):
"""
self.job_lock.acquire()
try:
+ # self.running gets cleared during _shutdown(), before the
+ # stopWaitingForJobs() is called. This check has to
+ # happen with the job_lock held, otherwise there would be
+ # a window for race conditions between manipulation of
+ # "running" and "waiting_for_jobs".
+ if not self.running:
+ raise InterruptedError()
+
self.waiting_for_jobs += 1
self.log.debug("Get job; number of threads waiting for jobs: %s" %
self.waiting_for_jobs)
@@ -1792,13 +1800,6 @@ class Worker(BaseClient):
if not job:
self._updateStateMachines()
- # That variable get cleared during _shutdown(), before the
- # stopWaitingForJobs() is called. The check has to happen with the
- # self.job_lock held, otherwise there would be a window for race
- # conditions between manipulation of "running" and
- # "waiting_for_jobs".
- if not self.running:
- raise InterruptedError()
finally:
self.job_lock.release()