This is an automated email from the ASF dual-hosted git repository.

markt pushed a commit to branch 10.1.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git


The following commit(s) were added to refs/heads/10.1.x by this push:
     new af6e918162 Explicitly clean up after failed multi-part upload
af6e918162 is described below

commit af6e9181620304c0d818121c29c074e1330610d0
Author: Mark Thomas <[email protected]>
AuthorDate: Mon Sep 29 17:50:43 2025 +0100

    Explicitly clean up after failed multi-part upload
---
 .../catalina/connector/LocalStrings.properties      |  1 +
 java/org/apache/catalina/connector/Request.java     | 21 ++++++++++++++++++++-
 webapps/docs/changelog.xml                          |  4 ++++
 3 files changed, 25 insertions(+), 1 deletion(-)

diff --git a/java/org/apache/catalina/connector/LocalStrings.properties 
b/java/org/apache/catalina/connector/LocalStrings.properties
index b3b94d5c1f..2e2c8960d1 100644
--- a/java/org/apache/catalina/connector/LocalStrings.properties
+++ b/java/org/apache/catalina/connector/LocalStrings.properties
@@ -92,6 +92,7 @@ request.asyncNotSupported=A filter or servlet of the current 
chain does not supp
 request.fragmentInDispatchPath=The fragment in dispatch path [{0}] has been 
removed
 request.illegalWrap=The request wrapper must wrap the request obtained from 
getRequest()
 request.notAsync=It is illegal to call this method if the current request is 
not in asynchronous mode (i.e. isAsyncStarted() returns false)
+request.partCleanup.failed=Unable to delete temporary file for uploaded part 
after multi-part processing failed
 request.session.failed=Failed to load session [{0}] due to [{1}]
 
 requestFacade.nullRequest=The request object has been recycled and is no 
longer associated with this facade
diff --git a/java/org/apache/catalina/connector/Request.java 
b/java/org/apache/catalina/connector/Request.java
index 16514e1ba3..e1f0c81b45 100644
--- a/java/org/apache/catalina/connector/Request.java
+++ b/java/org/apache/catalina/connector/Request.java
@@ -2670,8 +2670,9 @@ public class Request implements HttpServletRequest {
             upload.setFileCountMax(partLimit);
 
             parts = new ArrayList<>();
+            List<FileItem> items = null;
             try {
-                List<FileItem> items = upload.parseRequest(new 
ServletRequestContext(this));
+                items = upload.parseRequest(new ServletRequestContext(this));
                 int maxPostSize = getConnector().getMaxPostSize();
                 long postSize = 0;
                 Charset charset = getCharset();
@@ -2724,6 +2725,24 @@ public class Request implements HttpServletRequest {
                 // addParameters() will set parseFailedReason
                 checkSwallowInput();
                 partsParseException = e;
+            } finally {
+                /*
+                 * GC will delete any temporary copies of uploaded files left 
in the work directory but if we know that the
+                 * upload has failed then explicitly clean up now.
+                 */
+                if (!success) {
+                    parts.clear();
+                    if (items != null) {
+                        for (FileItem item : items) {
+                            try {
+                                item.delete();
+                            } catch (Throwable t) {
+                                ExceptionUtils.handleThrowable(t);
+                                
log.warn(sm.getString("request.partCleanup.failed"), t);
+                            }
+                        }
+                    }
+                }
             }
         } finally {
             // This might look odd but is correct. setParseFailedReason() only
diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml
index d79fc5e8b2..e3b88a38a2 100644
--- a/webapps/docs/changelog.xml
+++ b/webapps/docs/changelog.xml
@@ -125,6 +125,10 @@
       <fix>
         Fix a case-sensitivity issue in the trailer header allow list. (markt)
       </fix>
+      <fix>
+        Be proactive in cleaning up temporary files after a failed multi-part
+        upload rather than waiting for GC to do it. (markt)
+      </fix>
     </changelog>
   </subsection>
   <subsection name="Coyote">


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to