]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-122187: Avoid TSan reported race in `run_udp_echo_server` (#122189)
authorSam Gross <colesbury@gmail.com>
Thu, 25 Jul 2024 08:16:53 +0000 (04:16 -0400)
committerGitHub <noreply@github.com>
Thu, 25 Jul 2024 08:16:53 +0000 (13:46 +0530)
TSan doesn't fully recognize the synchronization via I/O, so ensure that
socket name is retrieved earlier and use a different socket for sending
the "STOP" message.

Lib/test/test_asyncio/utils.py

index dbb8d27c1769502e16566537fe1ee65978a44560..35893ab3118e1ec6b6a310566206fe19f534723c 100644 (file)
@@ -301,12 +301,17 @@ def run_udp_echo_server(*, host='127.0.0.1', port=0):
     family, type, proto, _, sockaddr = addr_info[0]
     sock = socket.socket(family, type, proto)
     sock.bind((host, port))
+    sockname = sock.getsockname()
     thread = threading.Thread(target=lambda: echo_datagrams(sock))
     thread.start()
     try:
-        yield sock.getsockname()
+        yield sockname
     finally:
-        sock.sendto(b'STOP', sock.getsockname())
+        # gh-122187: use a separate socket to send the stop message to avoid
+        # TSan reported race on the same socket.
+        sock2 = socket.socket(family, type, proto)
+        sock2.sendto(b'STOP', sockname)
+        sock2.close()
         thread.join()