Terminate workflow is throwing error, although it terminates the workflow | Community
Skip to main content
Level 4
March 6, 2025
Solved

Terminate workflow is throwing error, although it terminates the workflow

  • March 6, 2025
  • 3 replies
  • 546 views

Hi Team,

 

We have a custom process step, where based on logic we are starting the workflow/terminating the workflow.

 

We are terminating the workflow by using the below line.

 

wfsession.terminateWorkflow(item.getWorkflow());

 

This does terminate the workflow and safely completes whatever we need, but it throws an error in the backend

java.lang.IllegalStateException: Workflow is already finished.
	at com.adobe.granite.workflow.core.WorkflowSessionImpl.terminateWorkflow(WorkflowSessionImpl.java:546) [com.adobe.granite.workflow.core:2.0.240.CQ660-B0017]
	at com.adobe.granite.workflow.core.job.HandlerBase.executeProcess(HandlerBase.java:198) [com.adobe.granite.workflow.core:2.0.240.CQ660-B0017]
	at com.adobe.granite.workflow.core.job.JobHandler.process(JobHandler.java:271) [com.adobe.granite.workflow.core:2.0.240.CQ660-B0017]
	at org.apache.sling.event.impl.jobs.JobConsumerManager$JobConsumerWrapper.process(JobConsumerManager.java:502) [org.apache.sling.event:4.2.24]
	at org.apache.sling.event.impl.jobs.queues.JobQueueImpl.startJob(JobQueueImpl.java:351) [org.apache.sling.event:4.2.24]
	at org.apache.sling.event.impl.jobs.queues.JobQueueImpl.access$100(JobQueueImpl.java:60) [org.apache.sling.event:4.2.24]
	at org.apache.sling.event.impl.jobs.queues.JobQueueImpl$1.run(JobQueueImpl.java:287) [org.apache.sling.event:4.2.24]
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
	at java.base/java.lang.Thread.run(Thread.java:829)


This seems like an product error. Can some please comment how to get around this?

 

Best answer by Harwinder-singh

@vishwanath881 You can try checking the state of the workflow using item.getWorkflow().getState(). If this returns anything else than RUNNING, you can avoid calling the terminateWorkflow method.

3 replies

Harwinder-singh
Community Advisor
Harwinder-singhCommunity AdvisorAccepted solution
Community Advisor
March 6, 2025

@vishwanath881 You can try checking the state of the workflow using item.getWorkflow().getState(). If this returns anything else than RUNNING, you can avoid calling the terminateWorkflow method.

giuseppebaglio
Level 10
March 14, 2025

The error occurs because you’re attempting to terminate a workflow that is already in a finished state (e.g., COMPLETED, ABORTED, or SUSPENDED). To avoid this:

 

  • Check Workflow State Before Termination

 

import com.adobe.granite.workflow.Workflow; import com.adobe.granite.workflow.exec.WorkflowState; Workflow workflow = item.getWorkflow(); if (workflow.getState() == WorkflowState.RUNNING) { wfsession.terminateWorkflow(workflow); }

 

  • Even with the check above, race conditions may occur if the workflow finishes naturally between the state check and termination. Wrap the termination in a try-catch block:

 

try { if (workflow.getState() == WorkflowState.RUNNING) { wfsession.terminateWorkflow(workflow); } } catch (IllegalStateException e) { // Log and ignore if the workflow is already finished logger.warn("Workflow {} is already terminated/completed.", workflow.getId()); }

 

 
kautuk_sahni
Community Manager
Community Manager
April 1, 2025

@vishwanath881 Did you find the suggestions helpful? Please let us know if you need more information. If a response worked, kindly mark it as correct for posterity; alternatively, if you found a solution yourself, we’d appreciate it if you could share it with the community. Thank you!

Kautuk Sahni