sanic-org / sanic

Accelerate your web app development | Build fast. Run fast.

Home Page:https://sanic.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Issue with identical named tasks

cnicodeme opened this issue · comments

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

I'm trying to debug an issue on my code and realized an issue that's worth mentioning that affect Sanic (even though I think it's more a decision from Python, but a discussion is important here).

It is possible to add multiple tasks with the same name. When canceling a task by its given name, only the recently added one will be stopped

Take the following code sample as an example:

async def some_task():
    uid = str(uuid.uuid4())
    print('Starting task {}'.format(uid))
    for i in range(0, 5):
        print('Task {} - {}'.format(uid, i))
        await asyncio.sleep(1)
    print('Finished task {}'.format(uid))

counter = 0

@app.get('/test')
async def trigger(request):
    global counter
    if counter > 2:
        print('Killing task')
        await request.app.cancel_task('async_test_task')

    request.app.add_task(task(), name="async_test_task")
    counter += 1
    return text('ok')

Refreshing the /test page 5 times will show something like:

Starting task 3458a72a-b173-48de-a6a9-854e49f4d240
Task 3458a72a-b173-48de-a6a9-854e49f4d240 - 0
Task 3458a72a-b173-48de-a6a9-854e49f4d240 - 1
Starting task 273f1368-29e2-4cb3-ace3-fea1fa6b81b3
Task 273f1368-29e2-4cb3-ace3-fea1fa6b81b3 - 0
Task 3458a72a-b173-48de-a6a9-854e49f4d240 - 2
Starting task 35793ea7-58b0-4405-808c-c3a5716d130b
Task 35793ea7-58b0-4405-808c-c3a5716d130b - 0
Task 273f1368-29e2-4cb3-ace3-fea1fa6b81b3 - 1
Task 3458a72a-b173-48de-a6a9-854e49f4d240 - 3
Killing task
Starting task 5a2aee75-695a-4dbf-8f85-71998c5d5c00
Task 5a2aee75-695a-4dbf-8f85-71998c5d5c00 - 0
Task 273f1368-29e2-4cb3-ace3-fea1fa6b81b3 - 2
Task 3458a72a-b173-48de-a6a9-854e49f4d240 - 4
Task 5a2aee75-695a-4dbf-8f85-71998c5d5c00 - 1
Task 273f1368-29e2-4cb3-ace3-fea1fa6b81b3 - 3
Finished task 3458a72a-b173-48de-a6a9-854e49f4d240
Task 5a2aee75-695a-4dbf-8f85-71998c5d5c00 - 2
Task 273f1368-29e2-4cb3-ace3-fea1fa6b81b3 - 4
Killing task
Starting task 0f106620-edf3-435e-a5af-e28a76696e7c
Task 0f106620-edf3-435e-a5af-e28a76696e7c - 0
Finished task 273f1368-29e2-4cb3-ace3-fea1fa6b81b3
Task 0f106620-edf3-435e-a5af-e28a76696e7c - 1
Task 0f106620-edf3-435e-a5af-e28a76696e7c - 2
Task 0f106620-edf3-435e-a5af-e28a76696e7c - 3
Task 0f106620-edf3-435e-a5af-e28a76696e7c - 4
Finished task 0f106620-edf3-435e-a5af-e28a76696e7c

When calling "killing task", only the most recently task is stopped ("35793ea7-58b0-4405-808c-c3a5716d130b"). The first two ("3458a72a-b173-48de-a6a9-854e49f4d240" and "273f1368-29e2-4cb3-ace3-fea1fa6b81b3") aren't.

This is not an issue with Sanic directly as it is known in the Python env (https://stackoverflow.com/questions/68147088/avoid-naming-different-asyncio-tasks-with-the-same-variable-to-prevent-unstoppab for instance) but could cause some issues of never ending tasks if a task is created multiple time with the same name.

Here's my suggestion: it would be better to notify the developer when a task with the same name is created. A warning is good, a parameter to raise (or not) in case of conflict is interesting too.

What do you think?

Code snippet

No response

Expected Behavior

No response

How do you run Sanic?

As a script (app.run or Sanic.serve)

Operating System

Debian and Fedora

Sanic Version

23.0.1

Additional context

No response

Have you raised this at all in cPython?

No, I had under the assumption coming from the Stackoverflow link that this was a known issue.