fix(tasks): newly assigned `None` in registry (#2381)

This commit is contained in:
Ryu juheon 2022-02-08 15:33:09 +09:00 committed by GitHub
parent 88bc6d8966
commit 68b654d981
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 3 deletions

View File

@ -1268,10 +1268,9 @@ class Sanic(BaseSanic, RunnerMixin, metaclass=TouchUpMeta):
... ...
def purge_tasks(self): def purge_tasks(self):
for task in self.tasks: for key, task in self._task_registry.items():
if task.done() or task.cancelled(): if task.done() or task.cancelled():
name = task.get_name() self._task_registry[key] = None
self._task_registry[name] = None
self._task_registry = { self._task_registry = {
k: v for k, v in self._task_registry.items() if v is not None k: v for k, v in self._task_registry.items() if v is not None

View File

@ -80,6 +80,18 @@ async def test_purge_tasks(app: Sanic):
assert len(app._task_registry) == 0 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(): def test_shutdown_tasks_on_app_stop():
class TestSanic(Sanic): class TestSanic(Sanic):
shutdown_tasks = Mock() shutdown_tasks = Mock()