fix: unbind ShutdownStateSaver on ReActAgent.close() to prevent memory leak#2384
Open
sikisama wants to merge 1 commit into
Open
fix: unbind ShutdownStateSaver on ReActAgent.close() to prevent memory leak#2384sikisama wants to merge 1 commit into
sikisama wants to merge 1 commit into
Conversation
…y leak GracefulShutdownManager.bindStateSaver() registers a ShutdownStateSaver in a static ConcurrentHashMap (stateSavers) keyed by agentId, but there is no corresponding unbind method. ReActAgent.close() was a no-op. When ephemeral/per-call ReActAgent instances are created with an AgentStateStore (e.g. via factory patterns), each constructor call registers a saver that is never removed. The stateSavers map grows unboundedly, retaining all agent instances (and their Model, HttpClient, Memory, Toolkit references) until JVM exit. In production this caused 8073+ ReActAgent instances to accumulate in stateSavers (ConcurrentHashMap capacity 16384), occupying 140MB (43% of heap). The leak also indirectly caused HttpClient thread proliferation (1600+ groups) and CLOSE-WAIT connection buildup. Fix: - Add GracefulShutdownManager.unbindStateSaver(Agent) to remove the entry - Implement ReActAgent.close() to call unbindStateSaver(this) - Users should call agent.close() (or use try-with-resources) after each per-call agent is done
sikisama
force-pushed
the
fix/graceful-shutdown-state-saver-leak
branch
from
July 24, 2026 08:43
ad88979 to
c1290ec
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes a memory leak in
GracefulShutdownManager.stateSaverswhere per-callReActAgentinstances (created withAgentStateStore) register aShutdownStateSaverin the constructor but are never unregistered.Changes
GracefulShutdownManager.java: AddunbindStateSaver(Agent)method (counterpart tobindStateSaver)ReActAgent.java: Implementclose()to callshutdownManager.unbindStateSaver(this)(was no-op)Problem
bindStateSaver()doesstateSavers.put(agentId, saver)but there was no remove/unbind path.ReActAgent.close()was a no-op. In high-throughput services creating one agent per request (as documented in the class Javadoc),stateSaversaccumulates every agent instance forever — retaining their Model, HttpClient, Memory, Toolkit, etc.Production heap dump showed 8073 ReActAgent instances retained by
stateSavers(ConcurrentHashMap capacity 16384), occupying 140MB (43% of heap), plus 1666+ HttpClient thread groups and CLOSE-WAIT connection buildup.Testing
close()is called;stateSaversstays bounded.Checklist
close()is safe to call multiple times (ConcurrentHashMap.removeis idempotent)stateSaversis only read duringperformGracefulShutdown; removing an already-closed agent's entry is correct — that agent is no longer active)stateSaverssize stays bounded after close())