Commit 0098c9d6 authored by Neal Norwitz's avatar Neal Norwitz

Introduce a lock to fix a race condition which caused an exception in the test.

Some buildbots were consistently failing (e.g., amd64).
Also remove a couple of semi-colons.
parent 5bb647df
...@@ -337,6 +337,7 @@ else: ...@@ -337,6 +337,7 @@ else:
# we assume the certfile contains both private key and certificate # we assume the certfile contains both private key and certificate
self.certfile = certfile self.certfile = certfile
self.active = False self.active = False
self.active_lock = threading.Lock()
self.allow_reuse_address = True self.allow_reuse_address = True
def get_request (self): def get_request (self):
...@@ -361,8 +362,15 @@ else: ...@@ -361,8 +362,15 @@ else:
# We want this to run in a thread, so we use a slightly # We want this to run in a thread, so we use a slightly
# modified version of "forever". # modified version of "forever".
self.active = True self.active = True
while self.active: while 1:
try: try:
# We need to lock while handling the request.
# Another thread can close the socket after self.active
# has been checked and before the request is handled.
# This causes an exception when using the closed socket.
with self.active_lock:
if not self.active:
break
self.handle_request() self.handle_request()
except socket.timeout: except socket.timeout:
pass pass
...@@ -370,12 +378,14 @@ else: ...@@ -370,12 +378,14 @@ else:
self.server_close() self.server_close()
return return
except: except:
sys.stdout.write(''.join(traceback.format_exception(*sys.exc_info()))); sys.stdout.write(''.join(traceback.format_exception(*sys.exc_info())))
break
def server_close(self): def server_close(self):
# Again, we want this to run in a thread, so we need to override # Again, we want this to run in a thread, so we need to override
# close to clear the "active" flag, so that serve_forever() will # close to clear the "active" flag, so that serve_forever() will
# terminate. # terminate.
with self.active_lock:
HTTPServer.server_close(self) HTTPServer.server_close(self)
self.active = False self.active = False
...@@ -664,7 +674,7 @@ else: ...@@ -664,7 +674,7 @@ else:
not in cert['subject']): not in cert['subject']):
raise test_support.TestFailed( raise test_support.TestFailed(
"Missing or invalid 'organizationName' field in certificate subject; " "Missing or invalid 'organizationName' field in certificate subject; "
"should be 'Python Software Foundation'."); "should be 'Python Software Foundation'.")
s.close() s.close()
finally: finally:
server.stop() server.stop()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment