From e3fb3f34cf611725866198d321c0d90c76b56e01 Mon Sep 17 00:00:00 2001 From: Kumar Aditya Date: Mon, 6 Jan 2025 11:37:43 +0000 Subject: [PATCH] wip --- Lib/test/test_asyncio/test_free_threading.py | 86 -------------------- Modules/_asynciomodule.c | 9 +- 2 files changed, 2 insertions(+), 93 deletions(-) diff --git a/Lib/test/test_asyncio/test_free_threading.py b/Lib/test/test_asyncio/test_free_threading.py index 8fb5ea5f5612ea4..90bddbf3a9dda16 100644 --- a/Lib/test/test_asyncio/test_free_threading.py +++ b/Lib/test/test_asyncio/test_free_threading.py @@ -7,10 +7,6 @@ threading_helper.requires_working_threading(module=True) -class MyException(Exception): - pass - - def tearDownModule(): asyncio._set_event_loop_policy(None) @@ -57,88 +53,6 @@ def runner(): with threading_helper.start_threads(threads): pass - def test_run_coroutine_threadsafe(self) -> None: - results = [] - - def in_thread(loop: asyncio.AbstractEventLoop): - coro = asyncio.sleep(0.1, result=42) - fut = asyncio.run_coroutine_threadsafe(coro, loop) - result = fut.result() - self.assertEqual(result, 42) - results.append(result) - - async def main(): - loop = asyncio.get_running_loop() - async with asyncio.TaskGroup() as tg: - for _ in range(10): - tg.create_task(asyncio.to_thread(in_thread, loop)) - self.assertEqual(results, [42] * 10) - - with asyncio.Runner() as r: - loop = r.get_loop() - loop.set_task_factory(self.factory) - r.run(main()) - - def test_run_coroutine_threadsafe_exception_caught(self) -> None: - exc = MyException("test") - - async def coro(): - await asyncio.sleep(0.1) - raise exc - - def in_thread(loop: asyncio.AbstractEventLoop): - fut = asyncio.run_coroutine_threadsafe(coro(), loop) - self.assertEqual(fut.exception(), exc) - return exc - - async def main(): - loop = asyncio.get_running_loop() - tasks = [] - async with asyncio.TaskGroup() as tg: - for _ in range(10): - task = tg.create_task(asyncio.to_thread(in_thread, loop)) - tasks.append(task) - for task in tasks: - self.assertEqual(await task, exc) - - with asyncio.Runner() as r: - loop = r.get_loop() - loop.set_task_factory(self.factory) - r.run(main()) - - def test_run_coroutine_threadsafe_exception_uncaught(self) -> None: - async def coro(): - await asyncio.sleep(1) - raise MyException("test") - - def in_thread(loop: asyncio.AbstractEventLoop): - fut = asyncio.run_coroutine_threadsafe(coro(), loop) - return fut.result() - - async def main(): - loop = asyncio.get_running_loop() - tasks = [] - try: - async with asyncio.TaskGroup() as tg: - for _ in range(10): - task = tg.create_task(asyncio.to_thread(in_thread, loop)) - tasks.append(task) - except ExceptionGroup: - for task in tasks: - try: - await task - except (MyException, asyncio.CancelledError): - pass - else: - self.fail("Task should have raised an exception") - else: - self.fail("TaskGroup should have raised an exception") - - with asyncio.Runner() as r: - loop = r.get_loop() - loop.set_task_factory(self.factory) - r.run(main()) - class TestPyFreeThreading(TestFreeThreading, TestCase): all_tasks = staticmethod(asyncio.tasks._py_all_tasks) diff --git a/Modules/_asynciomodule.c b/Modules/_asynciomodule.c index 0bd6d255b55b158..2852fccf60bb779 100644 --- a/Modules/_asynciomodule.c +++ b/Modules/_asynciomodule.c @@ -3,7 +3,6 @@ #endif #include "Python.h" -#include "pycore_object.h" #include "pycore_critical_section.h" // Py_BEGIN_CRITICAL_SECTION_MUT() #include "pycore_dict.h" // _PyDict_GetItem_KnownHash() #include "pycore_freelist.h" // _Py_FREELIST_POP() @@ -2816,12 +2815,8 @@ TaskObj_dealloc(PyObject *self) { TaskObj *task = (TaskObj *)self; - _PyObject_ResurrectStart(self); - - TaskObj_finalize(task); - - if (_PyObject_ResurrectEnd(self)) { - return; + if (PyObject_CallFinalizerFromDealloc(self) < 0) { + // resurrected. } PyTypeObject *tp = Py_TYPE(task);