-
-
Notifications
You must be signed in to change notification settings - Fork 10.9k
MAINT: use an atomic load/store and a mutex to initialize the argparse and runtime import caches #26780
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
MAINT: use an atomic load/store and a mutex to initialize the argparse and runtime import caches #26780
Changes from 1 commit
e55b25c
6386423
9a22d84
6ea18a4
3c620cb
2e2fa1d
b6af99a
dabfe59
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -82,14 +82,16 @@ npy_import(const char *module, const char *attr) | |
static inline int | ||
npy_cache_import_runtime(const char *module, const char *attr, PyObject **obj) { | ||
if (!npy_atomic_load_ptr(obj)) { | ||
PyObject* value = npy_import(module, attr); | ||
if (value == NULL) { | ||
return -1; | ||
} | ||
PyThread_acquire_lock(npy_runtime_imports.import_mutex, WAIT_LOCK); | ||
if (!npy_atomic_load_ptr(obj)) { | ||
npy_atomic_store_ptr(obj, npy_import(module, attr)); | ||
if (obj == NULL) { | ||
return -1; | ||
} | ||
npy_atomic_store_ptr(obj, Py_NewRef(value)); | ||
} | ||
PyThread_release_lock(npy_runtime_imports.import_mutex); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move the release before the error check, or we will dead-lock on error! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch! Also, the But I think it would be better not to hold the lock around the
So I'd suggest writing this as: if (!npy_atomic_load_ptr(obj)) {
PyObject *value = npy_import(module, attr);
if (value == NULL) {
return -1;
}
PyThread_acquire_lock(npy_runtime_imports.import_mutex, WAIT_LOCK);
if (!npy_atomic_load_ptr(obj)) {
npy_atomic_store_ptr(obj, Py_NewRef(value));
}
PyThread_release_lock(npy_runtime_imports.import_mutex);
Py_DECREF(value);
} Or you can get rid of the lock if you implement compare-exchange: if (!npy_atomic_load_ptr(obj)) {
PyObject *value = npy_import(module, attr);
if (value == NULL) {
return -1;
}
PyObject *exepected = NULL;
if (!npy_atomic_compare_exchange_ptr(obj, &expected, value)) {
Py_DECREF(value);
}
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks! I kept the lock since the next iteration will use PyMutex and I'd like to make sure this gets in for NumPy 2.1. Thankfully deadsnakes was updated over the weekend so we can test against a version with a public PyMutex now. |
||
Py_DECREF(value); | ||
} | ||
return 0; | ||
} | ||
|
Uh oh!
There was an error while loading. Please reload this page.