{"record":{"id":"c456afeebc893f04","repo":"alibaba/arthas","slug":"cannot-set-to-foreground-a-process-in-state","errorCode":null,"errorMessage":"Cannot set to foreground a process in {} state","messagePattern":"Cannot set to foreground a process in (.+?) state","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"core/src/main/java/com/taobao/arthas/core/shell/system/impl/ProcessImpl.java","lineNumber":232,"sourceCode":"            }\n        } else {\n            throw new IllegalStateException(\"Cannot set to background a process in \" + processStatus + \" state\");\n        }\n    }\n\n    @Override\n    public void toForeground() {\n        toForeground(null);\n    }\n\n    @Override\n    public void toForeground(Handler<Void> completionHandler) {\n        if (processStatus == ExecStatus.RUNNING) {\n            if (!processForeground) {\n                updateStatus(ExecStatus.RUNNING, null, true, foregroundHandler, terminatedHandler, completionHandler);\n            }\n        } else {\n            throw new IllegalStateException(\"Cannot set to foreground a process in \" + processStatus + \" state\");\n        }\n    }\n\n    @Override\n    public void terminate() {\n        terminate(null);\n    }\n\n    @Override\n    public void terminate(Handler<Void> completionHandler) {\n        if (!terminate(-10, completionHandler, null)) {\n            throw new IllegalStateException(\"Cannot terminate terminated process\");\n        }\n    }\n\n    private synchronized boolean terminate(int exitCode, Handler<Void> completionHandler, String message) {\n        if (processStatus != ExecStatus.TERMINATED) {\n            //add status message","sourceCodeStart":214,"sourceCodeEnd":250,"githubUrl":"https://github.com/alibaba/arthas/blob/21cf2e9ba52b305290be7223b980ff504bb9cb5b/core/src/main/java/com/taobao/arthas/core/shell/system/impl/ProcessImpl.java#L214-L250","documentation":"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.","triggerScenarios":"Issuing 'fg <jobId>' on a job that is stopped, ready, or terminated rather than running in the background.","commonSituations":"User tries to foreground a job that already exited or was suspended.","solutions":["Verify the target job is RUNNING via 'jobs' before calling fg.","If the job is STOPPED, resume it before foregrounding."],"exampleFix":"// before\njob.process().toForeground();\n\n// after\nif (job.process().status() == ExecStatus.RUNNING) {\n    job.process().toForeground();\n}","handlingStrategy":"type-guard","validationCode":"if (process.status() == ExecStatus.RUNNING) {\n    process.toForeground();\n}","typeGuard":"boolean canForeground(Process p) {\n    return p.status() == ExecStatus.RUNNING;\n}","tryCatchPattern":"try {\n    process.toForeground();\n} catch (IllegalStateException e) {\n    if (e.getMessage().contains(\"Cannot set to foreground\")) {\n        // not running; no-op\n    }\n}","preventionTips":["Confirm the target job is RUNNING before foregrounding.","Don't foreground jobs that have already exited."],"tags":["process-state","foreground","lifecycle"],"backgroundTag":null,"analyzedSha":"21cf2e9ba52b305290be7223b980ff504bb9cb5b","analyzedAt":"2026-08-14T00:57:07.243Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}