question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

FluxSwitchMap infinite while loop causes high CPU usage

See original GitHub issue

Expected Behavior

Loop should not stuck in an infinite loop and should not cause high cpu usage.

Actual Behavior

We realised this problem on production and our CPUs were randomly spiking. After some investigation with heap dumps, we have found the problem is on FluxSwitchMap#drain or FluxSwitchMap line 353, where it does:

Object second;
while((second = q.poll()) == null) {
}

This causes a high cpu and we have to restart our application to stop this (otherwise its never recovered for days).

Screenshot 2021-01-11 at 16 20 27

Screenshot above shows one of our pods in production, where it spikes to that CPU level for days until we notice and restart the application.

Screenshot 2021-01-12 at 11 01 18

Screenshot above shows one of the pods has doubled the CPU spike. This means both parallel threads are in deadlock.

Steps to Reproduce

This is not our source code but I tried to repeat it in a junit4 test. I’m not the best in repeater codes so please run it more than once if it succeeds. I know that its a race condition and it happens randomly. That’s why I repeat the StepVerifier for many time.

While running the test, at some point it stops printing the numbers and CPU spikes. That means the thread is stuck on that while loop.

 @Test(timeout = 120_000)
    public void test() {
        Flux<Integer> integerFlux = Flux.range(0, 20)
                .delayElements(Duration.ofMillis(2))
                .switchMap(s -> Flux.range(0, 20).delayElements(Duration.ofMillis(1)))
                .doOnNext(a -> System.out.println(a));

        for (int i = 0; i < 1000; i++) {
            StepVerifier.create(integerFlux)
                    .thenConsumeWhile(a -> true)
                    .verifyComplete();
        }
    }

Possible Solution

Your Environment

  • Spring Boot 2.2.6
  • Spring Boot 2.4.1 (both versions fail)
  • reactor-core-3.3.4.RELEASE
  • openjdk8

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:3
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
OlegDokukacommented, Jan 19, 2021

Thanks, @TunaYagci! So far I know where is the root cause of that issue, thus, stay tuned, I will keep you updated!

0reactions
TunaYagcicommented, Feb 11, 2021

huge thanks to @OlegDokuka and @simonbasle for your efforts! I will test it when you release and update this issue with my findings.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Infinite Loops Are Chewing up Your CPU! | by Coucou Camille
An infinite loop causes unexpected consumption of resources, such as CPU or memory. It could take CPU usage to near 100% and hinder...
Read more >
Why did switching to from an infinite loop to TimerTask cause ...
Three possibilities I can think of: You have a huge number of threads doing this, and they're context switching all the time.
Read more >
Reactor 3 Reference Guide
Similarly, infinite sequences are not necessarily empty. For example, Flux.interval(Duration) produces a Flux<Long> that is infinite and emits regular ticks ...
Read more >
IntelliJ IDEA 2021.3 EAP (213.5744.113 build) Release Notes
No subsystem Bug IDEA‑281332 No subsystem Bug IDEA‑146360 No subsystem Exception IDEA‑280424 Android Bug IDEA‑281716
Read more >
Infinite while loop CPU usage
The quick and dirty solution is to add a sleep call to yield the processor to other applications. This lowers the CPU usage...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found