…context is closed #9983

The directory /tmp/tomcat-* are not deleted when stopping a Spring Boot application. This PR provide a solution with backward compatibility

  • Add a parameter server.tmp-deletion-strategy with 3 possible value :
  • DELETE_ON_EXIT : The default strategy. when the context is shutdown, the created temporary directory is deleted. If the temporary directory is not empty, then it will not be deleted.
  • RECURSIVE_DELETE : When the context is shutdown, then created temporary directory is deleted with all its content.
  • NOTHING : When the context is shutdown, we keep all the created temporary directory. With this configuration, if you need to remove the directory, you need to do it manually or with a cron task.
  • Use this parameter when the Servlet needs to create a temporary directory

by default, the behaviour is the same. but if you want to delete everything, you need to add in your application.properties ;

server.tmp-deletion-strategy=RECURSIVE_DELETE

NB : this is a proposal to use Tomcat/Jetty/Undertow solutions to delete the tmp dirs after the server is stopped

Comment From: wilkinsona

Thanks for your enthusiasm to contribute, @CharlesLgn, but I'm afraid we're not in a position to review and potentially accept a contribution for this. The core team needs to do some design work for #9983 first. I also note that you're still using a shutdown hook in this proposal. As already explained in https://github.com/spring-projects/spring-boot/pull/42852, that's one approach that we know we definitely cannot use as it will result in a race condition.

Comment From: CharlesLgn

I'm afraid we're not in a position to review and potentially accept a contribution for this. The core team needs to do some design work for #9983 first.

No worry, the code written here is more like a POC so you'll have some solution pre-written.

I also note that you're still using a shutdown hook in this proposal. As already explained in #42852, that's one approach that we know we definitely cannot use as it will result in a race condition.

File#deleteOnExit put already the file in a shutdown hook

    public void deleteOnExit() {
        @SuppressWarnings("removal")
        SecurityManager security = System.getSecurityManager();
        if (security != null) {
            security.checkDelete(path);
        }
        if (isInvalid()) {
            return;
        }
        DeleteOnExitHook.add(path);
    }

so... we already have the race condition.

Moreovere, if you look at the code, you will see that Tomcat do not use the Shutdown Hook but something in its lifecycle