diff --git a/Lib/test/test_socket.py b/Lib/test/test_socket.py
index 9c5f6d3dc9a6d1568533ef91b55d8ab0a19f4b5e..e0e0f2437bb07a0b20ce67c80812688b2bcb74b2 100644
--- a/Lib/test/test_socket.py
+++ b/Lib/test/test_socket.py
@@ -1374,10 +1374,21 @@ def testStringToIPv6(self):
 
     def testSockName(self):
         # Testing getsockname()
-        port = socket_helper.find_unused_port()
         sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
         self.addCleanup(sock.close)
-        sock.bind(("0.0.0.0", port))
+
+        # Since find_unused_port() is inherently subject to race conditions, we
+        # call it a couple times if necessary.
+        for i in itertools.count():
+            port = socket_helper.find_unused_port()
+            try:
+                sock.bind(("0.0.0.0", port))
+            except OSError as e:
+                if e.errno != errno.EADDRINUSE or i == 5:
+                    raise
+            else:
+                break
+
         name = sock.getsockname()
         # XXX(nnorwitz): http://tinyurl.com/os5jz seems to indicate
         # it reasonable to get the host's addr in addition to 0.0.0.0.
diff --git a/Misc/NEWS.d/next/Tests/2022-08-22-14-59-42.gh-issue-95243.DeD66V.rst b/Misc/NEWS.d/next/Tests/2022-08-22-14-59-42.gh-issue-95243.DeD66V.rst
new file mode 100644
index 0000000000000000000000000000000000000000..a9ca1f8b86764c2cd3bf231f3f4b58c40474832a
--- /dev/null
+++ b/Misc/NEWS.d/next/Tests/2022-08-22-14-59-42.gh-issue-95243.DeD66V.rst
@@ -0,0 +1,3 @@
+Mitigate the inherent race condition from using find_unused_port() in
+testSockName() by trying to find an unused port a few times before failing.
+Patch by Ross Burton.