Skip to content

[Bug]: Tool bridge creates excessive TIME_WAIT connections — need connection pooling for tcp://127.0.0.1:3003 #41612

Closed

Description

@longman888

Bug Description

每次 tool call(terminal/execute_code)都新建 TCP 连接到 bridge,不复用。高频使用时数百个 TIME_WAIT 占满 Windows 端口池。建议加连接池或 keep-alive 复用。

Steps to Reproduce

在 Windows 10/11 上运行 Hermes Agent
持续使用 terminal 和 execute_code 工具(10+ 次调用)
运行 netstat -ano | find "TIME_WAIT" | find "3003" /c
观察每次工具调用都新增一个 TIME_WAIT 连接到 bridge 端口 3003,不复用,累积后 Windows 端口池耗尽

Expected Behavior

工具桥接应复用已有 TCP 长连接,而不是每次调用新建一个。高频使用时 TIME_WAIT 连接数应维持在个位数,不再随调用次数线性增长。

Actual Behavior

每次 terminal/execute_code 调用都在 127.0.0.1 上新建一个 TCP 连接到 bridge 端口,用完立即断开进入 TIME_WAIT。调用 50+ 次后累积 500+ 个 TIME_WAIT,导致 Windows 端口池(默认约 16,000 个临时端口)耗尽,报错 WSAENOBUFS (10055),任何新 Socket 连接都被拒绝。需手动修改注册表 TcpTimedWaitDelay 缩短释放时间才能缓解。

Affected Component

Gateway (Telegram/Discord/Slack/WhatsApp), Other

Messaging Platform (if gateway-related)

No response

Debug Report

> Hermes: 0.16.0 (pip install)
> OS: Windows 10 10.0.26200.8524
> 
> $ netstat -ano | find "TIME_WAIT" /c
> 453
> 
> $ netstat -ano | find "3003" /c | find "TIME_WAIT" /c
> 141     ← 桥接连接占 TIME_WAIT 总量的 31%
> 
> $ netsh int ipv4 show dynamicport tcp
> (未显示自定义范围,使用默认约 16,384 个临时端口)
> 
> 典型报错:
> ERROR: connect ENOBUFS 127.0.0.1:64886 - Local (undefined:undefined)
> WSAENOBUFS (10055)
>

Operating System

Windows 11

Python Version

No response

Hermes Version

No response

Additional Logs / Traceback (optional)

Root Cause Analysis (optional)

No response

Proposed Fix (optional)

No response

Are you willing to submit a PR for this?

  • I'd like to fix this myself and submit a PR

Activity

  1. added
    type/perfPerformance improvement or optimization
    P2Medium — degraded but workaround exists
    comp/toolsTool registry, model_tools, toolsets
    on Jun 8, 2026
  2. Mrchen116 commented on Jun 11, 2026

    @Mrchen116

    Confirming this on macOS — with much more severe consequences than the Windows report.

    Environment:

    • Hermes gateway × 2 (hermes_cli.main gateway run --replace, one with --profile finance), running unattended for ~1 month
    • macOS 26 (Darwin 25.2.0), Apple Silicon Mac mini, uptime 50 days
    • Outbound traffic routed through a local Clash proxy on 127.0.0.1:7890

    What happened:

    • 27,000+ TIME_WAIT sockets accumulated (netstat -an -p tcp | grep -c TIME_WAIT), of which 7,151 targeted 127.0.0.1:7890 (the local proxy) — same no-reuse, connect-per-call pattern described above. The storm was amplified when the proxy app was restarted on a different port: hermes kept hammering the dead port with retries (likely related: the zero-backoff retry path in [Enhancement] Empty response retries have no backoff delay — immediate retry wastes API calls #35230).
    • macOS's default ephemeral port range (49152–65535, 16,384 ports) was fully exhausted. Every new outbound TCP connection failed with EADDRNOTAVAIL ("Can't assign requested address") — the macOS equivalent of the WSAENOBUFS (10055) in the original report.
    • The machine became effectively network-dead for TCP: curl to any host failed in ~1ms, even nc 127.0.0.1 22 failed. Only ICMP/ping and DNS (existing UDP sockets) still worked. The gateway processes themselves were wedged hard enough that they no longer responded to SIGTERM — only kill -9 worked.
    • Killing the two gateway processes instantly restored all TCP connectivity, confirming hermes held the exhausted ports.

    Repro signal for anyone checking their own box:

    netstat -an -p tcp | grep -c TIME_WAIT          # thousands = affected
    lsof -i tcp -n -P | grep -c hermes              # sockets held by gateway
    

    +1 for connection pooling / socket reuse (#41638). A capped retry with jittered_backoff on the bridge/proxy connect path would also prevent the dead-port hammering variant.

  3. GottZ commented on Jul 31, 2026

    @GottZ

    This was generated by AI during triage.

    Summary

    One PR addresses the open issue #41612. #41638 modifies the RPC client path, but its diff adds documentation and reconnect-on-error logic rather than introducing the requested pooling: the module-global socket was already reused on main.

    Related pull requests

    • fix(41612): Reuse RPC socket across tool calls to prevent TIME_WAIT buildup on Windows #41638 [closed] unrelated — (+41/-11) — unrelated: The diff preserves the existing module-global _sock reuse and mainly adds a reconnect-and-retry path, so it does not fix the claimed connect-per-call cause. The closed PR remains relevant as an attempted fix, but the maintainer-bot close verdict further notes that tools/code_execution_tool.py:505-524 exits after the first client disconnects, making the proposed replacement-client retry incompatible with the server lifecycle; the contributor-reported stale-conn defect was corrected in d4972a6 but does not resolve that lifecycle mismatch.

    Suggested consolidation

    Keep #41638 closed as already implemented on main, consistent with the maintainer-bot verdict: its cited evidence is tools/code_execution_tool.py:350-381 and :390-400, where the cached client socket is created only when absent and then reused, with Windows loopback support attributed there to e93bfc6c93bfa6f9edd02629a03f717fc29ce013. There are no competing PRs or duplicate PRs to consolidate; #41612 remains open, so any further work should first reproduce the reported socket growth against current main and identify a cause outside the already-cached RPC client path.

    Cross-PR triage: Reviewed 1 pull request and 1 issue in this complex. Each diff was read against this issue; Assessment working set: 3 kB of PR diffs, 5 kB of issue/PR text, 4 kB of discussion (4 comments), 0 verify verdicts. verdicts reflect diff content, not PR titles. Part of an automated triage batch.

  4. kshitijk4poor commented on Sep 20, 2026

    @kshitijk4poor

    Closing as fixed on main.

    The per-call connect/close lifecycle that produced TIME_WAIT growth is gone: execute_code now runs a session-persistent kernel (#94647, b39d76d) that serves RPC over one connection for the kernel's whole life (tools/code_kernel.py::_rpc_forever, HERMES_RPC_PERSISTENT=1), and the client stub reuses a module-global socket under a lock (tools/code_execution_tool.py), reconnecting only after the 300 s idle disconnect. There is no fixed :3003 bridge anywhere in the tree (ephemeral loopback port on Windows, UDS elsewhere). Credit to @iamlukethedev (#41638) for the socket-reuse idea.

    @Mrchen116's data point (7,151 TIME_WAIT to a local Clash proxy on :7890) is outbound HTTP through a proxy — a different mechanism tracked under #35230, not the tool bridge.

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

    P2Medium — degraded but workaround existscomp/toolsTool registry, model_tools, toolsetstype/perfPerformance improvement or optimization

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions