Repository navigation
Description
Affects: Spring Framework 7.1.0 (Spring Boot 4.2.0), Tomcat 11.0.27 and Jetty 12 (ee11)
In 7.1.0, ObservationAsyncListener.onComplete calls recordError before it stops the observation (gh-37658). The filter's catch block and onError already report an exception that escapes an async request. As a result, the same failure, which Tomcat already reported twice in RC2, now reaches Observation.error, and every ObservationHandler.onError (tracing included), two or three times. The value onComplete reads from jakarta.servlet.error.exception is the container's ServletException wrapper, so the exception key value changes from the root cause to ServletException.
Minimal reproduction: a Spring Boot 4.2.0 app with spring-boot-starter-webmvc and spring-boot-starter-actuator, plus an ObservationHandler<ServerRequestObservationContext> that counts onError and onStop:
@GetMapping("/callable-fails")
Callable<String> callableFails() {
return () -> { throw new IllegalStateException("callable"); };
}| endpoint | container | 7.1.0-RC2 | 7.1.0 |
|---|---|---|---|
| Callable that throws (no handler) | Tomcat | onError ×2, exception=IllegalStateException | onError ×3, exception=ServletException |
| Callable that throws (no handler) | Jetty | onError ×1, exception=IllegalStateException | onError ×2, exception=ServletException |
DeferredResult#setErrorResult (no handler) |
Tomcat / Jetty | same as above | same as above |
servlet that calls startAsync() then throws |
Tomcat / Jetty | onError ×2 | onError ×3 |
DeferredResult timeout (503) |
Tomcat | stopped ×3, onError(null), exception=none | stopped ×2, onError ×3 (incl. null) |
Where the calls come from, on Tomcat, for the Callable case:
- The filter's
catchblock, during theASYNCdispatch. AsyncContextImpl.setErrorState→onError.CoyoteAdapter.asyncDispatchcalls it whenERROR_EXCEPTIONis set after the dispatch.onComplete→recordError, which reads theServletExceptionfromERROR_EXCEPTION.
For the timeout, and for any async dispatch that returns normally after sendError (a 404 ResponseStatusException from a Callable does it too), Tomcat fires onComplete from AsyncContextImpl.doInternalDispatch. Because response.isError() is true, CoyoteAdapter.asyncDispatch then calls setErrorState(null, true), and onComplete fires again from AbstractProcessor.asyncPostProcess. gh-37627 made onComplete the only place that stops, but the listener still stops the observation each time onComplete fires.
Synchronous failures are reported once on both containers, in 7.1.0 and RC2.
A fix with tests is ready on a fork branch, codeconsole/spring-framework@fix/observation-async-failure-once: report an error only when the filter hasn't reported it for the request already, unwrap the ServletException read from the request attribute, ignore onError without an exception or after completion, and stop on the first onComplete only. With that patch, every case above is reported once, with the root-cause exception value, and stops once, on both containers. I can open a PR once this is triaged.
Related: gh-37627 fixed the timeout being recorded twice by stopping only in onComplete. As reported there before it was closed, Tomcat 11 still completes the request a second time and calls onError without an exception. gh-37673, declined because gh-37627 wasn't triaged yet, guarded against both; the fix branch above carries those guards forward and adds the de-duplication 7.1.0 now needs.
Activity
bclozel commentedon Nov 20, 2026
Thanks for the report. We'll fix the
ServletExceptionregression here (theonCompletecallback reads "jakarta.servlet.error.exception" again). No need for contributions on that one.The rest of the report is Tomcat behavior. If I understand correctly, when an async dispatch ends with
sendError, Tomcat 11 callsonComplete, thenonError(null)after completion, thenonCompleteagain. This doesn't seem to match theAsyncListenercontract. We won't work around it in Spring.This is the second PR (after #37673) that patches over a container problem. Please stop sending those. Report this to Tomcat with a repro that uses Servlet only. If they say it's intended, please link their answer here so we can decide what to do.
[-]`ServerHttpObservationFilter` reports a failed async request's exception more than once in 7.1.0[/-][+]ServerHttpObservationFilter reports the container's ServletException for failed async requests[/+]codeconsole commentedon Nov 20, 2026
Thanks. Reported to Tomcat with a Servlet-only reproduction: https://bz.apache.org/bugzilla/show_bug.cgi?id=70362
It's a regression from Tomcat's fix for bug 66875: 10.1.15 delivers
[onComplete], and 10.1.16 and later (including 11.0.29) deliver[onComplete, onError(null), onComplete]when an async dispatch ends aftersendError.Ignore container error attribute on async completion
Note what Spring Framework 7.1.1 makes stale in ServerErrorObservation