Skip to content

Commit 0388a23

Browse files
committed
Fix multiprocessing Listener authkey bug
1 parent db68544 commit 0388a23

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

Lib/multiprocessing/connection.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -465,8 +465,9 @@ def accept(self):
465465
'''
466466
if self._listener is None:
467467
raise OSError('listener is closed')
468+
468469
c = self._listener.accept()
469-
if self._authkey:
470+
if self._authkey is not None:
470471
deliver_challenge(c, self._authkey)
471472
answer_challenge(c, self._authkey)
472473
return c

Lib/test/_test_multiprocessing.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3300,6 +3300,32 @@ def test_context(self):
33003300
if self.TYPE == 'processes':
33013301
self.assertRaises(OSError, l.accept)
33023302

3303+
def test_empty_authkey(self):
3304+
# bpo-43952: allow empty bytes as authkey
3305+
def handler(*args):
3306+
raise RuntimeError('Connection took too long...')
3307+
3308+
def run(addr, authkey):
3309+
client = self.connection.Client(addr, authkey=authkey)
3310+
client.send(1729)
3311+
3312+
timeout_in_s = 2
3313+
key = b""
3314+
3315+
try:
3316+
# wait 2s so the test won't hang forever in case of regression
3317+
signal.signal(signal.SIGALRM, handler)
3318+
signal.alarm(timeout_in_s)
3319+
with self.connection.Listener(authkey=key) as listener:
3320+
threading.Thread(target=run, args=(listener.address, key)).start()
3321+
with listener.accept() as d:
3322+
self.assertEqual(d.recv(), 1729)
3323+
finally:
3324+
signal.alarm(0)
3325+
3326+
if self.TYPE == 'processes':
3327+
self.assertRaises(OSError, listener.accept)
3328+
33033329
@unittest.skipUnless(util.abstract_sockets_supported,
33043330
"test needs abstract socket support")
33053331
def test_abstract_socket(self):
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix :meth:`multiprocessing.connection.Listener.accept()` to accept empty bytes
2+
as authkey. Not accepting empty bytes as key causes it to hang indefinitely.

0 commit comments

Comments
 (0)