Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ed4a8fc

Browse files
author
Charles-François Natali
committed
Issue #8184: multiprocessing: On Windows, don't set SO_REUSEADDR on Connection
sockets, and set FILE_FLAG_FIRST_PIPE_INSTANCE on named pipes, to make sure two listeners can't bind to the same socket/pipe (or any existing socket/pipe).
1 parent 1aa54a4 commit ed4a8fc

4 files changed

Lines changed: 26 additions & 3 deletions

File tree

Lib/multiprocessing/connection.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,8 @@ def Pipe(duplex=True):
544544
obsize, ibsize = 0, BUFSIZE
545545

546546
h1 = win32.CreateNamedPipe(
547-
address, openmode | win32.FILE_FLAG_OVERLAPPED,
547+
address, openmode | win32.FILE_FLAG_OVERLAPPED |
548+
win32.FILE_FLAG_FIRST_PIPE_INSTANCE,
548549
win32.PIPE_TYPE_MESSAGE | win32.PIPE_READMODE_MESSAGE |
549550
win32.PIPE_WAIT,
550551
1, obsize, ibsize, win32.NMPWAIT_WAIT_FOREVER, win32.NULL
@@ -576,7 +577,10 @@ class SocketListener(object):
576577
def __init__(self, address, family, backlog=1):
577578
self._socket = socket.socket(getattr(socket, family))
578579
try:
579-
self._socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
580+
# SO_REUSEADDR has different semantics on Windows (issue #2550).
581+
if os.name == 'posix':
582+
self._socket.setsockopt(socket.SOL_SOCKET,
583+
socket.SO_REUSEADDR, 1)
580584
self._socket.bind(address)
581585
self._socket.listen(backlog)
582586
self._address = self._socket.getsockname()
@@ -630,7 +634,8 @@ class PipeListener(object):
630634
def __init__(self, address, backlog=None):
631635
self._address = address
632636
handle = win32.CreateNamedPipe(
633-
address, win32.PIPE_ACCESS_DUPLEX,
637+
address, win32.PIPE_ACCESS_DUPLEX |
638+
win32.FILE_FLAG_FIRST_PIPE_INSTANCE,
634639
win32.PIPE_TYPE_MESSAGE | win32.PIPE_READMODE_MESSAGE |
635640
win32.PIPE_WAIT,
636641
win32.PIPE_UNLIMITED_INSTANCES, BUFSIZE, BUFSIZE,

Lib/test/test_multiprocessing.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1779,6 +1779,17 @@ def test_missing_fd_transfer(self):
17791779
self.assertRaises(RuntimeError, reduction.recv_handle, conn)
17801780
p.join()
17811781

1782+
class _TestListener(BaseTestCase):
1783+
1784+
ALLOWED_TYPES = ('processes')
1785+
1786+
def test_multiple_bind(self):
1787+
for family in self.connection.families:
1788+
l = self.connection.Listener(family=family)
1789+
self.addCleanup(l.close)
1790+
self.assertRaises(OSError, self.connection.Listener,
1791+
l.address, family)
1792+
17821793
class _TestListenerClient(BaseTestCase):
17831794

17841795
ALLOWED_TYPES = ('processes', 'threads')
@@ -1799,6 +1810,7 @@ def test_listener_client(self):
17991810
self.assertEqual(conn.recv(), 'hello')
18001811
p.join()
18011812
l.close()
1813+
18021814
#
18031815
# Test of sending connection and socket objects between processes
18041816
#

Misc/NEWS

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,11 @@ Library
472472

473473
- Issue #13846: Add time.monotonic(), monotonic clock.
474474

475+
- Issue #8184: multiprocessing: On Windows, don't set SO_REUSEADDR on
476+
Connection sockets, and set FILE_FLAG_FIRST_PIPE_INSTANCE on named pipes, to
477+
make sure two listeners can't bind to the same socket/pipe (or any existing
478+
socket/pipe).
479+
475480
- Issue #10811: Fix recursive usage of cursors. Instead of crashing,
476481
raise a ProgrammingError now.
477482

Modules/_multiprocessing/win32_functions.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -784,6 +784,7 @@ create_win32_namespace(void)
784784
WIN32_CONSTANT(F_DWORD, ERROR_PIPE_BUSY);
785785
WIN32_CONSTANT(F_DWORD, ERROR_PIPE_CONNECTED);
786786
WIN32_CONSTANT(F_DWORD, ERROR_SEM_TIMEOUT);
787+
WIN32_CONSTANT(F_DWORD, FILE_FLAG_FIRST_PIPE_INSTANCE);
787788
WIN32_CONSTANT(F_DWORD, FILE_FLAG_OVERLAPPED);
788789
WIN32_CONSTANT(F_DWORD, GENERIC_READ);
789790
WIN32_CONSTANT(F_DWORD, GENERIC_WRITE);

0 commit comments

Comments
 (0)