Skip to content

Commit

Permalink
DEBUG/DROPME: Mac, what doing?
Browse files Browse the repository at this point in the history
  • Loading branch information
qstokkink committed Nov 25, 2024
1 parent 3ce7030 commit a95d031
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 14 deletions.
41 changes: 29 additions & 12 deletions ipv8/taskmanager.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import asyncio
import logging
import time
import traceback
Expand Down Expand Up @@ -225,6 +226,7 @@ async def register_shutdown_task(self, basename: str, task: Callable | Coroutine
Register a task to be run when this manager is shut down.
"""
done_future: Future[bool] = Future()
finished_future: Future[bool] = Future()

async def catch_shutdown() -> None:
"""
Expand All @@ -243,22 +245,34 @@ async def after_cancel() -> None:
"""
This is a registered but uncancellable task. It will be awaited but cannot detect cancels.
"""
run_callback = await done_future
if run_callback:
fut = task
if callable(task):
fut = task(*args, **kwargs)
if not callable(task) or iscoroutinefunction(task):
await cast(Awaitable, fut)

done_future.after_cancel_task = after_cancel() # type: ignore[attr-defined]
print("AWAIT CANCEL")
try:
run_callback = await done_future
print("CALL CANCEL")
if run_callback:
fut = task
if callable(task):
fut = task(*args, **kwargs)
if not callable(task) or iscoroutinefunction(task):
await cast(Awaitable, fut)
finally:
print("FINISH CANCEL")
finished_future.set_result(True)

async def post_shutdown():
await finished_future

finished_future.after_cancel_task = after_cancel() # type: ignore[attr-defined]
finished_future.post_shutdown = post_shutdown() # type: ignore[attr-defined]
self.register_anonymous_task(f"[Catch shutdown] {basename}", catch_shutdown)
self.register_anonymous_task(f"[Run shutdown] {basename}",
shield(done_future.after_cancel_task)) # type: ignore[attr-defined]
self.register_anonymous_task(f"[Run shutdown] {basename}",
shield(finished_future.after_cancel_task)) # type: ignore[attr-defined]
self.register_anonymous_task(f"[Run shutdown] {basename}",
shield(finished_future.post_shutdown)) # type: ignore[attr-defined]

await sleep(0) # Enter both infinite loops

return done_future
return finished_future

def cancel_pending_task(self, name: Hashable) -> Future:
"""
Expand Down Expand Up @@ -333,6 +347,9 @@ async def shutdown_task_manager(self) -> None:
if tasks:
with suppress(CancelledError):
await gather(*tasks)
for task in tasks: # TODO: Mac exits ``gather`` on ``cancelling``, not ``cancelled``.
while not task.done():
await sleep(0)


__all__ = ["TaskManager", "task"]
12 changes: 10 additions & 2 deletions ipv8/test/test_taskmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,13 @@ async def test_delayed_looping_call_register_wait_and_cancel(self) -> None:
self.assertTrue(task1.cancelled())
self.assertTrue(task2.cancelled())

def test_raise_on_duplicate_task_name(self) -> None:
async def test_raise_on_duplicate_task_name(self) -> None:
"""
Check that a normal register task cannot be used as a replace task.
"""
self.tm.register_task("test", lambda: None)
self.tm.register_task("test", lambda: None, delay=5)
await sleep(0)

with self.assertRaises(RuntimeError):
self.tm.register_task("test", lambda: None)

Expand Down Expand Up @@ -300,6 +302,12 @@ async def test_register_shutdown_task(self) -> None:
fut = await self.tm.register_shutdown_task("test", sub_manager.shutdown_task_manager)

await self.tm.shutdown_task_manager()
await fut

print(fut)
print()
print("Shutdown state sub manager", sub_manager._shutdown)
print(">", [str(task) for task in sub_manager.get_tasks()])

self.assertTrue(fut.done())
self.assertTrue(sub_fut.cancelled())
Expand Down

0 comments on commit a95d031

Please sign in to comment.