From 68b654d98173f66f2a52aec3c846d688aefa0a5b Mon Sep 17 00:00:00 2001 From: Ryu juheon Date: Tue, 8 Feb 2022 15:33:09 +0900 Subject: [PATCH] fix(tasks): newly assigned ``None`` in registry (#2381) --- sanic/app.py | 5 ++--- tests/test_tasks.py | 12 ++++++++++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/sanic/app.py b/sanic/app.py index 5f3a0438..625962a7 100644 --- a/sanic/app.py +++ b/sanic/app.py @@ -1268,10 +1268,9 @@ class Sanic(BaseSanic, RunnerMixin, metaclass=TouchUpMeta): ... def purge_tasks(self): - for task in self.tasks: + for key, task in self._task_registry.items(): if task.done() or task.cancelled(): - name = task.get_name() - self._task_registry[name] = None + self._task_registry[key] = None self._task_registry = { k: v for k, v in self._task_registry.items() if v is not None diff --git a/tests/test_tasks.py b/tests/test_tasks.py index 63de50af..8396a9f9 100644 --- a/tests/test_tasks.py +++ b/tests/test_tasks.py @@ -80,6 +80,18 @@ async def test_purge_tasks(app: Sanic): assert len(app._task_registry) == 0 +async def test_purge_tasks_with_create_task(app: Sanic): + app.add_task(asyncio.create_task(dummy(3)), name="dummy") + + await app.cancel_task("dummy") + + assert len(app._task_registry) == 1 + + app.purge_tasks() + + assert len(app._task_registry) == 0 + + def test_shutdown_tasks_on_app_stop(): class TestSanic(Sanic): shutdown_tasks = Mock()