8000 gh-109860: Use a New Thread State When Switching Interpreters, When Necessary by ericsnowcurrently · Pull Request #110245 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-109860: Use a New Thread State When Switching Interpreters, When Necessary #110245

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
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Release threading._main_thread._tstate_lock in _PyInterpreterState_Se…
…tNotRunningMain().
  • Loading branch information
ericsnowcurrently committed Oct 2, 2023
commit cd288b5ff3e4c68f3fc8970c22d5726de88d55aa
7 changes: 5 additions & 2 deletions Lib/threading.py
Original file line number Diff line number Diff line change
Expand Up @@ -1593,8 +1593,11 @@ def _shutdown():
# The main thread isn't finished yet, so its thread state lock can't
# have been released.
assert tlock is not None
assert tlock.locked()
tlock.release()
if tlock.locked():
# It should have been released already by
# PyInterpreterState_SetNotRunningMain(), but there may be
# embedders that aren't calling that yet.
tlock.release()
_main_thread._stop()
else:
# bpo-1596321: _shutdown() must be called in the main thread.
Expand Down
27 changes: 22 additions & 5 deletions Python/pystate.c
Original file line number Diff line number Diff line change
Expand Up @@ -1113,7 +1113,20 @@ _PyInterpreterState_SetRunningMain(PyInterpreterState *interp)
void
_PyInterpreterState_SetNotRunningMain(PyInterpreterState *interp)
{
assert(interp->threads.main == current_fast_get(&_PyRuntime));
PyThreadState *tstate = interp->threads.main;
assert(tstate == current_fast_get(&_PyRuntime));

if (tstate->on_delete != NULL) {
// The threading module was imported for the first time in this
// thread, so it was set as threading._main_thread. (See gh-75698.)
// The thread has finished running the Python program so we mark
// the thread object as finished.
assert(tstate->_whence != _PyThreadState_WHENCE_THREADING);
tstate->on_delete(tstate->on_delete_data);
tstate->on_delete = NULL;
tstate->on_delete_data = NULL;
}

interp->threads.main = NULL;
}

Expand Down Expand Up @@ -1592,10 +1605,14 @@ PyThreadState_Clear(PyThreadState *tstate)
Py_CLEAR(tstate->context);

if (tstate->on_delete != NULL) {
// XXX Once we fix gh-75698:
//assert(tstate->_whence == _PyThreadState_WHENCE_THREADING
// || (tstate->interp->finalizing
// && tstate == _PyInterpreterState_GetFinalizing(tstate->interp)));
// For the "main" thread of each interpreter, this is meant
// to be done in PyInterpreterState_SetNotRunningMain().
// Tha leaves threads created by the threading module.
// However, we also accommodate "main" threads that still
// don't call PyInterpreterState_SetNotRunningMain() yet.
assert(tstate->_whence == _PyThreadState_WHENCE_THREADING
|| (tstate->interp->finalizing
&& tstate == _PyInterpreterState_GetFinalizing(tstate->interp)));
tstate->on_delete(tstate->on_delete_data);
}

Expand Down
0