handleStuck() ignores kill() signal, causing infinite loop on shutdown
I'm continuing down the Utility AI path and ran into another bug.
When a process is killed via kill() during stuck handling, the handleStuck() method ignores the KILLED status and continues to restart the process, causing an infinite handleStuck() → run() loop.
Reproduction
- Start a long-running process with a StuckHandler that returns REPLAN.
- Process enters STUCK state and
handleStuck()is called. - While StuckHandler is executing, call
process.kill()from another thread. handleStuck()returns REPLAN and overwrites KILLED with RUNNINGrun()is called recursively- When resources cleaned up during shutdown, process becomes STUCK again.
- Infinite loop
Stack trace
at c.e.a.core.support.AbstractAgentProcess.run(AbstractAgentProcess.kt:223)
at c.e.a.core.support.AbstractAgentProcess.handleStuck(AbstractAgentProcess.kt:274)
at c.e.a.core.support.AbstractAgentProcess.run(AbstractAgentProcess.kt:223)
at c.e.a.core.support.AbstractAgentProcess.handleStuck(AbstractAgentProcess.kt:274)
... (repeating)
I suspect the problem is here: https://github.com/embabel/embabel-agent/blob/7aca23561bf28e33c1c20f38230f61e1fec3885e/embabel-agent-api/src/main/kotlin/com/embabel/agent/core/support/AbstractAgentProcess.kt#L270-L275
Follow-up question: The kill command only flags the status; it does not actually terminate the AgentProcess. Would it be more appropriate to actually terminate the process?
abstract class AbstractAgentProcess(
override fun kill(): ProcessKilledEvent? {
setStatus(AgentProcessStatusCode.KILLED)
return ProcessKilledEvent(this)
}
}
Personally, I think defensive programming is best here. Adding a simple guard should fix this issue.
StuckHandlingResultCode.REPLAN -> {
if (finished) {
logger.info("Process {} is {} during stuck handling, will not replan", this.id, status)
return
}
logger.info("Process {} unstuck and will replan: {}", this.id, result.message)
setStatus(AgentProcessStatusCode.RUNNING)
run()
}
Let me write a unit test to confirm and I'll open a PR.