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

Skip to content

Commit 739ae56

Browse files
committed
Issue #14548: Make multiprocessing finalizers check pid before running
This protects from possibilty of gc running just after fork.
1 parent 692130a commit 739ae56

2 files changed

Lines changed: 12 additions & 3 deletions

File tree

Lib/multiprocessing/util.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import sys
1111
import functools
12+
import os
1213
import itertools
1314
import weakref
1415
import atexit
@@ -161,6 +162,7 @@ def __init__(self, obj, callback, args=(), kwargs=None, exitpriority=None):
161162
self._args = args
162163
self._kwargs = kwargs or {}
163164
self._key = (exitpriority, next(_finalizer_counter))
165+
self._pid = os.getpid()
164166

165167
_finalizer_registry[self._key] = self
166168

@@ -177,9 +179,13 @@ def __call__(self, wr=None,
177179
except KeyError:
178180
sub_debug('finalizer no longer registered')
179181
else:
180-
sub_debug('finalizer calling %s with args %s and kwargs %s',
181-
self._callback, self._args, self._kwargs)
182-
res = self._callback(*self._args, **self._kwargs)
182+
if self._pid != os.getpid():
183+
sub_debug('finalizer ignored because different process')
184+
res = None
185+
else:
186+
sub_debug('finalizer calling %s with args %s and kwargs %s',
187+
self._callback, self._args, self._kwargs)
188+
res = self._callback(*self._args, **self._kwargs)
183189
self._weakref = self._callback = self._args = \
184190
self._kwargs = self._key = None
185191
return res

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ Core and Builtins
4444
Library
4545
-------
4646

47+
- Issue #14548: Make multiprocessing finalizers check pid before
48+
running to cope with possibility of gc running just after fork.
49+
4750
- Issue #14863: Update the documentation of os.fdopen() to reflect the
4851
fact that it's only a thin wrapper around open() anymore.
4952

0 commit comments

Comments
 (0)