Skip to content

Commit

Permalink
gh-116664: In _warnings.c, make filters_version access thread-safe (#…
Browse files Browse the repository at this point in the history
…117374)

- assert that the lock is held in already_warned()
- protect 'filters_version' increment in warnings_filters_mutated_impl()
  • Loading branch information
erlend-aasland committed Mar 29, 2024
1 parent 019143f commit 05e0b67
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions Python/_warnings.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -1177,11 +1177,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;
}

Expand Down

0 comments on commit 05e0b67

Please sign in to comment.