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
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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I'll look into this as soon as possible, thanks for reporting.

@KowalskiThomas KowalskiThomas force-pushed the kowalski/fix-avoid-use-after-free-in-counter-update branch from 192fc9f to c34c4a2 Compare July 14, 2026 09:16
@KowalskiThomas

Copy link
Copy Markdown
Contributor Author

Sorry in advance, I had to rebase on main (instead of merge) because Git wouldn't let me "merge unrelated histories"... Not sure what happened there...

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