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

Handle onError events that are sent by the servlet containers [SPR-15614] #20173

Closed
spring-projects-issues opened this issue Jun 2, 2017 · 3 comments
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues commented Jun 2, 2017

Violeta Georgieva opened SPR-15614 and commented

Hi,

Currently when an I/O error happens, completeWithError will be called:
https://github.com/spring-projects/spring-framework/blob/master/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ResponseBodyEmitter.java#L172

At some point this call will result in:
https://github.com/spring-projects/spring-framework/blob/master/spring-web/src/main/java/org/springframework/web/context/request/async/WebAsyncManager.java#L357
Thus the implementation guarantees that the I/O error will be handled properly and the corresponding error handlers will be invoked.

On the other hand there is AsyncListener that will be notified by the servlet containers that an error occurred:
https://github.com/spring-projects/spring-framework/blob/master/spring-web/src/main/java/org/springframework/web/context/request/async/StandardServletAsyncWebRequest.java#L137
This does not trigger any error handling but just completes the async operation.

When the error is I/O then this is not a problem as the implementation handles it as described above, but if it is another type of error then it will not be handled properly.

What do you think?

Regards,
Violeta


Affects: 5.0 RC1

Issue Links:

Referenced from: commits e0678ba

@spring-projects-issues
Copy link
Collaborator Author

spring-projects-issues commented Jun 5, 2017

Rossen Stoyanchev commented

Taking a closer look, as far as I can see, if an onError AsyncEvent occurs when ResponseBodyEmitter is not trying to write, the AsyncListener in StandardServletAsyncWebRequest will result in a call to the onCompletion handler of ResponseBodyEmitter.

So the ResponseBodyEmitter will be notified at least and from what I can see after that Tomcat will complete the AsyncContext if neither dispatch nor complete where called.

Overall AsyncListener#onError is not exposed explicitly but treated as a completion callback instead. This the case of all the higher-level APIs like DeferredResult, DeferredResultProcessInterceptor, and ResponseBodyEmitter. The idea is that after a container-level exception the connection is practically over with. We just don't explicitly complete from onError but rather leave it to those higher level APIs (like DeferredResult) or to the container (in case of an error unrelated to writing) to do so. I also wonder what error can arise in the container prompting an onError but in any case I imagine that most often errors are related to attempts to write to the response.

So unless leaving it to the container to is an issue for, as it became with timeouts when containers started dispatching even if response status was set (#19233), then I don't see a strong reason for change. What do you think?

@spring-projects-issues
Copy link
Collaborator Author

Rossen Stoyanchev commented

After some more discussion and experiments (including on the Tomcat mailing list) it's clear that both onTimeout and onError notifications should only be handled from within a container thread, i.e. through the AsyncListener callbacks.

In case of a write error, the server is clearly aware of it and should turn it into a notification. So we can refrain from catching the write exception and calling SseEmitter.completeWithError(ex) and instead wait for the onError callback.

In order to do that we'll need addErrorHandler in StandardServletAsyncWebRequest next to similar options for timeout and complete. Then SseEmitter can register and call this::completeWithError from within the container thread.

@spring-projects-issues
Copy link
Collaborator Author

Violeta Georgieva commented

Here is the PR
#1463

@spring-projects-issues spring-projects-issues added type: enhancement A general enhancement in: web Issues in web modules (web, webmvc, webflux, websocket) labels Jan 11, 2019
@spring-projects-issues spring-projects-issues added this to the 5.0 RC3 milestone Jan 11, 2019
bclozel added a commit that referenced this issue May 13, 2024
Prior to this commit, the `ReactiveTypeHandler` would handle `Flux`-like
return types from controller methods and adapt them to SSE streams using
the `SseEmitter`/`ResponseBodyEmitter` APIs. In case an `IOException` is
thrown while writing to the HTTP response stream, the
`ReactiveTypeHandler` would rely on the Servlet container to call
`AsyncListener#onError` - this would be the signal for Spring MVC to
complete the async exchange. To prevent racing issues between this
signal and the actual handling of the exception, changes like  gh-20173
were applied. Since then, robust checks were added with gh-32340 in
`StandardServletAsyncWebRequest.LifecycleHttpServletResponse`.

With Jetty 12, `AsyncListener#onError` would not be called as the error
would happen while writing in blocking mode to the response (so, not
using the Servlet WriteListener contract). But still, such `IOException`
would still result in the closing of the HTTP connection. As of Jetty
12.0.4, this is no longer the case and the party managing the async
lifecycle is in charge of completing the exchange, as it should. This
means that the current behavior leaks HTTP connections for these cases
and causes memory issues.

This commit ensures that such exceptions happening during response
writes are caught and result in the completion of the `SSEEmitter` and
the closing of the exchange. Even if other Servlet containers still
propagate the error `AsyncListener#onError`, competing signals are still
managed with gh-32340.

Closes gh-32629
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

2 participants