question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Graceful shutdown of signal handlers

See original GitHub issue

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

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
jonathanstaniforthcommented, Apr 24, 2022

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?

0reactions
ahopkinscommented, Jan 20, 2022

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to handle signals with Go to graceful shutdown HTTP ...
Shutdown gracefully shuts down the server without interrupting any active connections. Shutdown works by first closing all open listeners, then ...
Read more >
Signal Handling — Gunicorn 20.1.0 documentation
TERM : Graceful shutdown. Waits for workers to finish their current requests up to the graceful_timeout. HUP : Reload the configuration, start the...
Read more >
Graceful shutdowns on Cloud Run: Deep dive
The graceful termination signal is primarily sent to your application when it's scaling down container instances that are not getting traffic.
Read more >
Implementing Graceful Shutdown in Go
Introducing signal handling · We need to use os.Interrupt to gracefully shutdown on Ctrl+C which is SIGINT · syscall. · Read more about...
Read more >
Ruby Graceful Application Shutdown with SignalException ...
By default in most systems Graceful Shutdown implements by sending SIGTERM signal and 30 seconds delay before terminating the instance. How to ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found