Skip to content

Commit 4019c87

Browse files
committed
Fix dispatch order issue in async request handling
Ensure post-processing logic is executed before re-dispatching during asynchronous request handling * SpringBootLambdaContainerHandler - Added logic within the handleRequest method to reprocess the request in cases where an asynchronous request requires re-dispatching. * AwsAsyncContext - Added an isDispatchStarted method that returns whether the dispatch has started or not. - Removed the part where doFilter is directly called within the dispatch function.
1 parent 7e07246 commit 4019c87

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

aws-serverless-java-container-core/src/main/java/com/amazonaws/serverless/proxy/internal/servlet/AwsAsyncContext.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public class AwsAsyncContext implements AsyncContext {
3737
private long timeout;
3838
private AtomicBoolean dispatched;
3939
private AtomicBoolean completed;
40+
private AtomicBoolean dispatchStarted;
4041

4142
private Logger log = LoggerFactory.getLogger(AwsAsyncContext.class);
4243

@@ -49,6 +50,7 @@ public AwsAsyncContext(HttpServletRequest request, HttpServletResponse response,
4950
timeout = 3000;
5051
dispatched = new AtomicBoolean(false);
5152
completed = new AtomicBoolean(false);
53+
dispatchStarted = new AtomicBoolean(false);
5254
}
5355

5456
@Override
@@ -68,16 +70,15 @@ public boolean hasOriginalRequestAndResponse() {
6870

6971
@Override
7072
public void dispatch() {
71-
try {
72-
log.debug("Dispatching request");
73-
if (dispatched.get()) {
74-
throw new IllegalStateException("Dispatching already started");
75-
}
73+
log.debug("Dispatching request");
74+
if (dispatched.get()) {
75+
throw new IllegalStateException("Dispatching already started");
76+
}
77+
if (!dispatchStarted.get()) {
78+
dispatchStarted.set(true);
79+
} else {
7680
dispatched.set(true);
77-
handler.doFilter(req, res, ((AwsServletContext)req.getServletContext()).getServletForPath(req.getRequestURI()));
7881
notifyListeners(NotificationType.START_ASYNC, null);
79-
} catch (ServletException | IOException e) {
80-
notifyListeners(NotificationType.ERROR, e);
8182
}
8283
}
8384

@@ -154,6 +155,10 @@ public boolean isCompleted() {
154155
return completed.get();
155156
}
156157

158+
public boolean isDispatchStarted() {
159+
return dispatchStarted.get();
160+
}
161+
157162
private void notifyListeners(NotificationType type, Throwable t) {
158163
listeners.forEach((h) -> {
159164
try {

aws-serverless-java-container-springboot3/src/main/java/com/amazonaws/serverless/proxy/spring/SpringBootLambdaContainerHandler.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.amazonaws.serverless.proxy.spring.embedded.ServerlessReactiveServletEmbeddedServerFactory;
2323
import com.amazonaws.serverless.proxy.spring.embedded.ServerlessServletEmbeddedServerFactory;
2424
import com.amazonaws.services.lambda.runtime.Context;
25+
import jakarta.servlet.AsyncContext;
2526
import org.slf4j.Logger;
2627
import org.slf4j.LoggerFactory;
2728
import org.springframework.boot.WebApplicationType;
@@ -172,9 +173,21 @@ protected void handleRequest(HttpServletRequest containerRequest, AwsHttpServlet
172173
((AwsHttpServletRequest)containerRequest).setResponse(containerResponse);
173174
}
174175
doFilter(containerRequest, containerResponse, reqServlet);
176+
if(requiresAsyncReDispatch(containerRequest)) {
177+
doFilter(containerRequest, containerResponse, reqServlet);
178+
}
175179
Timer.stop("SPRINGBOOT2_HANDLE_REQUEST");
176180
}
177181

182+
private boolean requiresAsyncReDispatch(HttpServletRequest request) {
183+
if (request.isAsyncStarted()) {
184+
AsyncContext asyncContext = request.getAsyncContext();
185+
return asyncContext instanceof AwsAsyncContext
186+
&& ((AwsAsyncContext) asyncContext).isDispatchStarted();
187+
}
188+
return false;
189+
}
190+
178191

179192
@Override
180193
public void initialize()

0 commit comments

Comments
 (0)