Skip to content

Commit

Permalink
pythongh-120974: Make _asyncio._enter_task atomic in the free-threade…
Browse files Browse the repository at this point in the history
…d build

Use `PyDict_SetDefaultRef` to set the current task in a single operation
under the dictionary's lock.
  • Loading branch information
colesbury committed Jul 22, 2024
1 parent ad935a9 commit 2f638e9
Showing 1 changed file with 5 additions and 10 deletions.
15 changes: 5 additions & 10 deletions Modules/_asynciomodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -2009,14 +2009,11 @@ static int
enter_task(asyncio_state *state, PyObject *loop, PyObject *task)
{
PyObject *item;
Py_hash_t hash;
hash = PyObject_Hash(loop);
if (hash == -1) {
int res = PyDict_SetDefaultRef(state->current_tasks, loop, task, &item);
if (res < 0) {
return -1;
}
item = _PyDict_GetItem_KnownHash(state->current_tasks, loop, hash);
if (item != NULL) {
Py_INCREF(item);
else if (res == 1) {
PyErr_Format(
PyExc_RuntimeError,
"Cannot enter into task %R while another " \
Expand All @@ -2025,10 +2022,8 @@ enter_task(asyncio_state *state, PyObject *loop, PyObject *task)
Py_DECREF(item);
return -1;
}
if (PyErr_Occurred()) {
return -1;
}
return _PyDict_SetItem_KnownHash(state->current_tasks, loop, task, hash);
Py_DECREF(item);
return 0;
}


Expand Down

0 comments on commit 2f638e9

Please sign in to comment.