Skip to content

ServerHttpObservationFilter reports the container's ServletException for failed async requests #37731

Closed

Description

@codeconsole

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:

  1. The filter's catch block, during the ASYNC dispatch.
  2. AsyncContextImpl.setErrorState → onError. CoyoteAdapter.asyncDispatch calls it when ERROR_EXCEPTION is set after the dispatch.
  3. onComplete → recordError, which reads the ServletException from ERROR_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

  1. added
    status: waiting-for-triage
    on Nov 20, 2026
  2. self-assigned this
    on Nov 20, 2026
  3. added
    in: web
    type: bug
    and removed
    status: waiting-for-triage
    on Nov 20, 2026
  4. added this to the 7.1.1 milestone on Nov 20, 2026
  5. bclozel commented on Nov 20, 2026

    @bclozel
    Member

    Thanks for the report. We'll fix the ServletException regression here (the onComplete callback 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 calls onComplete, then onError(null) after completion, then onComplete again. This doesn't seem to match the AsyncListener contract. 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.

  6. changed the title [-]`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[/+] on Nov 20, 2026
  7. codeconsole commented on Nov 20, 2026

    @codeconsole
    Author

    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 after sendError.

  8. added a commit that references this issue on Nov 20, 2026
    088fb71
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

in: webtype: bug

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions