Graceful shutdown of app service on Linux

Ad Gaasbeek 0 Reputation points
2025-01-31T09:11:04.2233333+00:00

I have a Azure App Service (Linux, Java).

When I scale down the number of instances, my application gets killed immediately. However, this leads to transaction errors and database locks. What I want to accomplish is to detect the shutdown, and being able to finish the ongoing work (while not starting any new processes and let my health-check return 503)/

I am able to detect receiving the TERM signal and starting my shutdown process. But Azure does not wait for this to complete, but apparently decides to immediately kill the application.

How can I make Azure to wait killing my application until I say my application is ready for it?

PS When stopping my application, it also does not respect the handling of the TERM signal, and kills immediately.

Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
8,960 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Laxman Reddy Revuri 5,405 Reputation points Microsoft External Staff Moderator
    2025-01-31T12:21:48.9466667+00:00

    Hi @Ad Gaasbeek
    Thanks for the question and using MS Q&A platform.
    Unfortunately, Azure App Service does not provide a built-in mechanism to extend the shutdown timeout beyond the default behavior, which may lead to immediate termination of your application. Therefore, you may need to handle the shutdown logic within your application to minimize the impact of abrupt terminations.

    Additionally for smooth shut down your Azure App Service on Linux, make sure to handle the TERM signal properly. Keep in mind that Azure App Service doesn't automatically wait for your app to finish shutting down before it terminates.

    Set up health probes to manage the app's state during the shutdown. By returning a 503-status code from your health checks when you get a shutdown signal, you can stop new requests while letting ongoing tasks wrap up. But note, Azure might still enforce a timeout period and forcibly terminate your app if it exceeds this period.

    1.Implement signal handling to catch the TERM signal.

    2.Use health probes to show when the app isn't ready for new traffic.

    3.Ensure the app finishes ongoing transactions before the timeout hits.
    references:
    https://learn.microsoft.com/en-us/azure/spring-apps/basic-standard/how-to-configure-health-probes-graceful-termination?tabs=azure-portal#health-probe-properties
    Please accept as "Yes" if the answer provided is useful, so that you can help others in the community looking for remediation for similar issues.


  2. Ad Gaasbeek 0 Reputation points
    2025-02-17T08:06:51.84+00:00

    It seems that I can solve my problem by:

    • setting the environment variable WEBSITES_SHUTDOWN_TIME (can not find it in the documentation)
    • in my @Startup code handle the term signal:
      Signal.handle(new Signal("TERM"), sig -> handleShutdown());
    • in the application properties set quarkus.shutdown.timeout

  3. Ad Gaasbeek 0 Reputation points
    2025-05-26T11:51:32.2066667+00:00

    I asked Senior Cloud Solution Architect from Microsoft, and this was his answer:

    I looked into the methods on how Linux on App Service operates, but generally speaking, as you already attest, it lacks the following:

    Azure App Service for Linux does not guarantee a graceful shutdown during scale-in operations. Specifically:

    • Immediate Termination: During scale-in, Azure may send a SIGTERM, but often follows up with a SIGKILL almost immediately, leaving little to no time for the application to perform cleanup operations.
    • WEBSITES_SHUTDOWN_TIMEOUT Ineffectiveness: Setting the WEBSITES_SHUTDOWN_TIMEOUT environment variable does not reliably extend the shutdown period during scale-in events.
    • Health Probes Limitations: While health probes can indicate application readiness, they do not influence the shutdown behavior during scale-in on Linux App Services.

    Therefore, I would suggest for Linux to look into alternative, and from my point of view much better, solutions, like Container Apps, which gives you an actual control plane for that, which you control.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.