Skip to content

Commit 913e868

Browse files
committed
Fix mutable default argument in multiprocessing.dummy.DummyProcess
Change the default value of the 'kwargs' parameter in DummyProcess.__init__() from {} to None to avoid mutable default argument issues. This makes DummyProcess consistent with threading.Thread and threading.Timer which already use this pattern. Found by Linux Verification Center (linuxtesting.org) with SVACE. Reported by: Dmitrii Chuprov <[email protected]> Signed-off-by: Denis Sergeev <[email protected]>
1 parent 4afa985 commit 913e868

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

Lib/multiprocessing/dummy/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@
3333

3434
class DummyProcess(threading.Thread):
3535

36-
def __init__(self, group=None, target=None, name=None, args=(), kwargs={}):
36+
def __init__(self, group=None, target=None, name=None, args=(), kwargs=None):
37+
if kwargs is None:
38+
kwargs = {}
3739
threading.Thread.__init__(self, group, target, name, args, kwargs)
3840
self._pid = None
3941
self._children = weakref.WeakKeyDictionary()

Lib/test/_test_multiprocessing.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7347,3 +7347,15 @@ def test_forkpty(self):
73477347
res = assert_python_failure("-c", code, PYTHONWARNINGS='error')
73487348
self.assertIn(b'DeprecationWarning', res.err)
73497349
self.assertIn(b'is multi-threaded, use of forkpty() may lead to deadlocks in the child', res.err)
7350+
7351+
class _TestDummyProcessKwargs(BaseTestCase):
7352+
ALLOWED_TYPES = ('threads',)
7353+
7354+
def test_dummyprocess_kwargs_default_not_shared(self):
7355+
p1 = self.Process()
7356+
p2 = self.Process()
7357+
self.assertIsInstance(p1._kwargs, dict)
7358+
self.assertIsInstance(p2._kwargs, dict)
7359+
self.assertIsNot(p1._kwargs, p2._kwargs)
7360+
p1._kwargs['sentinel'] = 42
7361+
self.assertNotIn('sentinel', p2._kwargs)

0 commit comments

Comments
 (0)