Skip to content

Commit 8faab2e

Browse files
colesburymiss-islington
authored andcommitted
pythongh-120974: Make _asyncio._enter_task atomic in the free-threaded build (pythonGH-122138)
Use `PyDict_SetDefaultRef` to set the current task in a single operation under the dictionary's lock. (cherry picked from commit 47847aa) Co-authored-by: Sam Gross <[email protected]>
1 parent 4892cc0 commit 8faab2e

File tree

1 file changed

+5
-10
lines changed

1 file changed

+5
-10
lines changed

Modules/_asynciomodule.c

+5-10
Original file line numberDiff line numberDiff line change
@@ -1928,14 +1928,11 @@ static int
19281928
enter_task(asyncio_state *state, PyObject *loop, PyObject *task)
19291929
{
19301930
PyObject *item;
1931-
Py_hash_t hash;
1932-
hash = PyObject_Hash(loop);
1933-
if (hash == -1) {
1931+
int res = PyDict_SetDefaultRef(state->current_tasks, loop, task, &item);
1932+
if (res < 0) {
19341933
return -1;
19351934
}
1936-
item = _PyDict_GetItem_KnownHash(state->current_tasks, loop, hash);
1937-
if (item != NULL) {
1938-
Py_INCREF(item);
1935+
else if (res == 1) {
19391936
PyErr_Format(
19401937
PyExc_RuntimeError,
19411938
"Cannot enter into task %R while another " \
@@ -1944,10 +1941,8 @@ enter_task(asyncio_state *state, PyObject *loop, PyObject *task)
19441941
Py_DECREF(item);
19451942
return -1;
19461943
}
1947-
if (PyErr_Occurred()) {
1948-
return -1;
1949-
}
1950-
return _PyDict_SetItem_KnownHash(state->current_tasks, loop, task, hash);
1944+
Py_DECREF(item);
1945+
return 0;
19511946
}
19521947

19531948

0 commit comments

Comments
 (0)