Charles-François Natali <neolo...@free.fr> added the comment:

> Well it would probably be closed when the connection object is
> destroyed, but the patch looks ok anyway.

Let's be nice with those non refcount-based implementations out there :-)

What do you think of the patch attached
(connection_multiple_bind-1.diff) for the original problem?
It does two things:
1. add the FILE_FLAG_FIRST_PIPE_INSTANCE when creating a pipe on Windows
2. remove SO_REUSEADDR altogether on Windows, since it doesn't seem to
be required

For 2, it should be correct if I understand Microsoft's documentation
correctly, but then I wonder why test.support goes through the pain of
using SO_REUSEADDR + SO_EXCLUSIVEADDRUSE instead of just dropping
SO_REUSEADDR. Do you happen to know a Windows guru (or maybe should I
ask on python-dev?).

----------
Added file: http://bugs.python.org/file24416/connection_multiple_bind-1.diff

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue8184>
_______________________________________
diff --git a/Lib/multiprocessing/connection.py 
b/Lib/multiprocessing/connection.py
--- a/Lib/multiprocessing/connection.py
+++ b/Lib/multiprocessing/connection.py
@@ -544,7 +544,8 @@
             obsize, ibsize = 0, BUFSIZE
 
         h1 = win32.CreateNamedPipe(
-            address, openmode | win32.FILE_FLAG_OVERLAPPED,
+            address, openmode | win32.FILE_FLAG_OVERLAPPED |
+            win32.FILE_FLAG_FIRST_PIPE_INSTANCE,
             win32.PIPE_TYPE_MESSAGE | win32.PIPE_READMODE_MESSAGE |
             win32.PIPE_WAIT,
             1, obsize, ibsize, win32.NMPWAIT_WAIT_FOREVER, win32.NULL
@@ -576,7 +577,10 @@
     def __init__(self, address, family, backlog=1):
         self._socket = socket.socket(getattr(socket, family))
         try:
-            self._socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
+            # SO_REUSEADDR has different semantics on Windows (issue #8184). 
+            if os.name == 'posix':
+                self._socket.setsockopt(socket.SOL_SOCKET,
+                                        socket.SO_REUSEADDR, 1)
             self._socket.bind(address)
             self._socket.listen(backlog)
             self._address = self._socket.getsockname()
@@ -630,7 +634,8 @@
         def __init__(self, address, backlog=None):
             self._address = address
             handle = win32.CreateNamedPipe(
-                address, win32.PIPE_ACCESS_DUPLEX,
+                address, win32.PIPE_ACCESS_DUPLEX |
+                win32.FILE_FLAG_FIRST_PIPE_INSTANCE,
                 win32.PIPE_TYPE_MESSAGE | win32.PIPE_READMODE_MESSAGE |
                 win32.PIPE_WAIT,
                 win32.PIPE_UNLIMITED_INSTANCES, BUFSIZE, BUFSIZE,
diff --git a/Lib/test/test_multiprocessing.py b/Lib/test/test_multiprocessing.py
--- a/Lib/test/test_multiprocessing.py
+++ b/Lib/test/test_multiprocessing.py
@@ -1738,6 +1738,17 @@
         self.assertRaises(RuntimeError, reduction.recv_handle, conn)
         p.join()
 
+class _TestListener(BaseTestCase):
+
+    ALLOWED_TYPES = ('processes')
+
+    def test_multiple_bind(self):
+        for family in self.connection.families:
+            l = self.connection.Listener(family=family)
+            self.addCleanup(l.close)
+            self.assertRaises(OSError, self.connection.Listener,
+                              l.address, family)
+
 class _TestListenerClient(BaseTestCase):
 
     ALLOWED_TYPES = ('processes', 'threads')
@@ -1758,6 +1769,7 @@
             self.assertEqual(conn.recv(), 'hello')
             p.join()
             l.close()
+
 #
 # Test of sending connection and socket objects between processes
 #
diff --git a/Modules/_multiprocessing/win32_functions.c 
b/Modules/_multiprocessing/win32_functions.c
--- a/Modules/_multiprocessing/win32_functions.c
+++ b/Modules/_multiprocessing/win32_functions.c
@@ -771,6 +771,7 @@
     WIN32_CONSTANT(F_DWORD, ERROR_PIPE_CONNECTED);
     WIN32_CONSTANT(F_DWORD, ERROR_SEM_TIMEOUT);
     WIN32_CONSTANT(F_DWORD, FILE_FLAG_OVERLAPPED);
+    WIN32_CONSTANT(F_DWORD, FILE_FLAG_FIRST_PIPE_INSTANCE);
     WIN32_CONSTANT(F_DWORD, GENERIC_READ);
     WIN32_CONSTANT(F_DWORD, GENERIC_WRITE);
     WIN32_CONSTANT(F_DWORD, INFINITE);
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to