Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
b64ce9f
Factor out tstate_verify_not_active().
ericsnowcurrently Jan 19, 2023
1be0ec4
Drop the check_current param from _PyThreadState_Delete().
ericsnowcurrently Jan 19, 2023
3454bf1
Drop _PyThreadState_Delete().
ericsnowcurrently Jan 19, 2023
a929e32
Unset the "current" thread state before zapping the threads instead o…
ericsnowcurrently Jan 19, 2023
e6ddd92
Only clear the current thread if the interpreter matches.
ericsnowcurrently Jan 19, 2023
36c512f
Always "check_current" in zapthreads().
ericsnowcurrently Jan 19, 2023
d402a82
Do not pass the runtime to _PyThreadState_DeleteExcept().
ericsnowcurrently Jan 19, 2023
2ddfe71
Add some notes, TODO comments, and asserts.
ericsnowcurrently Jan 19, 2023
99de509
Mark the main interpreter as finalizing during runtime fini.
ericsnowcurrently Jan 19, 2023
b77ae5e
Add more notes and TODO comments.
ericsnowcurrently Jan 19, 2023
9ca673c
Make PyThreadState._status more granular.
ericsnowcurrently Jan 19, 2023
8a71957
Add more status fields.
ericsnowcurrently Jan 20, 2023
62d1a93
Add a TODO.
ericsnowcurrently Jan 20, 2023
90cea92
Track active status.
ericsnowcurrently Jan 19, 2023
9965813
Clarify a TODO comment.
ericsnowcurrently Jan 20, 2023
fd5048b
Associate "bound" and "active".
ericsnowcurrently Jan 20, 2023
2105cd6
_PyThreadState_Prealloc() -> _PyThreadState_New()
ericsnowcurrently Jan 23, 2023
8b110cf
Factor out tstate_tss_*().
ericsnowcurrently Jan 24, 2023
ca98d68
current_tss_*() -> gilstate_tss_*().
ericsnowcurrently Jan 24, 2023
4f39976
Add bind_gilstate_tstate() and unbind_gilstate_tstate().
ericsnowcurrently Jan 24, 2023
6e73669
Update some TODO comments.
ericsnowcurrently Jan 24, 2023
cc3540d
Clean up _PyThreadState_Swap() a little.
ericsnowcurrently Jan 24, 2023
1ce3841
Fix the stable ABI.
ericsnowcurrently Jan 25, 2023
121d328
Fixes for multiprocessing.
ericsnowcurrently Jan 25, 2023
8666362
Move a comment.
ericsnowcurrently Jan 25, 2023
52ac2d9
Preserve errno more carefully.
ericsnowcurrently Jan 25, 2023
f7ac19a
Do not call bind_gilstate_tstate() in bind_tstate().
ericsnowcurrently Jan 25, 2023
a338248
Add an assert.
ericsnowcurrently Jan 25, 2023
5be78e9
Clean up bind_gilstate_tstate().
ericsnowcurrently Jan 25, 2023
f019bd6
Clear bound_gilstate for the old thread state.
ericsnowcurrently Jan 25, 2023
6869bfe
bound_gilstate and gilstate_tss_get() must match.
ericsnowcurrently Jan 25, 2023
f91d458
Add a blank line for clarity.
ericsnowcurrently Jan 25, 2023
9b45398
Do not call unbind_gilstate_tstate() in unbind_tstate().
ericsnowcurrently Jan 25, 2023
98a2dae
Fix comments.
ericsnowcurrently Jan 25, 2023
afde196
Fix padding.
ericsnowcurrently Jan 30, 2023
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
Add some notes, TODO comments, and asserts.
  • Loading branch information
ericsnowcurrently committed Jan 25, 2023
commit 2ddfe71488bb79a2aef97cc677ce33f1819ced13
32 changes: 32 additions & 0 deletions Python/pylifecycle.c
Original file line number Diff line number Diff line change
Expand Up @@ -1821,6 +1821,8 @@ Py_FinalizeEx(void)

/* Get current thread state and interpreter pointer */
PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
// XXX assert(_Py_IsMainInterpreter(tstate->interp));
// XXX assert(_Py_IsMainThread());

