Skip to content

bpo-16500: Use register_at_fork() in the threading module #1843

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions Lib/threading.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Thread module emulating a subset of Java's threading model."""

import os as _os
import sys as _sys
import _thread

Expand Down Expand Up @@ -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()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just pass flush=True to the print() above.

# Make sure that exc_tb gets deleted since it is a memory
# hog; deleting everything else is just for thoroughness
finally:
Expand Down Expand Up @@ -1319,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
Expand Down Expand Up @@ -1356,3 +1357,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")
18 changes: 0 additions & 18 deletions Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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);
}
Expand Down