[3.13] gh-122187: Avoid TSan reported race in run_udp_echo_server (GH-122189) (#122263)

gh-122187: Avoid TSan reported race in `run_udp_echo_server` (GH-122189)

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.
(cherry picked from commit 2f74b709b6)

Co-authored-by: Sam Gross <colesbury@gmail.com>
This commit is contained in:
Miss Islington (bot) 2024-07-25 10:42:38 +02:00 committed by GitHub
parent 6933c4ace9
commit 977c799286
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -301,12 +301,17 @@ def run_udp_echo_server(*, host='127.0.0.1', port=0):
family, type, proto, _, sockaddr = addr_info[0] family, type, proto, _, sockaddr = addr_info[0]
sock = socket.socket(family, type, proto) sock = socket.socket(family, type, proto)
sock.bind((host, port)) sock.bind((host, port))
sockname = sock.getsockname()
thread = threading.Thread(target=lambda: echo_datagrams(sock)) thread = threading.Thread(target=lambda: echo_datagrams(sock))
thread.start() thread.start()
try: try:
yield sock.getsockname() yield sockname
finally: 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() thread.join()