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.

Delete Tomcat's temporary directories when the context is closed

See original GitHub issue

Using Spring Boot 1.5.1.

The directory /tmp/tomcat-* are not deleted when stopping a Spring Boot application. The directory is created using (in org.springframework.boot.context.embedded.AbstractEmbeddedServletContainerFactory) :

File tempDir = File.createTempFile(prefix + ".", "." + getPort());
			tempDir.delete();
			tempDir.mkdir();
			tempDir.deleteOnExit();
			return tempDir;

I think that deleteOnExit() method doesn’t work if the directory is not empty. I made a little standard Java test:

public static void main(String[] args) throws IOException, InterruptedException {
		File tempDir = File.createTempFile("prefix", "suffix");
		tempDir.delete();
		tempDir.mkdir();
		tempDir.deleteOnExit();
		
		try (FileOutputStream os = new FileOutputStream(new File(tempDir, "noempty.txt"))) {
			os.write("This a not an empty file".getBytes());
		}
		Thread.sleep(Long.MAX_VALUE);
	}

Then I send a linux signal kill -15 <pid> but the folder ‘prefix*’ is still here.

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:5
  • Comments:21 (11 by maintainers)

github_iconTop GitHub Comments

2reactions
shollandercommented, Apr 16, 2019

@wilkinsona I have found that the shutdown hook created by

https://github.com/spring-projects/spring-boot/blob/b164b16c21734a8581b39553594f102a75bb9738/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/server/AbstractConfigurableWebServerFactory.java#L176

is never effective since there are other directories that get created underneath that top level directory. I am always left with the following directory tree

/tmp
   \- tomcat.123456789.8080
                          \- work
                                \- Tomcat
                                        \- localhost
                                                   \- ROOT

The directory tree is empty, but will not be cleaned up since the top level tomcat.* directory contains an empty directory. Perhaps a better way to do this would be to create a shutdown hook that would remove the entire directory tree if it does not contain any files.

That being said, I am not sure I understand what you mean by

Thanks for the suggestion, but if we always cleared out the whole directory on exit, we’d then get complaints from users who wanted it to remain. For example, users of JSPs may want the directory to remain so that unchanged JSPs don’t need to be recompiled.

The directory created using File.createTempFile() https://github.com/spring-projects/spring-boot/blob/b164b16c21734a8581b39553594f102a75bb9738/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/server/AbstractConfigurableWebServerFactory.java#L173 is guaranteed to be unique and will never be reused after the process exits so it should be safe to force deletion even if the directory does contain files.

2reactions
snicollcommented, Jan 4, 2018

@bbossola server.tomcat.basedir controls the location. We only create a temp dir if that property isn’t specified.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Java Servlet: delete temporary file - tomcat - Stack Overflow
io.tmpdir is global per-JVM, I thought the servlet context attribute gave you a different directory for each webapp (e.g. under $CATALINA_HOME/ ...
Read more >
Tomcat 6: how to delete temporary files after a web method ...
Essentially the datasource allows reading only once. After the stream is closed, the file is deleted. Since the JAX-WS stack only reads the...
Read more >
Cause of many files in Tomcat's temp directory
Sometimes, many files with name starting with "virt" are created in Tomcat's temp directory where JasperReports Server is installed.
Read more >
Apache Tomcat 9 (9.0.70) - Changelog
Fix: Remove and/or update references to the removed org.apache.tomcat.util.threads.res package. The LocalStrings*.properties files in that package were moved to ...
Read more >
Configure Tomcat to use a different temp directory for file ...
The java.io.tmpdir in Tomcat is set to $CATALINA_BASE/temp . You can change it by setting the $CATALINA_TMPDIR environment variable before ...
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