Skip to content

gh-151633: avoid use-after-free in Counter.update#151634

Open
KowalskiThomas wants to merge 9 commits into
python:mainfrom
KowalskiThomas:kowalski/fix-avoid-use-after-free-in-counter-update
Open

gh-151633: avoid use-after-free in Counter.update#151634
KowalskiThomas wants to merge 9 commits into
python:mainfrom
KowalskiThomas:kowalski/fix-avoid-use-after-free-in-counter-update

Conversation

@KowalskiThomas

@KowalskiThomas KowalskiThomas commented Jun 18, 2026

Copy link
Copy Markdown
Contributor

Comment thread Modules/_collectionsmodule.c Outdated
Co-authored-by: Pieter Eendebak <pieter.eendebak@gmail.com>
Comment thread Lib/test/test_free_threading/test_collections.py Outdated
Comment thread Lib/test/test_free_threading/test_collections.py Outdated
Comment thread Lib/test/test_free_threading/test_collections.py Outdated
Comment thread Modules/_collectionsmodule.c Outdated
@KowalskiThomas

Copy link
Copy Markdown
Contributor Author

Thanks for the review, I'll take a look ASAP.

int found;
int cs_err = 0;
PyObject *newval = NULL;
Py_BEGIN_CRITICAL_SECTION(mapping);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The current PR looks good, but I think we can improve a bit more. The locking via the critical section now happens on each iteration, which seems a bit of a waste.

Can we place the critical section outside the while loop and then extract most (all?) of the code into a method _count_elements_dict_lock_held?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Concurrent calls to Counter.update can cause a use after free

2 participants