Skip to content

Commit 26af5f7

Browse files
colesburynohlson
authored andcommitted
pythongh-120974: Make _asyncio._enter_task atomic in the free-threaded build (python#122138)
Use `PyDict_SetDefaultRef` to set the current task in a single operation under the dictionary's lock.
1 parent 5935143 commit 26af5f7

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
@@ -2009,14 +2009,11 @@ static int
20092009
enter_task(asyncio_state *state, PyObject *loop, PyObject *task)
20102010
{
20112011
PyObject *item;
2012-
Py_hash_t hash;
2013-
hash = PyObject_Hash(loop);
2014-
if (hash == -1) {
2012+
int res = PyDict_SetDefaultRef(state->current_tasks, loop, task, &item);
2013+
if (res < 0) {
20152014
return -1;
20162015
}
2017-
item = _PyDict_GetItem_KnownHash(state->current_tasks, loop, hash);
2018-
if (item != NULL) {
2019-
Py_INCREF(item);
2016+
else if (res == 1) {
20202017
PyErr_Format(
20212018
PyExc_RuntimeError,
20222019
"Cannot enter into task %R while another " \
@@ -2025,10 +2022,8 @@ enter_task(asyncio_state *state, PyObject *loop, PyObject *task)
20252022
Py_DECREF(item);
20262023
return -1;
20272024
}
2028-
if (PyErr_Occurred()) {
2029-
return -1;
2030-
}
2031-
return _PyDict_SetItem_KnownHash(state->current_tasks, loop, task, hash);
2025+
Py_DECREF(item);
2026+
return 0;
20322027
}
20332028

20342029

0 commit comments

Comments
 (0)