tracing: `ObservationThreadLocalAccessor.setValue()` starts `NullObservation` but doesn't stop it

I’m not sure if this is an issue with Spring Webflux, Micrometer Tracing or Reactor (this started after upgrading from Spring Boot 3.0 to 3.1, which brought new versions of all three libs).

When running with assertions enabled (-ea), the following test fails (without assertions, it just logs a warning):

@Test
void reproducer() {
    Hooks.enableAutomaticContextPropagation();
    WebClient.create()
            .get()
            .uri("https://httpbin.org/get")
            .retrieve()
            .bodyToMono(String.class)
            .block();
}

The logged warning/thrown error is 00:11:57.344 [main] WARN io.micrometer.observation.contextpropagation.ObservationThreadLocalAccessor -- Observation <{name=null(null), error=null, context=name='null', contextualName='null', error='null', lowCardinalityKeyValues=[], highCardinalityKeyValues=[], map=[], parentObservation=null}> to which we're restoring is not the same as the one set as this scope's parent observation <null> . Most likely a manually created Observation has a scope opened that was never closed. This may lead to thread polluting and memory leaks

While debugging this I found the following in ObservationThreadLocalAccessor: https://github.com/micrometer-metrics/micrometer/blob/9d368be199d3da8c82817994c82882e4c28f93f8/micrometer-observation/src/main/java/io/micrometer/observation/contextpropagation/ObservationThreadLocalAccessor.java#L111-L116 Reactor calls this method when automatic context propagation is enabled (via ContextSnapshotFactory.setThreadLocalsFrom(context)). This seems suspicious to me as an observation is started here that is later still around, causing the above warning/error.


Dependencies:

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webflux</artifactId>
        <version>6.0.10</version>
    </dependency>
    <dependency>
        <groupId>io.micrometer</groupId>
        <artifactId>micrometer-tracing</artifactId>
        <version>1.1.2</version>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter</artifactId>
        <version>5.9.3</version>
        <scope>test</scope>
    </dependency>
</dependencies>

Minimal Project (extract and run ./mvnw test): demo.zip

This may be related to #175.

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 20

Most upvoted comments

I am facing the same error as @PascalSchumacher when trying to upgrade to SB 3.1.1 today.

I created a reproducer app here: https://github.com/xsreality/micrometer-tracing-gh-296/