commit 4824d623487e0d305029ea46326eba94b4e4b738 Author: Damian Johnson atagar@torproject.org Date: Sun Jun 12 20:40:49 2011 -0700
Removing indefinite blocking on socket recv
When shutting down we can't join on _thread unless the socket receives data because we didn't have a timeout. This issues a 20 ms timeout on socket reads and cleans up _thread when we close. --- TorCtl.py | 5 +---- TorUtil.py | 11 ++++++++++- 2 files changed, 11 insertions(+), 5 deletions(-)
diff --git a/TorCtl.py b/TorCtl.py index 5403a95..b87f5f2 100755 --- a/TorCtl.py +++ b/TorCtl.py @@ -642,11 +642,8 @@ class Connection: self._queue.put("CLOSE") self._eventQueue.put((time.time(), "CLOSE")) self._closed = 1 - # XXX: For some reason, this does not cause the readline in - # self._read_reply() to return immediately. The _loop() thread - # thus tends to stick around until some event causes data to come - # back... self._s.close() + self._thread.join() self._eventThread.join() finally: self._sendLock.release() diff --git a/TorUtil.py b/TorUtil.py index 9e28779..c1bebf9 100644 --- a/TorUtil.py +++ b/TorUtil.py @@ -188,6 +188,10 @@ def unescape_dots(s, translate_nl=1): # XXX: Exception handling class BufSock: def __init__(self, s): + # sets a timeout on our recv operations + s.settimeout(0.02) + self._isDone = False + self._s = s self._buf = []
@@ -200,7 +204,11 @@ class BufSock: return result
while 1: - s = self._s.recv(128) + try: + s = self._s.recv(128) + except socket.timeout: + if not self._isDone: continue + if not s: return None # XXX: This really does need an exception # raise ConnectionClosed() @@ -221,6 +229,7 @@ class BufSock: self._s.send(s)
def close(self): + self._isDone = True self._s.close()
# SocketServer.TCPServer is nuts..