https://github.com/python/cpython/commit/48154e705318f1ef800c48f39984ce5896a4ee47
commit: 48154e705318f1ef800c48f39984ce5896a4ee47
branch: 3.13
author: Miss Islington (bot) <[email protected]>
committer: kumaraditya303 <[email protected]>
date: 2024-07-23T09:17:52Z
summary:

[3.13] gh-120974: Make _asyncio._enter_task atomic in the free-threaded build 
(GH-122138) (#122152)

gh-120974: Make _asyncio._enter_task atomic in the free-threaded build 
(GH-122138)

Use `PyDict_SetDefaultRef` to set the current task in a single operation
under the dictionary's lock.
(cherry picked from commit 47847aa8ef66837f984fc4e30187d88f8d8ab201)

Co-authored-by: Sam Gross <[email protected]>

files:
M Modules/_asynciomodule.c

diff --git a/Modules/_asynciomodule.c b/Modules/_asynciomodule.c
index 23cbd48528e0f8..fffa6d25d022ac 100644
--- a/Modules/_asynciomodule.c
+++ b/Modules/_asynciomodule.c
@@ -1928,14 +1928,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 " \
@@ -1944,10 +1941,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;
 }
 
 

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]

Reply via email to