Skip to content

retryWhen(backoff()) causing high resource usage in unit test #4054

@lelelemonade

Description

@lelelemonade

Our unit test will fail because of OOM. Steps to Reproduce contains shortest code snippet to reproduce. I searched github issure and didn't find anything similar. If I shouldn't write code this way, I hope I can get some suggestion.

Expected Behavior

Uni test should not consume so much resource

Actual Behavior

Unit test will fail because of OOM. Reproduced code will consume 1.7G memory.

Steps to Reproduce

public class TestReactor {
    public int exampleMethod() {
        return 2;
    }

    @Test
    public void testReactorRetry() {
        TestReactor test = mock(TestReactor.class);
        when(test.exampleMethod())
                .thenThrow(new IllegalStateException())
                .thenThrow(new IllegalStateException())
                .thenReturn(1);

        Flux<Object> testFlux = Flux.generate(
                sink -> {
                    sink.next(test.exampleMethod());
                    sink.next(test.exampleMethod());
                    sink.next(test.exampleMethod());
                    sink.complete();
                }).retryWhen(Retry.backoff(3, Duration.ofSeconds(1)));
        StepVerifier.create(testFlux)
                .expectNext(1)
                .thenCancel()
                .verify();
    }
}

Possible Solution

stacktrace shows that there is a nearly endless loop in reactor.test.DefaultStepVerifierBuilder.DefaultVerifySubscriber#pollTaskEventOrComplete. I think StepVerifier should be able to know the Flux it subscribe is in a retry backoff status and not endless loop here.

Your Environment

  • Reactor version(s) used: 3.8.0-M4
  • Other relevant libraries versions (eg. netty, ...):
  • JVM version (java -version): jdk 21
  • OS and version (eg uname -a): Linux cacacarrot 6.15.5-arch1-1 - Minor cosmetic tweaks for EmitterProcessor #1 SMP PREEMPT_DYNAMIC Sun, 06 Jul 2025 11:14:36 +0000 x86_64 GNU/Linux
Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions