Skip to content

Commit 38e29f9

Browse files
committed
Fix race condition in the test case (for real this time).
Apparently, the thread can start waiting at a point where the server isn't ready to receive messages, which caused it to block indefinitely for some reason. Before we create any threads or set any events, do a simple exchange with the server to ensure that it's in its message loop. This took way too damn long to figure out.
1 parent 6799b03 commit 38e29f9

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

Lib/test/test_ssl.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4628,11 +4628,11 @@ def test_thread_recv_while_main_thread_sends(self):
46284628
# if a thread started to infinitely block until data was received, calls
46294629
# to send() would deadlock, because it would wait forever on the lock
46304630
# that the recv() call held.
4631-
data = b"1" * 50
4631+
data = b"1" * 1024
46324632
event = threading.Event()
46334633
def background(sock):
46344634
event.set()
4635-
received = sock.recv(50)
4635+
received = sock.recv(len(data))
46364636
self.assertEqual(received, data)
46374637

46384638
client_context, server_context, hostname = testing_context()
@@ -4641,16 +4641,16 @@ def background(sock):
46414641
with client_context.wrap_socket(socket.socket(),
46424642
server_hostname=hostname) as sock:
46434643
sock.connect((HOST, server.port))
4644-
sock.settimeout(5)
4644+
sock.settimeout(1)
4645+
# Ensure that the server is ready to accept requests
4646+
sock.sendall(b"123")
4647+
self.assertEqual(sock.recv(3), b"123")
46454648
with threading_helper.catch_threading_exception() as cm:
46464649
thread = threading.Thread(target=background,
46474650
args=(sock,), daemon=True)
46484651
thread.start()
46494652
event.wait()
4650-
# We need to yield the GIL to prevent some silly race
4651-
# condition in ThreadedEchoServer
4652-
time.sleep(0)
4653-
sock.sendall(b"1" * 50)
4653+
sock.sendall(data)
46544654
thread.join()
46554655
if cm.exc_value is not None:
46564656
raise cm.exc_value

0 commit comments

Comments
 (0)