mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
bpo-16500: Use register_at_fork() in the threading module (#1843)
* bpo-16500: Use register_at_fork() in the threading module * Update comment at top of _after_fork()
This commit is contained in:
parent
0aa0a06e8b
commit
4a8bcdf79c
2 changed files with 9 additions and 22 deletions
|
@ -1,5 +1,6 @@
|
||||||
"""Thread module emulating a subset of Java's threading model."""
|
"""Thread module emulating a subset of Java's threading model."""
|
||||||
|
|
||||||
|
import os as _os
|
||||||
import sys as _sys
|
import sys as _sys
|
||||||
import _thread
|
import _thread
|
||||||
|
|
||||||
|
@ -943,6 +944,7 @@ class Thread:
|
||||||
exc_tb.tb_frame.f_code.co_name)), file=self._stderr)
|
exc_tb.tb_frame.f_code.co_name)), file=self._stderr)
|
||||||
exc_tb = exc_tb.tb_next
|
exc_tb = exc_tb.tb_next
|
||||||
print(("%s: %s" % (exc_type, exc_value)), file=self._stderr)
|
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
|
# Make sure that exc_tb gets deleted since it is a memory
|
||||||
# hog; deleting everything else is just for thoroughness
|
# hog; deleting everything else is just for thoroughness
|
||||||
finally:
|
finally:
|
||||||
|
@ -1319,10 +1321,9 @@ except ImportError:
|
||||||
|
|
||||||
|
|
||||||
def _after_fork():
|
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
|
Cleanup threading module state that should not exist after a fork.
|
||||||
# state that should not exist after a fork.
|
"""
|
||||||
|
|
||||||
# Reset _active_limbo_lock, in case we forked while the lock was held
|
# Reset _active_limbo_lock, in case we forked while the lock was held
|
||||||
# by another (non-forked) thread. http://bugs.python.org/issue874900
|
# by another (non-forked) thread. http://bugs.python.org/issue874900
|
||||||
global _active_limbo_lock, _main_thread
|
global _active_limbo_lock, _main_thread
|
||||||
|
@ -1356,3 +1357,7 @@ def _after_fork():
|
||||||
_active.clear()
|
_active.clear()
|
||||||
_active.update(new_active)
|
_active.update(new_active)
|
||||||
assert len(_active) == 1
|
assert len(_active) == 1
|
||||||
|
|
||||||
|
|
||||||
|
if hasattr(_os, "fork"):
|
||||||
|
_os.register_at_fork(_after_fork, when="child")
|
||||||
|
|
|
@ -240,8 +240,6 @@ PyEval_ReleaseThread(PyThreadState *tstate)
|
||||||
void
|
void
|
||||||
PyEval_ReInitThreads(void)
|
PyEval_ReInitThreads(void)
|
||||||
{
|
{
|
||||||
_Py_IDENTIFIER(_after_fork);
|
|
||||||
PyObject *threading, *result;
|
|
||||||
PyThreadState *current_tstate = PyThreadState_GET();
|
PyThreadState *current_tstate = PyThreadState_GET();
|
||||||
|
|
||||||
if (!gil_created())
|
if (!gil_created())
|
||||||
|
@ -251,22 +249,6 @@ PyEval_ReInitThreads(void)
|
||||||
take_gil(current_tstate);
|
take_gil(current_tstate);
|
||||||
main_thread = PyThread_get_thread_ident();
|
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 */
|
/* Destroy all threads except the current one */
|
||||||
_PyThreadState_DeleteExcept(current_tstate);
|
_PyThreadState_DeleteExcept(current_tstate);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue