apache/hadoop · error · IllegalStateException

StopWatch is already running

Error message

StopWatch is already running

What it means

org.apache.hadoop.util.StopWatch accumulates elapsed time across start()/stop() pairs. start() throws IllegalStateException when the watch is already running — a second start without an intervening stop (or reset) is a state-machine violation, not a time measurement.

Source

Thrown at hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/StopWatch.java:60

  public StopWatch(Timer timer) {
    this.timer = timer;
  }

  /**
   * The method is used to find out if the StopWatch is started.
   * @return boolean If the StopWatch is started.
   */
  public boolean isRunning() {
    return isStarted;
  }

  /**
   * Start to measure times and make the state of stopwatch running.
   * @return this instance of StopWatch.
   */
  public StopWatch start() {
    if (isStarted) {
      throw new IllegalStateException("StopWatch is already running");
    }
    isStarted = true;
    startNanos = timer.monotonicNowNanos();
    return this;
  }

  /**
   * Stop elapsed time and make the state of stopwatch stop.
   * @return this instance of StopWatch.
   */
  public StopWatch stop() {
    if (!isStarted) {
      throw new IllegalStateException("StopWatch is already stopped");
    }
    long now = timer.monotonicNowNanos();
    isStarted = false;
    currentElapsedNanos += now - startNanos;
    return this;

View on GitHub (pinned to 2add963021)

Solutions

  1. Check state first: if (watch.isRunning()) watch.stop(); before start()
  2. Use reset() when you want a fresh measurement: watch.reset().start() (reset clears elapsed and stops the watch)
  3. Pair start/stop in try/finally so every start has exactly one stop
  4. Since StopWatch implements Closeable, prefer try (StopWatch sw = new StopWatch().start()) { ... } which stops on close

Example fix

// before
watch.start();
doWork();
watch.start();   // IllegalStateException

// after
watch.start();
doWork();
watch.stop();
watch.reset();   // elapsed cleared, watch stopped
watch.start();   // fresh interval is now legal
Defensive patterns

Strategy: validation

Validate before calling

if (watch.isRunning()) {
  watch.stop(); // close the previous interval first
}
watch.start();

Try / catch

try { watch.start(); } catch (IllegalStateException e) { /* watch already running: stop() or reset() before restarting */ }

Prevention

When it happens

Trigger: Calling watch.start() twice: instrumentation added at two layers wrapping the same watch; restart in a loop iteration that forgot stop(); timing code copy-pasted around a method that already started it.

Common situations: Refactoring moves a start() into a helper that callers also start; benchmark loops that start at the top of each iteration but only stop conditionally; watches shared across try blocks with early continues.

Related errors


AI-assisted analysis of apache/hadoop@2add963021 (2026-08-22). Data as JSON: /api/errors/4aaf2833c85b507f. Report an issue: GitHub.