8000 [WIP] bpo-33608: Factor out a private, per-interpreter _Py_AddPendingCall(). by ericsnowcurrently · Pull Request #9334 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

[WIP] bpo-33608: Factor out a private, per-interpreter _Py_AddPendingCall(). #9334

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

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
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
Prevent adding pending calls if finalizing.
  • Loading branch information
ericsnowcurrently committed Sep 15, 2018
commit 3faea72e1e1d3dc9b575905d07613228f871ec06
2 changes: 2 additions & 0 deletions Include/pystate.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ typedef struct _is {
int64_t id_refcount;
PyThread_type_lock id_mutex;

int finalizing;

PyObject *modules;
PyObject *modules_by_index;
PyObject *sysdict;
Expand Down
12 changes: 12 additions & 0 deletions Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,16 @@ _Py_AddPendingCall(PyInterpreterState *interp, int (*func)(void *), void *arg)
return -1;
}

if (interp->finalizing) {
PyObject *exc, *val, *tb;
PyErr_Fetch(&exc, &val, &tb);
PyErr_SetString(PyExc_SystemError, "Py_AddPendingCall: cannot add pending calls (interpreter shutting down)");
PyErr_Print();
PyErr_Restore(exc, val, tb);
result = -1;
goto done;
}

i = interp->ceval.pending.last;
j = (i + 1) % NPENDINGCALLS;
if (j == interp->ceval.pending.first) {
Expand All @@ -359,6 +369,8 @@ _Py_AddPendingCall(PyInterpreterState *interp, int (*func)(void *), void *arg)
}
/* signal main loop */
SIGNAL_PENDING_CALLS(interp);

done:
if (lock != NULL)
PyThread_release_lock(lock);
return result;
Expand Down
11 changes: 11 additions & 0 deletions Python/pylifecycle.c
589D
Original file line number Diff line number Diff line change
Expand Up @@ -1416,10 +1416,21 @@ Py_EndInterpreter(PyThreadState *tstate)
if (tstate->frame != NULL)
Py_FatalError("Py_EndInterpreter: thread still has a frame");

// Mark as finalizing.
if (interp->ceval.pending.lock != NULL) {
PyThread_acquire_lock(interp->ceval.pending.lock, 1);
}
interp->finalizing = 1;
if (interp->ceval.pending.lock != NULL) {
PyThread_release_lock(interp->ceval.pending.lock);
}

// Wrap up existing threads.
wait_for_thread_shutdown();
interp->ceval.active = 1;
interp->ceval.active_thread = PyThread_get_thread_ident();

// Make any pending calls.
if (_Py_atomic_load_relaxed(
&(interp->ceval.pending.calls_to_do)))
{
Expand Down
0