gh-116664: In _warnings.c, make filters_version access thread-safe (#117374)

- assert that the lock is held in already_warned()
- protect 'filters_version' increment in warnings_filters_mutated_impl()
This commit is contained in:
Erlend E. Aasland 2024-03-29 21:23:28 +01:00 committed by GitHub
parent 019143fecb
commit 05e0b67a43
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -398,9 +398,9 @@ already_warned(PyInterpreterState *interp, PyObject *registry, PyObject *key,
return -1; return -1;
WarningsState *st = warnings_get_state(interp); WarningsState *st = warnings_get_state(interp);
if (st == NULL) { assert(st != NULL);
return -1; _Py_CRITICAL_SECTION_ASSERT_MUTEX_LOCKED(&st->mutex);
}
PyObject *version_obj; PyObject *version_obj;
if (PyDict_GetItemRef(registry, &_Py_ID(version), &version_obj) < 0) { if (PyDict_GetItemRef(registry, &_Py_ID(version), &version_obj) < 0) {
return -1; return -1;
@ -1177,11 +1177,14 @@ warnings_filters_mutated_impl(PyObject *module)
if (interp == NULL) { if (interp == NULL) {
return NULL; return NULL;
} }
WarningsState *st = warnings_get_state(interp); WarningsState *st = warnings_get_state(interp);
if (st == NULL) { assert(st != NULL);
return NULL;
} Py_BEGIN_CRITICAL_SECTION_MUT(&st->mutex);
st->filters_version++; st->filters_version++;
Py_END_CRITICAL_SECTION();
Py_RETURN_NONE; Py_RETURN_NONE;
} }