Skip to content

Commit

Permalink
fix(tasks): newly assigned None in registry (#2381)
Browse files Browse the repository at this point in the history
  • Loading branch information
SaidBySolo authored Feb 8, 2022
1 parent 88bc6d8 commit 68b654d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
5 changes: 2 additions & 3 deletions sanic/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -1268,10 +1268,9 @@ async def cancel_task(
...

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
Expand Down
12 changes: 12 additions & 0 deletions tests/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 68b654d

Please sign in to comment.