alibaba/arthas · error · IllegalStateException

Cannot set to foreground a process in {} state

Error message

Cannot set to foreground a process in {} state

What it means

Thrown by Process.toForeground() when the process is not in the RUNNING state. Foregrounding is only valid for a running background process; calling it on a non-running process violates the state machine.

Source

Thrown at core/src/main/java/com/taobao/arthas/core/shell/system/impl/ProcessImpl.java:232

            }
        } else {
            throw new IllegalStateException("Cannot set to background a process in " + processStatus + " state");
        }
    }

    @Override
    public void toForeground() {
        toForeground(null);
    }

    @Override
    public void toForeground(Handler<Void> completionHandler) {
        if (processStatus == ExecStatus.RUNNING) {
            if (!processForeground) {
                updateStatus(ExecStatus.RUNNING, null, true, foregroundHandler, terminatedHandler, completionHandler);
            }
        } else {
            throw new IllegalStateException("Cannot set to foreground a process in " + processStatus + " state");
        }
    }

    @Override
    public void terminate() {
        terminate(null);
    }

    @Override
    public void terminate(Handler<Void> completionHandler) {
        if (!terminate(-10, completionHandler, null)) {
            throw new IllegalStateException("Cannot terminate terminated process");
        }
    }

    private synchronized boolean terminate(int exitCode, Handler<Void> completionHandler, String message) {
        if (processStatus != ExecStatus.TERMINATED) {
            //add status message

View on GitHub (pinned to 21cf2e9ba5)

Solutions

  1. Verify the target job is RUNNING via 'jobs' before calling fg.
  2. If the job is STOPPED, resume it before foregrounding.

Example fix

// before
job.process().toForeground();

// after
if (job.process().status() == ExecStatus.RUNNING) {
    job.process().toForeground();
}
Defensive patterns

Strategy: type-guard

Validate before calling

if (process.status() == ExecStatus.RUNNING) {
    process.toForeground();
}

Type guard

boolean canForeground(Process p) {
    return p.status() == ExecStatus.RUNNING;
}

Try / catch

try {
    process.toForeground();
} catch (IllegalStateException e) {
    if (e.getMessage().contains("Cannot set to foreground")) {
        // not running; no-op
    }
}

Prevention

When it happens

Trigger: Issuing 'fg <jobId>' on a job that is stopped, ready, or terminated rather than running in the background.

Common situations: User tries to foreground a job that already exited or was suspended.

Related errors


AI-assisted analysis of alibaba/arthas@21cf2e9ba5 (2026-08-14). Data as JSON: /api/errors/c456afeebc893f04. Report an issue: GitHub.