Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions java/org/apache/catalina/connector/CoyoteAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -238,10 +238,20 @@ public boolean asyncDispatch(org.apache.coyote.Request req, org.apache.coyote.Re
}

if (request.isAsyncDispatching()) {
connector.getService().getContainer().getPipeline().getFirst().invoke(request, response);
if (response.isError()) {
try {
connector.getService().getContainer().getPipeline().getFirst().invoke(request, response);
Throwable t = (Throwable) request.getAttribute(RequestDispatcher.ERROR_EXCEPTION);
asyncConImpl.setErrorState(t, true);
/*
* An error response does not start async error handling if the listeners have already been
* notified that the dispatch completed the async processing, or if it has no exception and the
* dispatch started a new async cycle and ended it with complete() or dispatch() (BZ 70362).
*/
if (response.isError() && !asyncConImpl.isCompletedByDispatch() &&
(t != null || !(request.isAsyncCompleting() || request.isAsyncDispatching()))) {
asyncConImpl.setErrorState(t, true);
}
} finally {
asyncConImpl.fireDeferredOnComplete();
}
}

Expand Down
64 changes: 62 additions & 2 deletions java/org/apache/catalina/core/AsyncContextImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ public class AsyncContextImpl implements AsyncContext, AsyncContextCallback {
private volatile boolean hasOriginalRequestAndResponse = true;
private final AtomicBoolean hasErrorProcessingStarted = new AtomicBoolean(false);
private final AtomicBoolean hasOnErrorReturned = new AtomicBoolean(false);
private volatile boolean completedByDispatch = false;
private volatile boolean completionDeferred = false;

/*
* ThreadLocal for passing over-ridden URL encoding state when dispatching.
Expand Down Expand Up @@ -362,6 +364,8 @@ public void recycle() {
timeout = -1;
hasErrorProcessingStarted.set(false);
hasOnErrorReturned.set(false);
completedByDispatch = false;
completionDeferred = false;
}

private void clearServletRequestResponse() {
Expand Down Expand Up @@ -402,6 +406,8 @@ public void setStarted(Context context, ServletRequest request, ServletResponse
this.servletResponse = response;
this.hasOriginalRequestAndResponse = originalRequestResponse;
this.event = new AsyncEvent(this, request, response);
this.completedByDispatch = false;
this.completionDeferred = false;

List<AsyncListenerWrapper> listenersCopy = new ArrayList<>(listeners);
listeners.clear();
Expand Down Expand Up @@ -444,7 +450,16 @@ protected void doInternalDispatch() throws ServletException, IOException {
dispatch = null;
runnable.run();
if (!request.isAsync()) {
fireOnComplete();
if (isUnreportedFailure()) {
/*
* The dispatch recorded a failure and returned. CoyoteAdapter starts async error handling which
* notifies the listeners of the failure before it completes the async processing.
*/
completionDeferred = true;
} else {
completedByDispatch = true;
fireOnComplete();
}
}
} catch (RuntimeException x) {
AtomicBoolean result = new AtomicBoolean();
Expand All @@ -456,7 +471,16 @@ protected void doInternalDispatch() throws ServletException, IOException {
* information required to determine that onComplete() needs to be called.
*/
if (!result.get()) {
fireOnComplete();
if (hasErrorProcessingStarted.get()) {
completedByDispatch = true;
fireOnComplete();
} else {
/*
* CoyoteAdapter starts async error handling for the exception, which notifies the listeners of it
* before it completes the async processing. If it does not, it calls fireDeferredOnComplete().
*/
completionDeferred = true;
}
}
if (x.getCause() instanceof ServletException) {
throw (ServletException) x.getCause();
Expand All @@ -469,6 +493,40 @@ protected void doInternalDispatch() throws ServletException, IOException {
}


/*
* Has the dispatch recorded a failure (an error response with an exception) that will start async error handling
* once the dispatch returns?
*/
private boolean isUnreportedFailure() {
return !hasErrorProcessingStarted.get() && request.getResponse().isError() &&
request.getAttribute(RequestDispatcher.ERROR_EXCEPTION) != null;
}


/**
* Has the most recent dispatch completed the asynchronous processing? If it has, the listeners have been notified
* that the processing is complete.
*
* @return {@code true} if the most recent dispatch completed the asynchronous processing, otherwise {@code false}
*/
public boolean isCompletedByDispatch() {
return completedByDispatch;
}


/**
* Notifies the listeners that the most recent dispatch completed the asynchronous processing if that notification
* was deferred for async error handling that has not started. Called once the dispatch has returned.
*/
public void fireDeferredOnComplete() {
if (completionDeferred) {
completionDeferred = false;
completedByDispatch = true;
fireOnComplete();
}
}


@Override
public long getTimeout() {
check();
Expand Down Expand Up @@ -509,6 +567,8 @@ public void setErrorState(Throwable t, boolean fireOnError) {
// Skip duplicate error processing
return;
}
// The error processing completes the async processing
completionDeferred = false;
if (t != null) {
request.setAttribute(RequestDispatcher.ERROR_EXCEPTION, t);
}
Expand Down
Loading
Loading