Fix hang on shutdown.

This commit is contained in:
Aldo Cortesi 2011-03-15 17:21:35 +13:00
parent fb28e71f0b
commit 6d5c32ad4b
4 changed files with 16 additions and 10 deletions

View File

@ -1277,7 +1277,7 @@ class ConsoleMaster(flow.FlowMaster):
slave = controller.Slave(q, self.server)
slave.start()
try:
while not self._shutdown:
while not controller.exit:
self.statusbar.redraw()
size = self.drawscreen()
self.tick(q)

View File

@ -16,6 +16,8 @@
import sys
import Queue, threading
exit = False
#begin nocover
class Msg:
@ -34,7 +36,12 @@ class Msg:
self.acked = False
try:
masterq.put(self, timeout=3)
return self.q.get()
while not exit:
try:
g = self.q.get(timeout=0.5)
except Queue.Empty:
continue
return g
except (Queue.Empty, Queue.Full):
return None
@ -52,7 +59,6 @@ class Slave(threading.Thread):
class Master:
def __init__(self, server):
self.server = server
self._shutdown = False
self.masterq = None
def tick(self, q):
@ -73,7 +79,7 @@ class Master:
self.masterq = q
slave = Slave(q, self.server)
slave.start()
while not self._shutdown:
while not exit:
self.tick(q)
self.shutdown()
@ -86,9 +92,8 @@ class Master:
msg.ack()
def shutdown(self):
if not self._shutdown:
self._shutdown = True
global exit
if not exit:
exit = True
if self.server:
self.server.shutdown()

View File

@ -1,6 +1,6 @@
import Queue
from cStringIO import StringIO
from libmproxy import console, proxy, filt, flow
from libmproxy import console, proxy, filt, flow, controller
import tutils
import libpry
@ -442,7 +442,7 @@ class uFlowMaster(libpry.AutoTree):
assert fm.do_server_playback(tutils.tflow())
q = Queue.Queue()
fm.tick(q)
assert fm._shutdown
assert controller.exit
def test_stickycookie(self):
s = flow.State()

View File

@ -59,6 +59,7 @@ class TestMaster(controller.Master):
class ProxyThread(threading.Thread):
def __init__(self, port, testq):
self.tmaster = TestMaster(port, testq)
controller.exit = False
threading.Thread.__init__(self)
def run(self):