apache/hadoop · error · IllegalStateException

StopWatch is already stopped

Error message

StopWatch is already stopped

What it means

org.apache.hadoop.util.StopWatch.stop() throws IllegalStateException when the watch is not running: stop() without a prior start(), or a second stop() after the watch already halted. The elapsed accumulation (currentElapsedNanos) can only be advanced from a running state.

Source

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

   * 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;
  }

  /**
   * Reset elapsed time to zero and make the state of stopwatch stop.
   * @return this instance of StopWatch.
   */
  public StopWatch reset() {
    currentElapsedNanos = 0;
    isStarted = false;
    return this;
  }

  /**

View on GitHub (pinned to 2add963021)

Solutions

  1. Check state: if (watch.isRunning()) watch.stop();
  2. Ensure start() precedes the try block so finally's stop() always has a running watch
  3. Centralize stop in one place (single owner) instead of multiple teardown paths
  4. Use try-with-resources with the Closeable behavior (close stops a running watch safely)

Example fix

// before
try {
  riskyInit();
  watch.start();
  doWork();
} finally {
  watch.stop();   // throws if riskyInit() failed before start
}

// after
watch.start();
try {
  doWork();
} finally {
  if (watch.isRunning()) {
    watch.stop();
  }
}
Defensive patterns

Strategy: validation

Validate before calling

if (watch.isRunning()) {
  watch.stop();
}

Try / catch

try { watch.stop(); } catch (IllegalStateException e) { /* was never started or already stopped: nothing to record */ }

Prevention

When it happens

Trigger: watch.stop() in a finally block when start() was skipped or threw earlier; double-stop after an unconditional stop plus another teardown path; stop() called on a fresh or reset() watch.

Common situations: try/finally timing where start sits after the code that throws; loop bodies stopping per iteration but starting only once; close() implementations that stop a watch another layer already stopped.

Related errors


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