Apache Tomcat 10.1 Unable to delete temporary files

6.4k Views Asked by At

We just upgraded Tomcat from 9.0 to 10.1, and Java from 8 to 11. everything is running fine except now we are having temp files pile up in Apache Software Foundation\Tomcat 10.1\work\Catalina\localhost\root. This is causing 502 errors after Java heap space issues. We see errors like this..

07-Feb-2023 23:33:11.414 SEVERE [http-nio-81-exec-3] org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun Error running socket processor
    java.io.UncheckedIOException: Cannot delete C:\Program Files\Apache Software Foundation\Tomcat 10.1\work\Catalina\localhost\root\upload_ac88107d_733f_4b9d_a3a3_9274a46f92e3_00000509.tmp
        at org.apache.tomcat.util.http.fileupload.disk.DiskFileItem.delete(DiskFileItem.java:429)
        at org.apache.catalina.core.ApplicationPart.delete(ApplicationPart.java:54)
        at org.apache.catalina.connector.Request.recycle(Request.java:451)
        at org.apache.catalina.connector.CoyoteAdapter.log(CoyoteAdapter.java:512)
        at org.apache.catalina.connector.CoyoteAdapter.checkRecycled(CoyoteAdapter.java:536)
        at org.apache.coyote.http11.Http11Processor.recycle(Http11Processor.java:1440)
        at org.apache.coyote.AbstractProtocol$ConnectionHandler.release(AbstractProtocol.java:1052)
        at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:1024)
        at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1734)
        at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:52)
        at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191)
        at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659)
        at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
        at java.base/java.lang.Thread.run(Thread.java:834)
    Caused by: java.io.IOException: Cannot delete C:\Program Files\Apache Software Foundation\Tomcat 10.1\work\Catalina\localhost\root\upload_ac88107d_733f_4b9d_a3a3_9274a46f92e3_00000509.tmp
        ... 14 more

Any ideas what could be the issue?

1

There are 1 best solutions below

0
Satyaprasanna Dash On

It comes from the file handling section of your code. the code working fine for tomcat 8.0.50 but in higher version it throws error like : Failed to deleted temporary file .

There are 2 ways to fix the issue :

  1. Don't use byte[] type arg while reading and writing data. the correct way to read data from input file is :
try (FileOutputStream fos = new FileOutputStream(new File(newFileName));
    InputStream is = file.getInputstream()) {
    int byteValue;
    while ((byteValue = is.read()) != -1) { // don't use: is.read(byte[] arg)
        fos.write(byteValue);  // don't use: fos.write(byte[] arg, 0, byteValue)
    }
} catch (IOException e) {
    e.printStackTrace();
} 

NOTE : it will take some time for processing files, but it will not throw any error.

  1. Revert back to old tomcat version to use byte[].