Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
kumaraditya303 committed Jan 6, 2025
1 parent 451e6c6 commit e3fb3f3
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 93 deletions.
86 changes: 0 additions & 86 deletions Lib/test/test_asyncio/test_free_threading.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@

threading_helper.requires_working_threading(module=True)

class MyException(Exception):
pass


def tearDownModule():
asyncio._set_event_loop_policy(None)

Expand Down Expand Up @@ -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)
Expand Down
9 changes: 2 additions & 7 deletions Modules/_asynciomodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit e3fb3f3

Please sign in to comment.