Delete Tomcat's temporary directories when the context is closed
See original GitHub issueUsing 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:
- Created 6 years ago
- Reactions:5
- Comments:21 (11 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@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
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
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.@bbossola
server.tomcat.basedir
controls the location. We only create a temp dir if that property isn’t specified.