From 35a0222728fae257b878cff1fb8cce65c47f4b7f Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Fri, 29 Mar 2024 17:59:12 +0100 Subject: [PATCH 1/2] Assert that the lock is held in already_warned() --- Python/_warnings.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Python/_warnings.c b/Python/_warnings.c index 66a460e2a2c509..473490b840e1a2 100644 --- a/Python/_warnings.c +++ b/Python/_warnings.c @@ -398,9 +398,9 @@ already_warned(PyInterpreterState *interp, PyObject *registry, PyObject *key, return -1; WarningsState *st = warnings_get_state(interp); - if (st == NULL) { - return -1; - } + assert(st != NULL); + _Py_CRITICAL_SECTION_ASSERT_MUTEX_LOCKED(&st->mutex); + PyObject *version_obj; if (PyDict_GetItemRef(registry, &_Py_ID(version), &version_obj) < 0) { return -1; From 8459092533b5022ce3e3f61c8b468d0478794544 Mon Sep 17 00:00:00 2001 From: "Erlend E. Aasland" Date: Fri, 29 Mar 2024 18:14:30 +0100 Subject: [PATCH 2/2] Protect filters_version increment in warnings_filters_mutated_impl --- Python/_warnings.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/Python/_warnings.c b/Python/_warnings.c index 473490b840e1a2..c16bc5362d4502 100644 --- a/Python/_warnings.c +++ b/Python/_warnings.c @@ -1173,11 +1173,14 @@ warnings_filters_mutated_impl(PyObject *module) if (interp == NULL) { return NULL; } + WarningsState *st = warnings_get_state(interp); - if (st == NULL) { - return NULL; - } + assert(st != NULL); + + Py_BEGIN_CRITICAL_SECTION_MUT(&st->mutex); st->filters_version++; + Py_END_CRITICAL_SECTION(); + Py_RETURN_NONE; }