Skip to content

Commit 840b2a8

Browse files
encukoupicnixz
authored andcommitted
pythongh-126500: test_ssl: Don't stop ThreadedEchoServer on OSError in ConnectionHandler; rely on __exit__ (pythonGH-126503)
If `read()` in the ConnectionHandler thread raises `OSError` (except `ConnectionError`), the ConnectionHandler shuts down the entire ThreadedEchoServer, preventing further connections. It also does that for `EPROTOTYPE` in `wrap_conn`. As far as I can see, this is done to avoid the server thread getting stuck, forgotten, in its accept loop. However, since 2011 (5b95eb9) the server is used as a context manager, and its `__exit__` does `stop()` and `join()`. (I'm not sure if we *always* used `with` since that commit, but currently we do.) Make sure that the context manager *is* used, and remove the `server.stop()` calls from ConnectionHandler.
1 parent 53c3f88 commit 840b2a8

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

Lib/test/test_ssl.py

+12-5
Original file line numberDiff line numberDiff line change
@@ -2299,7 +2299,6 @@ def wrap_conn(self):
22992299
# See also http://erickt.github.io/blog/2014/11/19/adventures-in-debugging-a-potential-osx-kernel-bug/
23002300
if e.errno != errno.EPROTOTYPE and sys.platform != "darwin":
23012301
self.running = False
2302-
self.server.stop()
23032302
self.close()
23042303
return False
23052304
else:
@@ -2436,10 +2435,6 @@ def run(self):
24362435
self.close()
24372436
self.running = False
24382437

2439-
# normally, we'd just stop here, but for the test
2440-
# harness, we want to stop the server
2441-
self.server.stop()
2442-
24432438
def __init__(self, certificate=None, ssl_version=None,
24442439
certreqs=None, cacerts=None,
24452440
chatty=True, connectionchatty=False, starttls_server=False,
@@ -2473,21 +2468,33 @@ def __init__(self, certificate=None, ssl_version=None,
24732468
self.conn_errors = []
24742469
threading.Thread.__init__(self)
24752470
self.daemon = True
2471+
self._in_context = False
24762472

24772473
def __enter__(self):
2474+
if self._in_context:
2475+
raise ValueError('Re-entering ThreadedEchoServer context')
2476+
self._in_context = True
24782477
self.start(threading.Event())
24792478
self.flag.wait()
24802479
return self
24812480

24822481
def __exit__(self, *args):
2482+
assert self._in_context
2483+
self._in_context = False
24832484
self.stop()
24842485
self.join()
24852486

24862487
def start(self, flag=None):
2488+
if not self._in_context:
2489+
raise ValueError(
2490+
'ThreadedEchoServer must be used as a context manager')
24872491
self.flag = flag
24882492
threading.Thread.start(self)
24892493

24902494
def run(self):
2495+
if not self._in_context:
2496+
raise ValueError(
2497+
'ThreadedEchoServer must be used as a context manager')
24912498
self.sock.settimeout(1.0)
24922499
self.sock.listen(5)
24932500
self.active = True

0 commit comments

Comments
 (0)