while (true) loop with no reachable exit

ID

scala.correctness.potential_infinite_loop

Severity

low

Remediation Complexity

medium

Remediation Risk

medium

Remediation Effort

medium

Resource

Infinite Loop

Language

Scala

Description

Reports a while (true) loop whose body contains no return, throw or break, so no statement in it can end the loop.

Rationale

A loop that cannot terminate pins a thread forever. In a request handler or an actor that exhausts the pool and the service stops answering; in a build or a batch job it hangs until it is killed, and the cause is hard to find because nothing fails and nothing is logged.

The following code illustrates the pattern detected by this rule:

class Queue {
  def poll(): AnyRef = null
}

class Worker(queue: Queue) {
  def drain(): Unit = {
    // FLAGGED: while (true) loop with no reachable exit
    while (true) {
      process(queue.poll())
    }
  }

  private def process(item: AnyRef): Unit = ()
}

Remediation

Give the loop a termination condition — while (!stopped) over a flag the body updates, a bounded counter, or an iterator — or make the exit explicit with return, an exception, or scala.util.control.Breaks. A genuinely endless service loop belongs in a supervised thread with a documented shutdown signal.

Configuration

This detector does not need any configuration.

© 2026 Xygeni. All rights reserved