agentscope-java icon indicating copy to clipboard operation
agentscope-java copied to clipboard

[Bug]:Session.delete(sessionKey, key) no-op causes repeated empty input after graceful shutdown retry`

Open dragondyt opened this issue 7 months ago • 0 comments

Title Session.delete(sessionKey, key) default no-op causes shutdown_interrupted flag to persist and repeated empty input after graceful shutdown retry

AgentScope-Java is an open-source project. To involve a broader community, we recommend asking your questions in English.

Describe the bug Session.delete(SessionKey sessionKey, String key) has a default no-op implementation.

default void delete(SessionKey sessionKey, String key) {
    // Default no-op; implementations should override if they support per-key deletion
}

Currently, InMemorySession and JsonSession override this per-key delete method, but RedisSession does not. Other custom Session implementations may also inherit the default no-op behavior.

This breaks the graceful shutdown resume logic.

During shutdown interruption, ActiveRequestContext.saveToSession() persists the agent state and writes the shutdown_interrupted flag:

agent.saveTo(session, sessionKey);
session.save(sessionKey, SHUTDOWN_INTERRUPTED_KEY, new ShutdownInterruptedState(true));

On the next request, GracefulShutdownHook handles PreCallEvent and calls checkAndClearShutdownInterrupted(...). If the flag exists, it tries to clear it:

binding.session()
        .delete(binding.sessionKey(), ActiveRequestContext.SHUTDOWN_INTERRUPTED_KEY);
return true;

However, when the underlying Session implementation does not override delete(sessionKey, key), the flag is never removed. checkAndClearShutdownInterrupted(...) still returns true, so every following request is treated as a shutdown-interrupted retry.

GracefulShutdownHook then discards the current user input:

event.setInputMessages(List.of());

As a result, the user input becomes empty repeatedly, and the agent may keep generating the same response again and again from the saved memory context.

To Reproduce Steps to reproduce the behavior:

  1. Use a Session implementation that does not override delete(SessionKey sessionKey, String key), for example RedisSession.
  2. Create an agent with session persistence enabled.
  3. Start a request and trigger graceful shutdown while the request is active.
  4. The shutdown logic saves the agent state and writes shutdown_interrupted = true into the session.
  5. Restart the application or retry the same client request.
  6. GracefulShutdownHook receives PreCallEvent and calls checkAndClearShutdownInterrupted(...).
  7. checkAndClearShutdownInterrupted(...) reads the flag and calls session.delete(sessionKey, "shutdown_interrupted").
  8. Because delete(sessionKey, key) is a no-op for this session implementation, the flag remains.
  9. The hook calls event.setInputMessages(List.of()), clearing the user input.
  10. Retry or continue the conversation again.
  11. The same flag is detected again, user input is cleared again, and the agent repeatedly outputs the same content.

Expected behavior The shutdown_interrupted flag should be removed after it is detected once.

After checkAndClearShutdownInterrupted(...) returns true, subsequent requests should not be treated as shutdown-interrupted retries unless a new shutdown interruption actually occurs.

Session.delete(sessionKey, key) should either:

  • be implemented by all built-in Session implementations, including RedisSession; or
  • not have a silent no-op default implementation; or
  • checkAndClearShutdownInterrupted(...) should verify that the flag was actually removed before returning true.

Error messages No exception is thrown.

Observed behavior:

  • shutdown_interrupted remains in session storage.
  • User input is repeatedly cleared by event.setInputMessages(List.of()).
  • The agent repeatedly generates the same or similar output.
  • The issue is silent because the default delete(sessionKey, key) implementation does nothing and does not fail.

Environment (please complete the following information):

  • AgentScope-Java Version: 1.0.11
  • Java Version: [please fill in, e.g. 17 / 21 / 25]
  • OS: Windows

Additional context The issue is caused by a mismatch between the graceful shutdown logic and the Session contract.

GracefulShutdownManager.checkAndClearShutdownInterrupted(...) assumes per-key deletion works:

if (flag.isPresent() && flag.get().interrupted()) {
    binding.session()
            .delete(binding.sessionKey(), ActiveRequestContext.SHUTDOWN_INTERRUPTED_KEY);
    return true;
}

But Session.delete(sessionKey, key) is optional in practice because the interface provides a no-op default implementation.

This is especially problematic for persistent or distributed sessions such as RedisSession, where the flag survives process restarts and causes every retry to be treated as a shutdown resume.

Possible fixes:

  • Implement delete(SessionKey sessionKey, String key) in RedisSession and other built-in session implementations.
  • Consider making delete(SessionKey sessionKey, String key) a required interface method instead of a default no-op.
  • Add a regression test using a non-memory session implementation to verify that shutdown_interrupted is cleared after the first retry.
  • Optionally make checkAndClearShutdownInterrupted(...) re-read the flag after deletion or log a warning if the flag still exists.

dragondyt avatar Apr 29 '26 07:04 dragondyt