summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRoss Burton <ross.burton@arm.com>2022-08-25 01:37:18 (GMT)
committerGitHub <noreply@github.com>2022-08-25 01:37:18 (GMT)
commitdf110126971d0271a977ce10779083b3e335b4da (patch)
tree2cb51687e1131c7621aa6b190c0b9c8bcde006ab
parente34c82abeb7ace09e6b5d116585c47cc372996c1 (diff)
downloadcpython-df110126971d0271a977ce10779083b3e335b4da.zip
cpython-df110126971d0271a977ce10779083b3e335b4da.tar.gz
cpython-df110126971d0271a977ce10779083b3e335b4da.tar.bz2
gh-95243: Mitigate the race condition in testSockName (#96173)
find_unused_port() has an inherent race condition, but we can't use bind_port() as that uses .getsockname() which this test is exercising. Try binding to unused ports a few times before failing. Signed-off-by: Ross Burton <ross.burton@arm.com>
-rw-r--r--Lib/test/test_socket.py15
-rw-r--r--Misc/NEWS.d/next/Tests/2022-08-22-14-59-42.gh-issue-95243.DeD66V.rst3
2 files changed, 16 insertions, 2 deletions
diff --git a/Lib/test/test_socket.py b/Lib/test/test_socket.py
index f1b57fc..d808f3f 100644
--- a/Lib/test/test_socket.py
+++ b/Lib/test/test_socket.py
@@ -1398,10 +1398,21 @@ class GeneralModuleTests(unittest.TestCase):
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 0000000..a9ca1f8
--- /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.