Skip to content

fix(harness): share one default HttpClient across web tools (per-build leak) - #3833

Merged
juruoO-o merged 1 commit into
agentscope-ai:mainfrom
aiyili:fix/webtools-shared-client
Nov 12, 2026
Merged

juruoO-o merged 1 commit into
agentscope-ai:mainfrom
aiyili:fix/webtools-shared-client

Conversation

@aiyili

@aiyili aiyili commented Oct 28, 2026

Copy link
Copy Markdown
Contributor

Fixes #3714

Root cause (code-level confirmation from my earlier analysis on the issue)

Without an injected webHttpClient, every HarnessAgent.build() allocated two fresh java.net.http.HttpClient instances (one per web tool). On Java 17-20 those clients have no close() (added in 21), so each retains its selector/executor threads as long as the tool objects stay reachable from the toolkit. Repeated builds — per-session agent factories, config reloads, multi-instance deployments — accumulate clients linearly, matching the MAT dominator picture and the web_fetch/web_search observation from @chenxb-888.

Fix

The no-arg constructors now share one process-wide SHARED_DEFAULT_CLIENT: repeated builds become idempotent (zero additional clients), and the shared client's lifecycle intentionally matches the process — a singleton client is the JDK-documented usage pattern and HttpClient is thread-safe. Caller-supplied clients via webHttpClient(...) / the explicit constructors are untouched: their lifecycle remains the caller's.

Note

@joman-jiang's GracefulShutdownManager map hypothesis may be a separate retention node — this PR closes the confirmed per-build client accumulation; if the MAT report still shows growth after this, that map deserves its own investigation.

每次 HarnessAgent.build() 在未注入 webHttpClient 时,WebFetchTool/
WebSearchTool 各自 new 一个 java.net.http.HttpClient;Java 17-20
的 HttpClient 无 close()(21 才有),客户端及其 selector/executor
线程随工具对象可达性一直存活——按会话工厂/配置重载/多实例反复
构建即持续累积,与 MAT 报告及社区 web_fetch/web_search 观察一致。

- 无参构造改用进程级共享 SHARED_DEFAULT_CLIENT:重复构建幂等,
  生命周期与进程一致(单例 client 是 JDK 文档认可的使用方式)
- 注入式构造不受影响:caller-owned client 的生命周期仍归调用方
- 并发安全:HttpClient 本身线程安全,共享无串扰

Fixes agentscope-ai#3714
@jujn jujn added the P1 紧急不重要 label Nov 3, 2026

@juruoO-o juruoO-o left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed shared default HttpClient ownership. Explicitly injected clients retain their existing construction and ownership behavior.

No blocking findings in the reviewed diff. This was a static review of source and tests; local Java/Maven tests were not run.

@juruoO-o
juruoO-o merged commit 867c092 into agentscope-ai:main Nov 12, 2026
5 checks passed
chickenlj pushed a commit that referenced this pull request Nov 13, 2026
…d leak) (#3833)

Backport the verified fix to the 2.0.x implementation and retain its existing public contracts where the upstream kernel differs.

(cherry picked from commit 867c092)
Backport-review-patch: d13bdcb9bc7ccb466de52b9a2bf93350e9cee04e
Constraint: No AgentScope Service or SDK migration
Confidence: high
Not-tested: Whole-repository verification pending after lane integration
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

P1 紧急不重要

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: 内存泄露报告

3 participants