embabel-agent icon indicating copy to clipboard operation
embabel-agent copied to clipboard

handleStuck() ignores kill() signal, causing infinite loop on shutdown

Open Stuckya opened this issue 10 months ago • 2 comments

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

  1. Start a long-running process with a StuckHandler that returns REPLAN.
  2. Process enters STUCK state and handleStuck() is called.
  3. While StuckHandler is executing, call process.kill() from another thread.
  4. handleStuck() returns REPLAN and overwrites KILLED with RUNNING
  5. run() is called recursively
  6. When resources cleaned up during shutdown, process becomes STUCK again.
  7. 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

Stuckya avatar Jan 28 '26 23:01 Stuckya

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)
    }
}

zhuyouxiang avatar Jan 29 '26 13:01 zhuyouxiang

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.

Stuckya avatar Jan 29 '26 14:01 Stuckya