From 9287bffd6996609ec462b819b0d7ddc4ed2f6a43 Mon Sep 17 00:00:00 2001 From: Antoine Pitrou Date: Sun, 28 May 2017 11:49:42 +0200 Subject: [PATCH 1/2] bpo-16500: Use register_at_fork() in the threading module --- Lib/threading.py | 6 ++++++ Python/ceval.c | 18 ------------------ 2 files changed, 6 insertions(+), 18 deletions(-) diff --git a/Lib/threading.py b/Lib/threading.py index 929a4b006669c54..df8a66ee16d9fe7 100644 --- a/Lib/threading.py +++ b/Lib/threading.py @@ -1,5 +1,6 @@ """Thread module emulating a subset of Java's threading model.""" +import os as _os import sys as _sys import _thread @@ -943,6 +944,7 @@ def _bootstrap_inner(self): exc_tb.tb_frame.f_code.co_name)), file=self._stderr) exc_tb = exc_tb.tb_next print(("%s: %s" % (exc_type, exc_value)), file=self._stderr) + self._stderr.flush() # Make sure that exc_tb gets deleted since it is a memory # hog; deleting everything else is just for thoroughness finally: @@ -1356,3 +1358,7 @@ def _after_fork(): _active.clear() _active.update(new_active) assert len(_active) == 1 + + +if hasattr(_os, "fork"): + _os.register_at_fork(_after_fork, when="child") diff --git a/Python/ceval.c b/Python/ceval.c index 302070bbda7a05b..fa5ef8c94382d0b 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -240,8 +240,6 @@ PyEval_ReleaseThread(PyThreadState *tstate) void PyEval_ReInitThreads(void) { - _Py_IDENTIFIER(_after_fork); - PyObject *threading, *result; PyThreadState *current_tstate = PyThreadState_GET(); if (!gil_created()) @@ -251,22 +249,6 @@ PyEval_ReInitThreads(void) take_gil(current_tstate); main_thread = PyThread_get_thread_ident(); - /* Update the threading module with the new state. - */ - threading = PyMapping_GetItemString(current_tstate->interp->modules, - "threading"); - if (threading == NULL) { - /* threading not imported */ - PyErr_Clear(); - return; - } - result = _PyObject_CallMethodId(threading, &PyId__after_fork, NULL); - if (result == NULL) - PyErr_WriteUnraisable(threading); - else - Py_DECREF(result); - Py_DECREF(threading); - /* Destroy all threads except the current one */ _PyThreadState_DeleteExcept(current_tstate); } From 463fc93245d4347ceb4a365b7f8ba48fb34cacc0 Mon Sep 17 00:00:00 2001 From: Antoine Pitrou Date: Sun, 28 May 2017 13:18:36 +0200 Subject: [PATCH 2/2] Update comment at top of _after_fork() --- Lib/threading.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/Lib/threading.py b/Lib/threading.py index c84a425fd037bfd..2eaf49a611f658b 100644 --- a/Lib/threading.py +++ b/Lib/threading.py @@ -1321,10 +1321,9 @@ def main_thread(): def _after_fork(): - # This function is called by Python/ceval.c:PyEval_ReInitThreads which - # is called from PyOS_AfterFork_Child. Here we cleanup threading module - # state that should not exist after a fork. - + """ + Cleanup threading module state that should not exist after a fork. + """ # Reset _active_limbo_lock, in case we forked while the lock was held # by another (non-forked) thread. http://bugs.python.org/issue874900 global _active_limbo_lock, _main_thread