]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-154695: fix asyncio.Task eager_start=True with no explicit loop (#154697)
authorTimofei <128279579+deadlovelll@users.noreply.github.com>
Sun, 26 Jul 2026 04:56:24 +0000 (07:56 +0300)
committerGitHub <noreply@github.com>
Sun, 26 Jul 2026 04:56:24 +0000 (10:26 +0530)
Lib/test/test_asyncio/test_tasks.py
Misc/NEWS.d/next/Core_and_Builtins/2026-07-25-17-35-14.gh-issue-154695.aQWLLv.rst [new file with mode: 0644]
Modules/_asynciomodule.c

index 609ccdbfdb2b28900817c873aeea5340c6615e3c..110cd0936c007c91b7b13db4c4ead551f3a5118a 100644 (file)
@@ -2733,6 +2733,18 @@ class BaseTaskTests:
             self.assertEqual(name, "example")
             await t
 
+    def test_eager_start_true_no_loop(self):
+        # gh-154695: eager_start must use the resolved loop, not loop=None.
+        async def asyncfn():
+            return 42
+
+        async def main():
+            t = self.__class__.Task(asyncfn(), eager_start=True)
+            self.assertTrue(t.done())
+            self.assertEqual(await t, 42)
+
+        asyncio.run(main(), loop_factory=asyncio.EventLoop)
+
     def test_eager_start_false(self):
         name = None
 
diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-07-25-17-35-14.gh-issue-154695.aQWLLv.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-07-25-17-35-14.gh-issue-154695.aQWLLv.rst
new file mode 100644 (file)
index 0000000..6e147cf
--- /dev/null
@@ -0,0 +1,2 @@
+Fix :class:`asyncio.Task` raising :exc:`AttributeError` when created with
+``eager_start=True`` and no explicit *loop* argument.
index 9c29eb8232ed3f927be18ad5e17f783bcf52e541..0cd41d0b4c4d0dccea8b9e5a60a038ad036ca6f8 100644 (file)
@@ -2364,7 +2364,9 @@ _asyncio_Task___init___impl(TaskObj *self, PyObject *coro, PyObject *loop,
     _PyObject_SetMaybeWeakref((PyObject *)self);
 #endif
     if (eager_start) {
-        PyObject *res = PyObject_CallMethodNoArgs(loop, &_Py_ID(is_running));
+        // gh-154695: loop may be None here, future_init() resolved it into task_loop.
+        PyObject *res = PyObject_CallMethodNoArgs(self->task_loop,
+                                                  &_Py_ID(is_running));
         if (res == NULL) {
             return -1;
         }