{"record":{"id":"c5a67e9f24ce029a","repo":"alibaba/spring-ai-alibaba","slug":"schedule-already-started","errorCode":null,"errorMessage":"Schedule already started","messagePattern":"Schedule already started","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"spring-ai-alibaba-graph-core/src/main/java/com/alibaba/cloud/ai/graph/scheduling/ScheduledAgentTask.java","lineNumber":72,"sourceCode":"\n\tprivate final String taskId;\n\n\tpublic ScheduledAgentTask(CompiledGraph graph, ScheduleConfig config) {\n\t\tthis.graph = graph;\n\t\tthis.config = config;\n\t\tScheduledAgentManager scheduledAgentManager = ScheduledAgentManagerFactory.getInstance().getManager();\n\t\tthis.taskScheduler = scheduledAgentManager.getTaskScheduler();\n\t\t// Register with the active manager\n\t\tthis.taskId = scheduledAgentManager.registerTask(this);\n\t\tlog.debug(\"Created ScheduledAgentTask with ID: {}\", taskId);\n\t}\n\n\t/**\n\t * Start the scheduled execution\n\t */\n\tpublic ScheduledAgentTask start() {\n\t\tif (started) {\n\t\t\tthrow new IllegalStateException(\"Schedule already started\");\n\t\t}\n\n\t\tswitch (config.getMode()) {\n\t\t\tcase CRON:\n\t\t\t\tscheduledFuture = taskScheduler.schedule(this::executeGraph,\n\t\t\t\t\t\tnew CronTrigger(config.getCronExpression()));\n\t\t\t\tbreak;\n\t\t\tcase FIXED_DELAY:\n\t\t\t\tscheduledFuture = taskScheduler.scheduleWithFixedDelay(this::executeGraph,\n\t\t\t\t\t\tInstant.now().plusMillis(config.getInitialDelay()), Duration.ofMillis(config.getFixedDelay()));\n\t\t\t\tbreak;\n\t\t\tcase FIXED_RATE:\n\t\t\t\tscheduledFuture = taskScheduler.scheduleAtFixedRate(this::executeGraph,\n\t\t\t\t\t\tInstant.now().plusMillis(config.getInitialDelay()), Duration.ofMillis(config.getFixedRate()));\n\t\t\t\tbreak;\n\t\t\tcase ONE_TIME:\n\t\t\t\tscheduledFuture = taskScheduler.schedule(this::executeGraph,\n\t\t\t\t\t\tInstant.now().plusMillis(config.getInitialDelay()));","sourceCodeStart":54,"sourceCodeEnd":90,"githubUrl":"https://github.com/alibaba/spring-ai-alibaba/blob/f82da0b50f35744c13968191be2b1cd2452ef550/spring-ai-alibaba-graph-core/src/main/java/com/alibaba/cloud/ai/graph/scheduling/ScheduledAgentTask.java#L54-L90","documentation":"ScheduledAgentTask.start() throws IllegalStateException if the task was already started (started flag true). Each ScheduledAgentTask instance guards against double-scheduling, which would otherwise register duplicate executions with the taskScheduler.","triggerScenarios":"Calling start() twice on the same ScheduledAgentTask instance — e.g. restart logic that re-invokes start on an existing task, or configuration code that starts tasks both in initialization and in a scheduler scan.","commonSituations":"Application hot-reload calling start again; idempotency wrappers missing around task bootstrap; tests running setup twice.","solutions":["Track started tasks and only call start() once per instance (check a started flag or idempotent wrapper)","Create a new ScheduledAgentTask instance for restart instead of reusing the old one","Call stop()/cancel on the existing task before starting a new instance","Wrap start() in an idempotency guard: if (taskStartedIds.add(taskId)) task.start();"],"exampleFix":"// before\ntask.start();\ntask.start(); // throws\n// after\nif (!task.isStarted()) {\n    task.start();\n}","handlingStrategy":"validation","validationCode":"if (!task.isStarted()) {\n    task.start();\n}","typeGuard":"boolean canStart(ScheduledAgentTask t) {\n    return t != null && !t.isStarted();\n}","tryCatchPattern":"try {\n    task.start();\n} catch (IllegalStateException e) {\n    if (\"Schedule already started\".equals(e.getMessage())) {\n        // idempotent: already running, safe to ignore\n    } else throw e;\n}","preventionTips":["Wrap start() calls in idempotent guards keyed by task id","Create a new ScheduledAgentTask for every (re)schedule instead of restarting the old one","Centralize task startup in one lifecycle method","Track started task ids in a Set to avoid double invocation"],"tags":["illegal-state","double-start","scheduling","lifecycle"],"backgroundTag":"invalid-state-transition","analyzedSha":"f82da0b50f35744c13968191be2b1cd2452ef550","analyzedAt":"2026-09-09T15:32:42.421Z","contentChangedAt":"2026-09-09T15:32:42.421Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}