mirror of https://github.com/python/cpython
Merged revisions 80487,80489 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/py3k ................ r80487 | antoine.pitrou | 2010-04-26 00:01:43 +0200 (lun., 26 avril 2010) | 12 lines Merged revisions 80484 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r80484 | antoine.pitrou | 2010-04-25 23:40:32 +0200 (dim., 25 avril 2010) | 6 lines Issue #2302: Fix a race condition in SocketServer.BaseServer.shutdown, where the method could block indefinitely if called just before the event loop started running. This also fixes the occasional freezes witnessed in test_httpservers. ........ ................ r80489 | antoine.pitrou | 2010-04-26 00:19:43 +0200 (lun., 26 avril 2010) | 9 lines Merged revisions 80480 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r80480 | antoine.pitrou | 2010-04-25 23:15:50 +0200 (dim., 25 avril 2010) | 3 lines Replace a Lock with a better suited Event. ........ ................
This commit is contained in:
parent
d5781076eb
commit
e312391529
|
@ -197,7 +197,7 @@ class BaseServer:
|
||||||
self.server_address = server_address
|
self.server_address = server_address
|
||||||
self.RequestHandlerClass = RequestHandlerClass
|
self.RequestHandlerClass = RequestHandlerClass
|
||||||
self.__is_shut_down = threading.Event()
|
self.__is_shut_down = threading.Event()
|
||||||
self.__serving = False
|
self.__shutdown_request = False
|
||||||
|
|
||||||
def server_activate(self):
|
def server_activate(self):
|
||||||
"""Called by constructor to activate the server.
|
"""Called by constructor to activate the server.
|
||||||
|
@ -214,17 +214,19 @@ class BaseServer:
|
||||||
self.timeout. If you need to do periodic tasks, do them in
|
self.timeout. If you need to do periodic tasks, do them in
|
||||||
another thread.
|
another thread.
|
||||||
"""
|
"""
|
||||||
self.__serving = True
|
|
||||||
self.__is_shut_down.clear()
|
self.__is_shut_down.clear()
|
||||||
while self.__serving:
|
try:
|
||||||
# XXX: Consider using another file descriptor or
|
while not self.__shutdown_request:
|
||||||
# connecting to the socket to wake this up instead of
|
# XXX: Consider using another file descriptor or
|
||||||
# polling. Polling reduces our responsiveness to a
|
# connecting to the socket to wake this up instead of
|
||||||
# shutdown request and wastes cpu at all other times.
|
# polling. Polling reduces our responsiveness to a
|
||||||
r, w, e = select.select([self], [], [], poll_interval)
|
# shutdown request and wastes cpu at all other times.
|
||||||
if r:
|
r, w, e = select.select([self], [], [], poll_interval)
|
||||||
self._handle_request_noblock()
|
if self in r:
|
||||||
self.__is_shut_down.set()
|
self._handle_request_noblock()
|
||||||
|
finally:
|
||||||
|
self.__shutdown_request = False
|
||||||
|
self.__is_shut_down.set()
|
||||||
|
|
||||||
def shutdown(self):
|
def shutdown(self):
|
||||||
"""Stops the serve_forever loop.
|
"""Stops the serve_forever loop.
|
||||||
|
@ -233,7 +235,7 @@ class BaseServer:
|
||||||
serve_forever() is running in another thread, or it will
|
serve_forever() is running in another thread, or it will
|
||||||
deadlock.
|
deadlock.
|
||||||
"""
|
"""
|
||||||
self.__serving = False
|
self.__shutdown_request = True
|
||||||
self.__is_shut_down.wait()
|
self.__is_shut_down.wait()
|
||||||
|
|
||||||
# The distinction between handling, getting, processing and
|
# The distinction between handling, getting, processing and
|
||||||
|
|
|
@ -34,14 +34,14 @@ class TestServerThread(threading.Thread):
|
||||||
threading.Thread.__init__(self)
|
threading.Thread.__init__(self)
|
||||||
self.request_handler = request_handler
|
self.request_handler = request_handler
|
||||||
self.test_object = test_object
|
self.test_object = test_object
|
||||||
self.test_object.lock.acquire()
|
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
self.server = HTTPServer(('', 0), self.request_handler)
|
self.server = HTTPServer(('', 0), self.request_handler)
|
||||||
self.test_object.PORT = self.server.socket.getsockname()[1]
|
self.test_object.PORT = self.server.socket.getsockname()[1]
|
||||||
self.test_object.lock.release()
|
self.test_object.server_started.set()
|
||||||
|
self.test_object = None
|
||||||
try:
|
try:
|
||||||
self.server.serve_forever()
|
self.server.serve_forever(0.05)
|
||||||
finally:
|
finally:
|
||||||
self.server.server_close()
|
self.server.server_close()
|
||||||
|
|
||||||
|
@ -51,13 +51,12 @@ class TestServerThread(threading.Thread):
|
||||||
|
|
||||||
class BaseTestCase(unittest.TestCase):
|
class BaseTestCase(unittest.TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.lock = threading.Lock()
|
self.server_started = threading.Event()
|
||||||
self.thread = TestServerThread(self, self.request_handler)
|
self.thread = TestServerThread(self, self.request_handler)
|
||||||
self.thread.start()
|
self.thread.start()
|
||||||
self.lock.acquire()
|
self.server_started.wait()
|
||||||
|
|
||||||
def tearDown(self):
|
def tearDown(self):
|
||||||
self.lock.release()
|
|
||||||
self.thread.stop()
|
self.thread.stop()
|
||||||
|
|
||||||
def request(self, uri, method='GET', body=None, headers={}):
|
def request(self, uri, method='GET', body=None, headers={}):
|
||||||
|
|
|
@ -243,6 +243,30 @@ class SocketServerTest(unittest.TestCase):
|
||||||
# socketserver.DatagramRequestHandler,
|
# socketserver.DatagramRequestHandler,
|
||||||
# self.dgram_examine)
|
# self.dgram_examine)
|
||||||
|
|
||||||
|
def test_shutdown(self):
|
||||||
|
# Issue #2302: shutdown() should always succeed in making an
|
||||||
|
# other thread leave serve_forever().
|
||||||
|
class MyServer(socketserver.TCPServer):
|
||||||
|
pass
|
||||||
|
|
||||||
|
class MyHandler(socketserver.StreamRequestHandler):
|
||||||
|
pass
|
||||||
|
|
||||||
|
threads = []
|
||||||
|
for i in range(20):
|
||||||
|
s = MyServer((HOST, 0), MyHandler)
|
||||||
|
t = threading.Thread(
|
||||||
|
name='MyServer serving',
|
||||||
|
target=s.serve_forever,
|
||||||
|
kwargs={'poll_interval':0.01})
|
||||||
|
t.daemon = True # In case this function raises.
|
||||||
|
threads.append((t, s))
|
||||||
|
for t, s in threads:
|
||||||
|
t.start()
|
||||||
|
s.shutdown()
|
||||||
|
for t, s in threads:
|
||||||
|
t.join()
|
||||||
|
|
||||||
|
|
||||||
def test_main():
|
def test_main():
|
||||||
if imp.lock_held():
|
if imp.lock_held():
|
||||||
|
|
|
@ -40,6 +40,11 @@ Core and Builtins
|
||||||
Library
|
Library
|
||||||
-------
|
-------
|
||||||
|
|
||||||
|
- Issue #2302: Fix a race condition in SocketServer.BaseServer.shutdown,
|
||||||
|
where the method could block indefinitely if called just before the
|
||||||
|
event loop started running. This also fixes the occasional freezes
|
||||||
|
witnessed in test_httpservers.
|
||||||
|
|
||||||
- Issue #8524: When creating an SSL socket, the timeout value of the
|
- Issue #8524: When creating an SSL socket, the timeout value of the
|
||||||
original socket wasn't retained (instead, a socket with a positive timeout
|
original socket wasn't retained (instead, a socket with a positive timeout
|
||||||
would be turned into a non-blocking SSL socket).
|
would be turned into a non-blocking SSL socket).
|
||||||
|
|
Loading…
Reference in New Issue