Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Graceful shutdown of signal handlers #2369

Open
ahopkins opened this issue Jan 12, 2022 · 5 comments · May be fixed by #2444
Open

Graceful shutdown of signal handlers #2369

ahopkins opened this issue Jan 12, 2022 · 5 comments · May be fixed by #2444
Assignees
Milestone

Comments

@ahopkins
Copy link
Member

When signals are added, we should run some sort of a cleanup like this on shutdown to make sure any custom signals are run. This also includes a suggestion that we identify them with a name: signal-XXXXX.

async def cleanup(app, _):
    for task in asyncio.all_tasks():
        if task.get_name().startswith("signal"):
          await task
@ChihweiLHBird
Copy link
Member

ChihweiLHBird commented Jan 20, 2022

Is this issue somehow related to #1874 since they are both about cleaning in shutdown? And how was #1874 going so far?

@ahopkins
Copy link
Member Author

Yes, exactly. But I still want to name all of our signal tasks. I think that is a good idea.

@ChihweiLHBird
Copy link
Member

Thanks. So, named background tasks clean shutdown was done. Here we would need to clean named signal tasks (asyncio tasks, not background tasks) as well. Is my understanding correct? Do we also need to clean unnamed background tasks in the clean shutdown?

@ahopkins
Copy link
Member Author

asyncio tasks, not background tasks

How are you distinguishing these?

Do we also need to clean unnamed background tasks in the clean shutdown?

I am hesitant to do this because it may or may not be desirable. If anything it should be controllable by config.

@theconsolelogger
Copy link

Hi @ahopkins 👋 , I'm interested in taking this as my first issue! I have some ideas and questions which I will discuss below.

If I followed the flow correctly, I believe the task for signal handlers gets created in the sanic.signals.SignalRouter.dispatch method. In which case, a name could be assigned to the task like so:

task = asyncio.get_running_loop().create_task(dispatch, name="signal")

For the XXXXX part of the signal task's name, what were you thinking of passing here?

For the running of signal tasks on shutdown, I was thinking that a new method could be added to sanic.app.Sanic that's similar to the sanic.app.Sanic.shutdown_tasks method. This method would then be called in the sanic.mixins.runner.RunnerMixin.stop method to run the signal tasks on shutdown. Is this approach similar to what you were looking for?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants