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

spring-projects-issues opened this issue Jun 2, 2017 · 3 comments


Copy link

@spring-projects-issues spring-projects-issues commented Jun 2, 2017

Violeta Georgieva opened SPR-15614 and commented


Currently when an I/O error happens, completeWithError will be called:

At some point this call will result in:
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:
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?


Affects: 5.0 RC1

Issue Links:

  • #20607 WebAsyncManager concurrency issue with SseEmitter when client disconnect
  • #21091 Improve docs and handling of send errors in ResponseBodyEmitter

Referenced from: commits e0678ba

Copy link
Collaborator Author

@spring-projects-issues 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?

Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Jun 13, 2017

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.

Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Jun 23, 2017

Violeta Georgieva commented

Here is the PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants