Charles-François Natali added the comment:

Here's a patch.

----------
keywords: +patch
Added file: http://bugs.python.org/file36070/find_unused_race.diff

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue19875>
_______________________________________
diff -r f7643c893587 Lib/test/test_socket.py
--- a/Lib/test/test_socket.py   Wed Jul 23 19:28:13 2014 +0100
+++ b/Lib/test/test_socket.py   Thu Jul 24 19:43:26 2014 +0100
@@ -3,6 +3,7 @@
 
 import errno
 import io
+import itertools
 import socket
 import select
 import tempfile
@@ -1147,17 +1148,24 @@
         sock.close()
 
     def test_getsockaddrarg(self):
-        host = '0.0.0.0'
+        sock = socket.socket()
+        self.addCleanup(sock.close)
         port = support.find_unused_port()
         big_port = port + 65536
         neg_port = port - 65536
-        sock = socket.socket()
-        try:
-            self.assertRaises(OverflowError, sock.bind, (host, big_port))
-            self.assertRaises(OverflowError, sock.bind, (host, neg_port))
-            sock.bind((host, port))
-        finally:
-            sock.close()
+        self.assertRaises(OverflowError, sock.bind, (HOST, big_port))
+        self.assertRaises(OverflowError, sock.bind, (HOST, neg_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 = support.find_unused_port()
+            try:
+                sock.bind((HOST, port))
+            except OSError as e:
+                if e.errno != errno.EADDRINUSE or i == 5:
+                    raise
+            else:
+                break
 
     @unittest.skipUnless(os.name == "nt", "Windows specific")
     def test_sock_ioctl(self):
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to