]> granicus.if.org Git - python/commitdiff
Issue #14548: Make multiprocessing finalizers check pid before
authorRichard Oudkerk <roudkerk@google.com>
Thu, 23 Jan 2014 00:11:04 +0000 (00:11 +0000)
committerRichard Oudkerk <roudkerk@google.com>
Thu, 23 Jan 2014 00:11:04 +0000 (00:11 +0000)
running to cope with possibility of gc running just after fork.
(Backport from 3.x.)

Lib/multiprocessing/util.py
Misc/NEWS

index d1b3d2e3e87f7c650898f25211942f30533607c2..092b61ce0902245c2a08ee351d16b612f80cab08 100644 (file)
@@ -32,6 +32,7 @@
 # SUCH DAMAGE.
 #
 
+import os
 import itertools
 import weakref
 import atexit
@@ -184,6 +185,7 @@ class Finalize(object):
         self._args = args
         self._kwargs = kwargs or {}
         self._key = (exitpriority, _finalizer_counter.next())
+        self._pid = os.getpid()
 
         _finalizer_registry[self._key] = self
 
@@ -196,9 +198,13 @@ class Finalize(object):
         except KeyError:
             sub_debug('finalizer no longer registered')
         else:
-            sub_debug('finalizer calling %s with args %s and kwargs %s',
-                     self._callback, self._args, self._kwargs)
-            res = self._callback(*self._args, **self._kwargs)
+            if self._pid != os.getpid():
+                sub_debug('finalizer ignored because different process')
+                res = None
+            else:
+                sub_debug('finalizer calling %s with args %s and kwargs %s',
+                          self._callback, self._args, self._kwargs)
+                res = self._callback(*self._args, **self._kwargs)
             self._weakref = self._callback = self._args = \
                             self._kwargs = self._key = None
             return res
index 35846b5a0a80818157357bcb67cf92167b9e88d9..54aac520c2f18ebc8c1da69f8df753eedfbf6f94 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -38,6 +38,10 @@ Core and Builtins
 Library
 -------
 
+- Issue #14548: Make multiprocessing finalizers check pid before
+  running to cope with possibility of gc running just after fork.
+  (Backport from 3.x.)
+
 - Issue #20262: Warnings are raised now when duplicate names are added in the
   ZIP file or too long ZIP file comment is truncated.