Skip to content

Commit 448a7d8

Browse files
committed
[3.7] [3.8] bpo-39850: Add support for abstract sockets in multiprocessing (pythonGH-18866)
(cherry picked from commit 6012f30) Co-authored-by: Pablo Galindo <[email protected]>. (cherry picked from commit 5e217bb) Co-authored-by: Pablo Galindo <[email protected]>
1 parent 80be9c3 commit 448a7d8

File tree

5 files changed

+43
-3
lines changed

5 files changed

+43
-3
lines changed

Lib/multiprocessing/connection.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ def address_type(address):
102102
return 'AF_INET'
103103
elif type(address) is str and address.startswith('\\\\'):
104104
return 'AF_PIPE'
105-
elif type(address) is str:
105+
elif type(address) is str or util.is_abstract_socket_namespace(address):
106106
return 'AF_UNIX'
107107
else:
108108
raise ValueError('address type of %r unrecognized' % address)
@@ -587,7 +587,8 @@ def __init__(self, address, family, backlog=1):
587587
self._family = family
588588
self._last_accepted = None
589589

590-
if family == 'AF_UNIX':
590+
if family == 'AF_UNIX' and not util.is_abstract_socket_namespace(address):
591+
# Linux abstract socket namespaces do not need to be explicitly unlinked
591592
self._unlink = util.Finalize(
592593
self, os.unlink, args=(address,), exitpriority=0
593594
)

Lib/multiprocessing/forkserver.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,8 @@ def ensure_running(self):
116116
with socket.socket(socket.AF_UNIX) as listener:
117117
address = connection.arbitrary_address('AF_UNIX')
118118
listener.bind(address)
119-
os.chmod(address, 0o600)
119+
if not util.is_abstract_socket_namespace(address):
120+
os.chmod(address, 0o600)
120121
listener.listen()
121122

122123
# all client processes own the write end of the "alive" pipe;

Lib/multiprocessing/util.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,29 @@ def log_to_stderr(level=None):
102102
_log_to_stderr = True
103103
return _logger
104104

105+
106+
# Abstract socket support
107+
108+
def _platform_supports_abstract_sockets():
109+
if sys.platform == "linux":
110+
return True
111+
if hasattr(sys, 'getandroidapilevel'):
112+
return True
113+
return False
114+
115+
116+
def is_abstract_socket_namespace(address):
117+
if not address:
118+
return False
119+
if isinstance(address, bytes):
120+
return address[0] == 0
121+
elif isinstance(address, str):
122+
return address[0] == "\0"
123+
raise TypeError('address type of {address!r} unrecognized')
124+
125+
126+
abstract_sockets_supported = _platform_supports_abstract_sockets()
127+
105128
#
106129
# Function returning a temp directory which will be removed on exit
107130
#

Lib/test/_test_multiprocessing.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3140,6 +3140,19 @@ def test_context(self):
31403140
if self.TYPE == 'processes':
31413141
self.assertRaises(OSError, l.accept)
31423142

3143+
@unittest.skipUnless(util.abstract_sockets_supported,
3144+
"test needs abstract socket support")
3145+
def test_abstract_socket(self):
3146+
with self.connection.Listener("\0something") as listener:
3147+
with self.connection.Client(listener.address) as client:
3148+
with listener.accept() as d:
3149+
client.send(1729)
3150+
self.assertEqual(d.recv(), 1729)
3151+
3152+
if self.TYPE == 'processes':
3153+
self.assertRaises(OSError, listener.accept)
3154+
3155+
31433156
class _TestListenerClient(BaseTestCase):
31443157

31453158
ALLOWED_TYPES = ('processes', 'threads')
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
:mod:`multiprocessing` now supports abstract socket addresses (if abstract sockets
2+
are supported in the running platform). Patch by Pablo Galindo.

0 commit comments

Comments
 (0)