You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
ServerHttpObservationFilter records http.server.requests twice when an async request times out #37627
After onTimeout, the container dispatches or completes the request and then calls onComplete, so a timed-out request is stopped twice. Observation.stop() notifies its handlers each time, and DefaultMeterObservationHandler records the timer each time.
The result:
A DeferredResult that ends by its timeout with a timeout result (a long-poll) is counted twice, both times as 200.
A timeout that ends in a 503 is counted once as 200 (the status when onTimeout runs, before the response is written) and once as 503.
Requests completed by the application, Callable, and an SseEmitter the client closes are counted once.
Reproduction
Spring Boot app with spring-boot-starter-web and spring-boot-starter-actuator, run on its embedded Tomcat:
@RestControllerclassPollController {
@PostMapping("/poll")
DeferredResult<List<String>> poll() {
// Never set, so it always ends by the async timeout.returnnewDeferredResult<>(500L, List.of());
}
}
Setting the same empty result from the application before the timeout records one sample per request.
Suggested fix
Stop the observation only in onComplete.
The Servlet spec has the container complete the request after onTimeout when no listener completes or dispatches it, so onComplete always follows, and by then the response carries its final status.
I independently reproduced this using Spring Boot 3.5.16, Spring Framework 6.2.19, and embedded Tomcat 10.1.55.
In the unmodified framework:
5 timed-out requests produced 10 observations.
3 requests ending in HTTP 503 produced 6 observations, including 3 incorrectly tagged as HTTP 200.
I tested a local candidate that leaves the observation open during onTimeout() and stops it during onComplete().
The expanded local suite now passes 11/11 tests, covering normal responses, timeouts, HTTP 503 and 500 error paths, concurrent requests, delayed completion, and listener behavior. Unhandled errors were correctly recorded under uri=/error.
This is an isolated reproduction and local source override, not a full Spring Framework build or cross-container verification.
Would keeping observation completion exclusively in onComplete() be consistent with the intended servlet lifecycle handling? I can prepare an upstream regression test once the issue has been triaged.
It stops the observation only in onComplete, which the container always calls after onTimeout, so a timed-out request is recorded once and with its final status.
Two tests in ServerHttpObservationFilterTests cover it: a timed-out request is stopped once, and a timeout that ends in 503 is recorded as 503. ./gradlew :spring-web:check passes.
Happy to open a PR against main once this is triaged.
Hey @VinayPolisetti, nice work on this. I independently arrived at the same approach, keeping the observation open during onTimeout() and stopping it in onComplete().
I've tested the change against Spring Framework itself, with the targeted tests and Checkstyle passing. I also added a regression test that explicitly counts observation stops and checks the final 503 status.
One thing I was considering is asserting the stop count directly in the upstream tests, since duplicate completion is the underlying issue. Curious what you think.
Happy to collaborate on the regression coverage once this gets triaged.
Also, outside this issue, what are you currently working on? Always interested in connecting with engineers tackling interesting problems.
Some data from Spring Boot 4.1.2 and 4.2.0-RC2 that may help with triage. On Tomcat 11, stopping only in onComplete would still record these requests twice.
With a plain Spring Boot application (starter-web and starter-actuator), these are the stops of one request:
Container, Spring Boot
DeferredResult that times out (client gets 503)
DeferredResult completed after 50 ms with a 503 ResponseStatusException
Tomcat 11.0.27, 4.2.0-RC2
3: 200 SUCCESS; 503 with AsyncRequestTimeoutException; 503 with uri=/error, error=none
2: 503 with ResponseStatusException; 503 with uri=/error, error=none
Tomcat 11.0.27, 4.1.2
3: 200 SUCCESS; 503; 503 with uri=/error
not measured
Jetty 12.1.14, 4.2.0-RC2
2: 200 SUCCESS; 503 with uri=/error
1: 503 with uri=/error, error=ResponseStatusException
The container callbacks show why. For a request whose response ends in the error page, Tomcat 11 calls onComplete twice: after the async dispatch from AsyncContextImpl.doInternalDispatch, and again from AsyncStateMachine.asyncPostProcess. In between, CoyoteAdapter.asyncDispatch calls onError with no throwable, because the 503 was sent with sendError. ObservationAsyncListener.onError passes that null to Observation.error, which clears the error Spring MVC 7.1 records for the 503, such as the AsyncRequestTimeoutException that DefaultHandlerExceptionResolver records. So this happens without a timeout too, as the right-hand column shows.
So beyond stopping only in onComplete, a fix needs to stop the observation only once and to ignore an error notification without a throwable. #37673 does both, with tests that replay these callback sequences, and on Tomcat 11 and Jetty, with Spring Boot 4.2.0-RC2, each of these requests is then recorded once with its final status and error. It's closed until this is triaged. I'm happy to reopen it then, or to leave the fix to @VinayPolisetti and send the extra cases as tests.
The uri=/error on Jetty has a different cause: the handler mappings set the path pattern during the error dispatch while the observation is still open. I've opened #37675 for it.
Activity
ayodamolami4-bot commentedon Nov 2, 2026
I independently reproduced this using Spring Boot 3.5.16, Spring Framework 6.2.19, and embedded Tomcat 10.1.55.
In the unmodified framework:
I tested a local candidate that leaves the observation open during
onTimeout()and stops it duringonComplete().The expanded local suite now passes 11/11 tests, covering normal responses, timeouts, HTTP 503 and 500 error paths, concurrent requests, delayed completion, and listener behavior. Unhandled errors were correctly recorded under
uri=/error.This is an isolated reproduction and local source override, not a full Spring Framework build or cross-container verification.
Would keeping observation completion exclusively in
onComplete()be consistent with the intended servlet lifecycle handling? I can prepare an upstream regression test once the issue has been triaged.VinayPolisetti commentedon Nov 2, 2026
I have a fix with regression tests on my fork: VinayPolisetti@a1546d7
It stops the observation only in
onComplete, which the container always calls afteronTimeout, so a timed-out request is recorded once and with its final status.Two tests in
ServerHttpObservationFilterTestscover it: a timed-out request is stopped once, and a timeout that ends in 503 is recorded as 503../gradlew :spring-web:checkpasses.Happy to open a PR against
mainonce this is triaged.ayodamolami4-bot commentedon Nov 2, 2026
Hey @VinayPolisetti, nice work on this. I independently arrived at the same approach, keeping the observation open during
onTimeout()and stopping it inonComplete().I've tested the change against Spring Framework itself, with the targeted tests and Checkstyle passing. I also added a regression test that explicitly counts observation stops and checks the final 503 status.
One thing I was considering is asserting the stop count directly in the upstream tests, since duplicate completion is the underlying issue. Curious what you think.
Happy to collaborate on the regression coverage once this gets triaged.
Also, outside this issue, what are you currently working on? Always interested in connecting with engineers tackling interesting problems.
codeconsole commentedon Nov 9, 2026
Some data from Spring Boot 4.1.2 and 4.2.0-RC2 that may help with triage. On Tomcat 11, stopping only in
onCompletewould still record these requests twice.With a plain Spring Boot application (starter-web and starter-actuator), these are the stops of one request:
DeferredResultthat times out (client gets 503)DeferredResultcompleted after 50 ms with a 503ResponseStatusExceptionSUCCESS; 503 withAsyncRequestTimeoutException; 503 withuri=/error,error=noneResponseStatusException; 503 withuri=/error,error=noneSUCCESS; 503; 503 withuri=/errorSUCCESS; 503 withuri=/erroruri=/error,error=ResponseStatusExceptionThe container callbacks show why. For a request whose response ends in the error page, Tomcat 11 calls
onCompletetwice: after the async dispatch fromAsyncContextImpl.doInternalDispatch, and again fromAsyncStateMachine.asyncPostProcess. In between,CoyoteAdapter.asyncDispatchcallsonErrorwith no throwable, because the 503 was sent withsendError.ObservationAsyncListener.onErrorpasses that null toObservation.error, which clears the error Spring MVC 7.1 records for the 503, such as theAsyncRequestTimeoutExceptionthatDefaultHandlerExceptionResolverrecords. So this happens without a timeout too, as the right-hand column shows.So beyond stopping only in
onComplete, a fix needs to stop the observation only once and to ignore an error notification without a throwable. #37673 does both, with tests that replay these callback sequences, and on Tomcat 11 and Jetty, with Spring Boot 4.2.0-RC2, each of these requests is then recorded once with its final status and error. It's closed until this is triaged. I'm happy to reopen it then, or to leave the fix to @VinayPolisetti and send the extra cases as tests.The
uri=/erroron Jetty has a different cause: the handler mappings set the path pattern during the error dispatch while the observation is still open. I've opened #37675 for it.github-actions commentedon Nov 12, 2026
Fixed via d1c8abb
Stop server observation on async completion only