Spring-cloud-sleuth: `Spring Context is not yet refreshed` exceptions are thrown when adding Spring Sleuth dependency to project

Created on 13 Aug 2020  路  9Comments  路  Source: spring-cloud/spring-cloud-sleuth

I have a working project and I don't change anything in it except for adding a gradle dependency on Spring Sleuth as below:
implementation "org.springframework.cloud:spring-cloud-starter-sleuth:2.2.4.RELEASE"

When dependency is added I have occasional but frequent failures while running integration tests. Below is the example of the error being thrown:

Caused by:
        java.lang.AssertionError: Spring Context [org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@738ef0bd, started on Thu Aug 13 13:40:44 CEST 2020, parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@57fbecf6] is not yet refreshed. This is unexpected. Reactor Context is [Context0{}] and name is [lambda]
            at org.springframework.cloud.sleuth.instrument.reactor.ReactorSleuth.lambda$scopePassingSpanOperator$0(ReactorSleuth.java:94)
            at reactor.core.publisher.FluxLift.subscribeOrReturn(FluxLift.java:50)
            at reactor.core.publisher.Flux.subscribe(Flux.java:8311)
            at reactor.core.publisher.Flux.subscribeWith(Flux.java:8494)
            at reactor.core.publisher.Flux.subscribe(Flux.java:8295)
            at reactor.core.publisher.Flux.subscribe(Flux.java:8222)
            at reactor.core.publisher.Flux.subscribe(Flux.java:8140)
            at com.klarna.card.transaction.listener.logging.TestLoggingHelper.testCorrelationIdLogInjection(TestLoggingHelper.java:100)

It looks like it is something related to Reactor project as well. I use WebFlux and SpringBoot 2.3.2.RELEASE version. Please, let me know if there is any other information that can be useful for investigation.

bug

Most helpful comment

I am having this same issue. When run independently, unit tests that use reactor APIs work fine, but when as part of a full mvn build, the unit tests that run after spring tests fail with this error.

It seems that spring-cloud-sleuth does not clean up its hooks when the spring context shuts down.

Adding this to my unit tests works around the problem:

    @BeforeAll
    public static void beforeAllTests() {
        Hooks.resetOnEachOperator();
        Hooks.resetOnLastOperator();
        Schedulers.resetOnScheduleHooks();
    }

We can see that some tests in spring-cloud-sleuth itself do the same thing (though it's not clear if this is to work around the same problem, or because the tests actually expect the hooks to be dirtied by something inside the test itself): https://github.com/spring-cloud/spring-cloud-sleuth/blob/6e306e/tests/common/src/main/java/org/springframework/cloud/sleuth/instrument/web/client/HttpClientBeanPostProcessorTest.java#L51

It's also possible to work around this by making each test class execute in its own JVM, although this may slow your build down considerably. E.g.:

                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <configuration>
                        <forkCount>1</forkCount>
                        <threadCount>1</threadCount>
                        <reuseForks>false</reuseForks>
                    </configuration>
                </plugin>

All 9 comments

Hi, i'm struggling with exactly same problem. I have tried different spring/cloud versions but always the same problem.

java.lang.AssertionError: Spring Context [org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@75a180cc, started on Fri Sep 04 10:57:51 CEST 2020, parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@41dd414b] is not yet refreshed. This is unexpected. Reactor Context is [Context0{}] and name is [blockingOptional] at org.springframework.cloud.sleuth.instrument.reactor.ReactorSleuth.lambda$scopePassingSpanOperator$0(ReactorSleuth.java:94) at reactor.core.publisher.MonoLiftFuseable.subscribeOrReturn(MonoLiftFuseable.java:46) at reactor.core.publisher.Mono.subscribe(Mono.java:4195) at reactor.core.publisher.Mono.blockOptional(Mono.java:1710)

What i've noticed is that this problem occurs only when i run tests from maven. If I execute tests directly in intellij (even whole set of tests) everything works fine. @mikhailsoparin Have You found any solution or workaround for this problem?

It seems that in my case, the problem is with @DirtiesContext annotation. When i remove it, this problem is gone

I am having this same issue. When run independently, unit tests that use reactor APIs work fine, but when as part of a full mvn build, the unit tests that run after spring tests fail with this error.

It seems that spring-cloud-sleuth does not clean up its hooks when the spring context shuts down.

Adding this to my unit tests works around the problem:

    @BeforeAll
    public static void beforeAllTests() {
        Hooks.resetOnEachOperator();
        Hooks.resetOnLastOperator();
        Schedulers.resetOnScheduleHooks();
    }

We can see that some tests in spring-cloud-sleuth itself do the same thing (though it's not clear if this is to work around the same problem, or because the tests actually expect the hooks to be dirtied by something inside the test itself): https://github.com/spring-cloud/spring-cloud-sleuth/blob/6e306e/tests/common/src/main/java/org/springframework/cloud/sleuth/instrument/web/client/HttpClientBeanPostProcessorTest.java#L51

It's also possible to work around this by making each test class execute in its own JVM, although this may slow your build down considerably. E.g.:

                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <configuration>
                        <forkCount>1</forkCount>
                        <threadCount>1</threadCount>
                        <reuseForks>false</reuseForks>
                    </configuration>
                </plugin>

Hello guys,
I had a similar problem @mikhailsoparin and I work around it by adding "@AutoConfigureWebClient(registerRestTemplate = true)" in the unit test class.

Will remove the assertion mechanism so that those messages are not displayed. You'll see them again if you turn on trace mode logging

Hi I wonder when will the 2.2.7 be released (which seems to contain the fix to this bug)? @marcingrzejszczak

There's no due date for that release train yet. You can check this page to see if a tentative date was set (https://github.com/spring-cloud/spring-cloud-release/milestones)

@marcingrzejszczak Hmm so is there a rough time range? That page seems to have no milestone related to 2.2.7 yet...

Nope, no date at this point. If you really need to use it asap, you can use snapshots or https://jitpack.io/ and align your version to a concrete commit.

Was this page helpful?
0 / 5 - 0 ratings