Skip to content

Commit

Permalink
put a Lock around self.client usage in call()
Browse files Browse the repository at this point in the history
  • Loading branch information
fetep committed Mar 25, 2012
1 parent aa51195 commit 80e6929
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion circus/flapping.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import errno
from threading import Thread, Timer
from threading import Lock, Thread, Timer
import time
import uuid

Expand Down Expand Up @@ -27,6 +27,7 @@ def __init__(self, context, endpoint, pubsub_endpoint, check_delay):
self.timers = {}
self.configs = {}
self.tries = {}
self.zeromq_lock = Lock()

@debuglog
def initialize(self):
Expand Down Expand Up @@ -84,8 +85,10 @@ def handle_recv(self, data):
self.update_conf(topic_parts[1])

def call(self, cmd):
self.zeromq_lock.acquire()
self.client.send(json.dumps(cmd))
msg = self.client.recv()
self.zeromq_lock.release()
return json.loads(msg)

def update_conf(self, watcher_name):
Expand Down

1 comment on commit 80e6929

@tarekziade
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your current code does not guarantee you release the lock, in case the send/receive lines raise an error. You miss a try...finally

but in Py there is a better syntax, you can do:

with self.zeromq_lock:
   ....

so you don't have to call acquire/release explicitly

Please sign in to comment.