Skip to content

[Bug]: 内存泄露报告 #3714

Closed
Closed

Description

@joman-jiang

版本:2.0.4
问题:服务长时间运行导致环境异常,内存占用。下面是mat分析报告

Image Image

Leak Suspects_files.zip

Activity

  1. added
    bug
    on Oct 22, 2026
  2. chenxb-888 commented on Oct 22, 2026

    @chenxb-888

    我也发现了类似问题,通过AI分析,说是web_fetch / web_search这两个新增加的工具持有的HttpClient 没有关闭造成的。
    另外,如果有注册mcp给harnessagent,注册的mcp也可能持有HttpClient 导致资源不释放。

  3. joman-jiang commented on Oct 22, 2026

    @joman-jiang
    Author

    感觉根因因该是GracefulShutdownManager实例中的ConcurrentHashMap得不到remove,导致内存无法回收吧。

  4. xiaoshuai288 commented on Oct 22, 2026

    @xiaoshuai288

    应该要调用reActAgent.close();吧,因为每次都是无状态的调用,用完就不需要跟踪请求,直接关闭生命周期

     return reActAgent.streamEvents(inputMsgList, ctx)
                        .doOnNext(event -> agentEventHelper.consumeAgentEvent(sessionId, event))
                        .doOnComplete(() -> log.info("SSE流完成, sessionId={}", sessionId))
                        .doOnError(err -> log.error("SSE流异常, sessionId={}", sessionId, err))
                        .doFinally(signal -> {
                            reActAgent.close();
                        });
  5. aiyili commented on Oct 23, 2026

    @aiyili
    Contributor

    Hi, I'd like to work on this. The MAT report points at retained clients; the community note about web_fetch/web_search HttpClients is a concrete lead. Plan: audit the tool/extension classes holding OkHttpClient/HttpClient/MCP connections for close()/shutdown paths, add explicit lifecycle release, and (if confirmed) reference the specific retention chain from the MAT dominator tree. Will post findings before the fix since leak attribution from a screenshot alone needs code-level confirmation. PR coming shortly.

  6. 1065609785 commented on Oct 23, 2026

    @1065609785

    我也遇到了,时间长了,系统无反应,官方什么时间能够修复呢?

  7. aiyili commented on Oct 23, 2026

    @aiyili
    Contributor

    @1065609785 To answer your timing question honestly: this is a community-tracked issue — I've claimed it and just finished the code-level confirmation, so the timeline depends on maintainer review of the fix rather than an official schedule. Here's what I found, plus workarounds you can apply today.

    Code-level leak chain (confirmed in WebTools.java + HarnessAgent.java:2738-2744):

    • When web tools are enabled, every HarnessAgent.builder()...build() creates two fresh java.net.http.HttpClient instances (one each for WebFetchTool/WebSearchTool) via the no-arg constructors.
    • On Java 17–20 those clients cannot be closed (no close() method — added only in Java 21), and each retains its internal selector/executor threads as long as the tool object is reachable from the toolkit.
    • Repeated agent builds (per-session agent factories, config reloads, multiple HarnessAgent instances) therefore accumulate clients and their threads — consistent with your MAT dominator picture and @chenxb-888's web_fetch/web_search observation. @joman-jiang's GracefulShutdownManager map is plausibly the same retention showing up at a different node.

    Interim workarounds:

    1. If you build agents repeatedly, pass one shared, caller-owned client: HarnessAgent.builder().webHttpClient(sharedClient)... — then only one client exists regardless of build count, and its lifecycle is yours to manage.
    2. Or disableWebTools() and register a single custom web tool instance yourself.

    Planned fix: reuse a single client across tool instances (and make repeated builds idempotent), plus a shutdown hook where the JDK version supports it. PR to follow after maintainer review of the approach.

  8. aiyili commented on Oct 28, 2026

    @aiyili
    Contributor

    Following up on my earlier code-level analysis (the two-fresh-HttpClient-per-build chain): implementing the fix now on the agreed direction — reuse a single shared client across WebFetchTool/WebSearchTool instances when the builder doesn't supply one (idempotent repeated builds), leaving caller-supplied clients untouched. PR coming shortly.

  9. added a commit that references this issue on Nov 12, 2026
    867c092
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions