DEV Community

Renato Fialho
Renato Fialho

Posted on

Handling SIGTERM and SIGKILL on Spring Boot web applications

This post was reproduced here as well

When debugging one system recently, I found out that every time that a deploy, downscale, container rotation or k8s updates happened I had a lot of connection errors in the ingress car.

In common on those situations, is that k8s sends a SIGTERM, then have a grace period for the application to handle it gracefully, and after N seconds, it sends a SIGKILL, and that is the end.

At first, being a little bit reluctant that the issue was with Spring Boot or the web server that I run with (Tomcat), I tried to check every possible misbehave from the load balancer, no success.

This is usually how the service infra works

One example on how this works, but mainly from AWS ECS:

AWS ECS ELB Container Draining

Then, after some digging into the server properties in Spring Boot, we found out that the common behaviour for the web server is to shutdown immediately after receiving a SIGTERM, not waiting for any persistentkeep-alive connections to be properly finished and closed.

And how did I fix this?

So, in order to fix that, you must add a line to your application.properties or application.yml file like this:



    server.shutdown=graceful


Enter fullscreen mode Exit fullscreen mode

OR



server:
    shutdown: graceful


Enter fullscreen mode Exit fullscreen mode

Passing graceful to the server shutdown property allows the web server to support graceful shutdown, allowing active requests time to complete and close any persistent keep-alive connections towards your service.

Oh, and if in any case you ended up here but want to know more how Quarkus handles this, i suggest you to take a look here, because it explains in an easy way how to implement this on Quarkus.

Top comments (0)