Avoid race condition in dict iteration.

Its possible that when iterating Arbiter.WORKERS in manage_workers we
get interupted to handle a SIGCHLD which will pop the child PID from
the dict which results in a "dict changed size while iterating
error. Reported on IRC. Simple fix is to just copy the dict into a
list that we iterate.
This commit is contained in:
Paul J. Davis 2011-06-14 16:17:47 -04:00
parent e1e634a8aa
commit 6d69509bb6

View File

@ -419,12 +419,10 @@ class Arbiter(object):
if len(self.WORKERS.keys()) < self.num_workers:
self.spawn_workers()
num_to_kill = len(self.WORKERS) - self.num_workers
for i in range(num_to_kill, 0, -1):
pid, age = 0, sys.maxint
for (wpid, worker) in self.WORKERS.iteritems():
if worker.age < age:
pid, age = wpid, worker.age
workers = self.WORKERS.items()
workers.sort(key=lambda w: w.age)
while len(workers) > self.num_workers:
(pid, _) = workers.pop(0)
self.kill_worker(pid, signal.SIGQUIT)
def spawn_worker(self):