Skip to content

gh-112075: Remove critical section in dict.get #129336

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion Lib/test/test_free_threading/test_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from ast import Or
from functools import partial
from threading import Thread
from threading import Barrier, Thread
from unittest import TestCase

try:
Expand Down Expand Up @@ -142,6 +142,27 @@ def writer_func(l):
for ref in thread_list:
self.assertIsNone(ref())

def test_racing_get_set_dict(self):
"""Races getting and setting a dict should be thread safe"""
THREAD_COUNT = 10
barrier = Barrier(THREAD_COUNT)
def work(d):
barrier.wait()
for _ in range(1000):
d[10] = 0
d.get(10, None)
_ = d[10]

d = {}
worker_threads = []
for ii in range(THREAD_COUNT):
worker_threads.append(Thread(target=work, args=[d]))
for t in worker_threads:
t.start()
for t in worker_threads:
t.join()


def test_racing_set_object_dict(self):
"""Races assigning to __dict__ should be thread safe"""
class C: pass
Expand Down
4 changes: 1 addition & 3 deletions Objects/clinic/dictobject.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions Objects/dictobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -4248,7 +4248,6 @@ dict___contains__(PyDictObject *self, PyObject *key)
}

/*[clinic input]
@critical_section
dict.get

key: object
Expand All @@ -4260,7 +4259,7 @@ Return the value for key if key is in the dictionary, else default.

static PyObject *
dict_get_impl(PyDictObject *self, PyObject *key, PyObject *default_value)
/*[clinic end generated code: output=bba707729dee05bf input=a631d3f18f584c60]*/
/*[clinic end generated code: output=bba707729dee05bf input=279ddb5790b6b107]*/
{
PyObject *val = NULL;
Py_hash_t hash;
Expand Down
Loading