Skip to content

gh-117783: Immortalize objects that use deferred reference counting #118112

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 6 commits into from
Apr 29, 2024
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
Remove module dict swapping
  • Loading branch information
colesbury committed Apr 26, 2024
commit 588255e43baf7adfa312cf0ea7dba8bbbdd66c9a
5 changes: 3 additions & 2 deletions Lib/concurrent/futures/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,9 @@ def __init__(self, executor):
# if there is no pending work item.
def weakref_cb(_,
thread_wakeup=self.thread_wakeup,
shutdown_lock=self.shutdown_lock):
mp.util.debug('Executor collected: triggering callback for'
shutdown_lock=self.shutdown_lock,
mp_util_debug=mp.util.debug):
mp_util_debug('Executor collected: triggering callback for'
' QueueManager wakeup')
with shutdown_lock:
thread_wakeup.wakeup()
Expand Down
18 changes: 0 additions & 18 deletions Python/pylifecycle.c
Original file line number Diff line number Diff line change
Expand Up @@ -1491,23 +1491,6 @@ finalize_modules_delete_special(PyThreadState *tstate, int verbose)
}
}

static void
swap_module_dict(PyModuleObject *mod)
{
if (_Py_IsImmortal(mod->md_dict)) {
// gh-117783: Immortalizing module dicts can cause some finalizers to
// run much later than typical leading to attribute errors due to
// partially cleared modules. To avoid this, we copy the module dict
// if it was immortalized.
PyObject *copy = PyDict_Copy(mod->md_dict);
if (copy == NULL) {
PyErr_FormatUnraisable("Exception ignored on removing modules");
return;
}
Py_SETREF(mod->md_dict, copy);
}
}

static PyObject*
finalize_remove_modules(PyObject *modules, int verbose)
{
Expand Down Expand Up @@ -1537,7 +1520,6 @@ finalize_remove_modules(PyObject *modules, int verbose)
if (verbose && PyUnicode_Check(name)) { \
PySys_FormatStderr("# cleanup[2] removing %U\n", name); \
} \
swap_module_dict((PyModuleObject *)mod); \
STORE_MODULE_WEAKREF(name, mod); \
if (PyObject_SetItem(modules, name, Py_None) < 0) { \
PyErr_FormatUnraisable("Exception ignored on removing modules"); \
Expand Down
Loading