// Wrap up existing "threading"-module-created, non-daemon threads.
wait_for_thread_shutdown(tstate);
Expand Down Expand Up @@ -1869,6 +1871,22 @@ Py_FinalizeEx(void)
immediately. */
_PyThreadState_DeleteExcept(tstate);

/* At this point no Python code should be running at all.
The only thread state left should be the main thread of the main
interpreter (AKA tstate), in which this code is running right now.
There may be other OS threads running but none of them will have
thread states associated with them, nor will be able to create
new thread states.

Thus tstate is the only possible thread state from here on out.
It may still be used during finalization to run Python code as
needed or provide runtime state (e.g. sys.modules) but that will
happen sparingly. Furthermore, the order of finalization aims
to not need a thread (or interpreter) state as soon as possible.
*/
// XXX Make sure we are preventing the creating of any new thread states
// (or interpreters).

/* Flush sys.stdout and sys.stderr */
if (flush_std_files() < 0) {
status = -1;
Expand Down Expand Up @@ -1958,6 +1976,20 @@ Py_FinalizeEx(void)
}
#endif /* Py_TRACE_REFS */

/* At this point there's almost no other Python code that will run,
nor interpreter state needed. The only possibility is the
finalizers of the objects stored on tstate (and tstate->interp),
which are triggered via finalize_interp_clear().

For now we operate as though none of those finalizers actually
need an operational thread state or interpreter. In reality,
those finalizers may rely on some part of tstate or
tstate->interp, and/or may raise exceptions
or otherwise fail.
*/
// XXX Do this sooner during finalization.
// XXX Ensure finalizer errors are handled properly.

finalize_interp_clear(tstate);
finalize_interp_delete(tstate->interp);

Expand Down
25 changes: 24 additions & 1 deletion Python/pystate.c
Original file line number Diff line number Diff line change
Expand Up @@ -671,18 +671,35 @@ PyInterpreterState_New(void)
static void
interpreter_clear(PyInterpreterState *interp, PyThreadState *tstate)
{
assert(interp != NULL && tstate != NULL);
_PyRuntimeState *runtime = interp->runtime;

/* XXX Conditions we need to enforce:

* the GIL must be held by the current thread
* tstate must be the "current" thread state (current_fast_get())
* tstate->interp must be interp
* for the main interpreter, tstate must be the main thread
*/

if (_PySys_Audit(tstate, "cpython.PyInterpreterState_Clear", NULL) < 0) {
_PyErr_Clear(tstate);
}

HEAD_LOCK(runtime);
// XXX Clear the current/main thread state last.
for (PyThreadState *p = interp->threads.head; p != NULL; p = p->next) {
PyThreadState_Clear(p);
}
HEAD_UNLOCK(runtime);

/* It is possible that any of the objects below have a finalizer
that runs Python code or otherwise relies on a thread state
or even the interpreter state. For now we trust that isn't
a problem.
*/
// XXX Make sure we properly deal with problematic finalizers.

Py_CLEAR(interp->audit_hooks);

PyConfig_Clear(&interp->config);
Expand Down Expand Up @@ -753,7 +770,6 @@ PyInterpreterState_Clear(PyInterpreterState *interp)
// garbage. It can be different than the current Python thread state
// of 'interp'.
PyThreadState *current_tstate = current_fast_get(interp->runtime);

interpreter_clear(interp, current_tstate);
}

Expand Down Expand Up @@ -1226,6 +1242,10 @@ _PyThreadState_Init(PyThreadState *tstate)
void
PyThreadState_Clear(PyThreadState *tstate)
{
// The GIL must be held by the current thread,
// which must not be the target.
// XXX Enforce that (check current_fast_get()).

int verbose = _PyInterpreterState_GetConfig(tstate->interp)->verbose;

if (verbose && tstate->cframe->current_frame != NULL) {
Expand Down Expand Up @@ -1270,6 +1290,9 @@ PyThreadState_Clear(PyThreadState *tstate)
if (tstate->on_delete != NULL) {
tstate->on_delete(tstate->on_delete_data);
}

// XXX Call _PyThreadStateSwap(runtime, NULL) here if "current".
// XXX Do it as early in the function as possible.
}


Expand Down