Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix AsyncContext to be completed on AsyncListener.onError() #31953

Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@ public void onTimeout(AsyncEvent event) throws IOException {

@Override
public void onError(AsyncEvent event) throws IOException {
if (this.asyncContext != null) {
this.asyncContext.complete();
}
this.exceptionHandlers.forEach(consumer -> consumer.accept(event.getThrowable()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@

package org.springframework.web.context.request.async;

import java.io.IOException;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;

import jakarta.servlet.AsyncEvent;
import jakarta.servlet.AsyncListener;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -124,6 +127,36 @@ public void onErrorHandler() throws Exception {
verify(errorHandler).accept(e);
}

@Test
public void onErrorShouldCompleteAsyncContext() throws Exception {
this.asyncRequest.startAsync();
AtomicInteger completeCounter = new AtomicInteger();
MockAsyncContext context = (MockAsyncContext) this.request.getAsyncContext();
context.addListener(new AsyncListener() {
@Override
public void onComplete(AsyncEvent asyncEvent) throws IOException {
completeCounter.incrementAndGet();
}

@Override
public void onTimeout(AsyncEvent asyncEvent) throws IOException {

}

@Override
public void onError(AsyncEvent asyncEvent) throws IOException {

}

@Override
public void onStartAsync(AsyncEvent asyncEvent) throws IOException {

}
});
this.asyncRequest.onError(new AsyncEvent(new MockAsyncContext(this.request, this.response), new Exception()));
assertThat(completeCounter.get()).isOne();
}

@Test
public void setTimeoutDuringConcurrentHandling() {
this.asyncRequest.startAsync();
Expand Down