8000 [3.12] gh-126500: test_ssl: Don't stop ThreadedEchoServer on OSError … · python/cpython@fa8024b · GitHub
[go: up one dir, main page]

Skip to content

Commit fa8024b

Browse files
[3.12] gh-126500: test_ssl: Don't stop ThreadedEchoServer on OSError in ConnectionHandler; rely on __exit__ (GH-126503) (GH-126572)
gh-126500: test_ssl: Don't stop ThreadedEchoServer on OSError in ConnectionHandler; rely on __exit__ (GH-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. (cherry picked from commit c9cda16) (cherry picked from commit aee80cd) Co-authored-by: Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> Co-authored-by: Petr Viktorin <encukou@gmail.com>
1 parent 6322edd commit fa8024b

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

Lib/test/test_ssl.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2488,7 +2488,6 @@ def wrap_conn(self):
24882488
# See also http://erickt.github.io/blog/2014/11/19/adventures-in-debugging-a-potential-osx-kernel-bug/
24892489
if e.errno != errno.EPROTOTYPE and sys.platform != "darwin":
24902490
self.running = False
2491-
self.server.stop()
24922491
self.close()
24932492
return False
24942493
else:
@@ -2623,10 +2622,6 @@ def run(self):
26232622
self.close()
26242623
self.running = False
26252624

2626-
# normally, we'd just stop here, but for the test
2627-
# harness, we want to stop the server
2628-
self.server.stop()
2629-
26302625
def __init__(self, certificate=None, ssl_version=None,
26312626
certreqs=None, cacerts=None,
26322627
chatty=True, connectionchatty=False, starttls_server=False,
@@ -2660,21 +2655,33 @@ def __init__(self, certificate=None, ssl_version=None,
26602655
self.conn_errors = []
26612656
threading.Thread.__init__(self)
26622657
self.daemon = True
2658+
self._in_context = False
26632659

26642660
def __enter__(self):
2661+
if self._in_context:
2662+
raise ValueError('Re-entering ThreadedEchoServer context')
2663+
self._in_context = True
26652664
self.start(threading.Event())
26662665
self.flag.wait()
26672666
return self
26682667

26692668
def __exit__(self, *args):
2669+
assert self._in_context
2670+
self._in_context = False
26702671
self.stop()
26712672
self.join()
26722673

26732674
def start(self, flag=None):
2675+
if not self._in_context:
2676+
raise ValueError(
2677+
'ThreadedEchoServer must be used as a context manager')
26742678
self.flag = flag
26752679
threading.Thread.start(self)
26762680

26772681
def run(self):
2682+
if not self._in_context:
2683+
raise ValueError(
2684+
'ThreadedEchoServer must be used as a context manager')
26782685
self.sock.settimeout(1.0)
26792686
self.sock.listen(5)
26802687
self.active = True

0 commit comments

Comments
 (0)
0