Skip to content

ServerHttpObservationFilter records http.server.requests twice when an async request times out #37627

Closed

Description

@VinayPolisetti

ServerHttpObservationFilter records http.server.requests twice when an async request times out.

Affects: spring-web 6.2.19 (Spring Boot 3.5.16, Micrometer 1.15.12, Tomcat 10.1.55). The same code is in 7.0.10.

What happens

ObservationAsyncListener stops the observation in both callbacks:

@Override
public void onTimeout(AsyncEvent event) {
	this.currentObservation.stop();
}

@Override
public void onComplete(AsyncEvent event) {
	this.currentObservation.stop();
}

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:

@RestController
class PollController {

	@PostMapping("/poll")
	DeferredResult<List<String>> poll() {
		// Never set, so it always ends by the async timeout.
		return new DeferredResult<>(500L, List.of());
	}
}
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class PollCountTests {

	@Autowired TestRestTemplate rest;
	@Autowired MeterRegistry registry;

	@Test
	void timedOutPollIsCountedOnce() throws Exception {
		for (int i = 0; i < 5; i++) {
			rest.postForEntity("/poll", null, String.class);
		}
		Thread.sleep(500);
		long count = registry.get("http.server.requests").tag("uri", "/poll").timer().count();
		assertThat(count).isEqualTo(5); // fails: 10
	}
}

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.

Activity

  1. added
    status: waiting-for-triage
    on Nov 2, 2026
  2. ayodamolami4-bot commented on Nov 2, 2026

    @ayodamolami4-bot

    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.

  3. VinayPolisetti commented on Nov 2, 2026

    @VinayPolisetti
    Author

    I have a fix with regression tests on my fork: VinayPolisetti@a1546d7

    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.

  4. ayodamolami4-bot commented on Nov 2, 2026

    @ayodamolami4-bot

    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.

  5. codeconsole commented on Nov 9, 2026

    @codeconsole

    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.

  6. self-assigned this
    on Nov 12, 2026
  7. added
    in: web
    type: bug
    and removed
    status: waiting-for-triage
    on Nov 12, 2026
  8. added this to the 7.0.11 milestone on Nov 12, 2026
  9. github-actions commented on Nov 12, 2026

    @github-actions
    Contributor

    Fixed via d1c8abb

  10. added a commit that references this issue on Nov 12, 2026
    d1c8abb
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