[2.7] bpo-24484: Avoid race condition in multiprocessing cleanup (GH-2159) (#2168)
* bpo-24484: Avoid race condition in multiprocessing cleanup
The finalizer registry can be mutated while inspected by multiprocessing
at process exit.
* Use test.support.start_threads()
* Add Misc/NEWS.
(cherry picked from commit 1eb6c0074d17f4fd425cacfdda893d65f5f77f0a)
diff --git a/Lib/multiprocessing/util.py b/Lib/multiprocessing/util.py
index 394cc44..2920f24 100644
--- a/Lib/multiprocessing/util.py
+++ b/Lib/multiprocessing/util.py
@@ -265,6 +265,9 @@
else:
f = lambda p : p[0][0] is not None and p[0][0] >= minpriority
+ # Careful: _finalizer_registry may be mutated while this function
+ # is running (either by a GC run or by another thread).
+
items = [x for x in _finalizer_registry.items() if f(x)]
items.sort(reverse=True)