From 152914a752d399acf8d51ed9ad0700fc91973461 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Basl=C3=A9?= Date: Thu, 11 Jul 2024 16:39:12 +0200 Subject: [PATCH] Fix multipart async servlet request temporary files deletion This change tracks the multipart nature of the async request within the `DispatcherServlet`, in the `WebAsyncManager`. This allows for the second ASYNC dispatch to recognize the multipart aspect and clean up the associated resources. Closes gh-33161 --- .../request/async/WebAsyncManager.java | 20 +++++++++++++++++++ .../web/servlet/DispatcherServlet.java | 3 ++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/spring-web/src/main/java/org/springframework/web/context/request/async/WebAsyncManager.java b/spring-web/src/main/java/org/springframework/web/context/request/async/WebAsyncManager.java index d4f0dfe3fa..1cfdeead2f 100644 --- a/spring-web/src/main/java/org/springframework/web/context/request/async/WebAsyncManager.java +++ b/spring-web/src/main/java/org/springframework/web/context/request/async/WebAsyncManager.java @@ -79,6 +79,8 @@ public final class WebAsyncManager { private AsyncTaskExecutor taskExecutor = DEFAULT_TASK_EXECUTOR; + private boolean isMultipartRequestParsed; + @Nullable private volatile Object concurrentResult = RESULT_NONE; @@ -242,6 +244,24 @@ public final class WebAsyncManager { } } + /** + * Mark the {@link WebAsyncManager} as wrapping a + * multipart async request. + * @since 6.1.12 + */ + public void setMultipartRequestParsed(boolean isMultipart) { + this.isMultipartRequestParsed = isMultipart; + } + + /** + * Return {@code true} if this {@link WebAsyncManager} was previously marked + * as wrapping a multipart async request, {@code false} otherwise. + * @since 6.1.12 + */ + public boolean isMultipartRequestParsed() { + return this.isMultipartRequestParsed; + } + /** * Clear {@linkplain #getConcurrentResult() concurrentResult} and * {@linkplain #getConcurrentResultContext() concurrentResultContext}. diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/DispatcherServlet.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/DispatcherServlet.java index d7542ae6a9..79699ad25b 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/DispatcherServlet.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/DispatcherServlet.java @@ -1118,10 +1118,11 @@ public class DispatcherServlet extends FrameworkServlet { if (mappedHandler != null) { mappedHandler.applyAfterConcurrentHandlingStarted(processedRequest, response); } + asyncManager.setMultipartRequestParsed(multipartRequestParsed); } else { // Clean up any resources used by a multipart request. - if (multipartRequestParsed) { + if (multipartRequestParsed || asyncManager.isMultipartRequestParsed()) { cleanupMultipart(processedRequest); } }