Skip to content
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

gh-120837: Update _Py_DumpExtensionModules to be async-signal-safe #121051

Merged
merged 8 commits into from
Jun 27, 2024
Merged
Changes from 6 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
39 changes: 37 additions & 2 deletions Python/pylifecycle.c
Original file line number Diff line number Diff line change
Expand Up @@ -2897,6 +2897,31 @@ fatal_error_exit(int status)
}
}

static inline int
acquire_dict_lock_for_dump(PyObject *obj) {
colesbury marked this conversation as resolved.
Show resolved Hide resolved
#ifdef Py_GIL_DISABLED
PyMutex *mutex = &obj->ob_mutex;
if (_PyMutex_LockTimed(mutex, 0, 0) == PY_LOCK_ACQUIRED) {
return 0;
colesbury marked this conversation as resolved.
Show resolved Hide resolved
}
return -1;
#else
return 0;
#endif
}

static inline void
release_dict_lock_for_dump(PyObject *obj) {
colesbury marked this conversation as resolved.
Show resolved Hide resolved
#ifdef Py_GIL_DISABLED
PyMutex *mutex = &obj->ob_mutex;
uint8_t expected = _Py_LOCKED;
// We can not call PyMutex_Unlock because it's not async-signal-safe.
// So not to wake up other threads, we just use a simple CAS in here.
_Py_atomic_compare_exchange_uint8(&mutex->_bits, &expected, _Py_UNLOCKED);
Copy link
Member Author

Choose a reason for hiding this comment

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

Do we have to run for loop to prevent CAS failure?

Copy link
Contributor

Choose a reason for hiding this comment

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

I think this should be a _Py_atomic_store_uint8(&mutex->_bits, _Py_UNLOCKED)

#else
return;
Copy link
Contributor

Choose a reason for hiding this comment

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

The #else return; doesn't do anything. I would delete it, but up to you.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done!

#endif
}

// Dump the list of extension modules of sys.modules, excluding stdlib modules
// (sys.stdlib_module_names), into fd file descriptor.
Expand Down Expand Up @@ -2924,13 +2949,18 @@ _Py_DumpExtensionModules(int fd, PyInterpreterState *interp)
PyObject *stdlib_module_names = NULL;
if (interp->sysdict != NULL) {
pos = 0;
while (PyDict_Next(interp->sysdict, &pos, &key, &value)) {
if (acquire_dict_lock_for_dump(interp->sysdict) < 0) {
// If we cannot acquire the lock, just don't dump the list of extension modules.
return;
}
while (_PyDict_Next(interp->sysdict, &pos, &key, &value, NULL)) {
if (PyUnicode_Check(key)
&& PyUnicode_CompareWithASCIIString(key, "stdlib_module_names") == 0) {
stdlib_module_names = value;
break;
}
}
release_dict_lock_for_dump(interp->sysdict);
}
// If we failed to get sys.stdlib_module_names or it's not a frozenset,
// don't exclude stdlib modules.
Expand All @@ -2942,7 +2972,11 @@ _Py_DumpExtensionModules(int fd, PyInterpreterState *interp)
int header = 1;
Py_ssize_t count = 0;
pos = 0;
while (PyDict_Next(modules, &pos, &key, &value)) {
if (acquire_dict_lock_for_dump(modules) < 0) {
// If we cannot acquire the lock, just don't dump the list of extension modules.
return;
}
while (_PyDict_Next(modules, &pos, &key, &value, NULL)) {
if (!PyUnicode_Check(key)) {
continue;
}
Expand Down Expand Up @@ -2983,6 +3017,7 @@ _Py_DumpExtensionModules(int fd, PyInterpreterState *interp)
_Py_DumpASCII(fd, key);
count++;
}
release_dict_lock_for_dump(modules);

if (count) {
PUTS(fd, " (total: ");
Expand Down
Loading