openpets icon indicating copy to clipboard operation
openpets copied to clipboard

HTTP MCP endpoint degrades to 0–50% success after the app has been running for a while — leaked CLOSED connections accumulate (`SelectablePipeHandle already closed`)

Open ericyhm80 opened this issue 2 months ago • 0 comments

Summary

The HTTP MCP endpoint (http://127.0.0.1:3001/mcp) is unreliable under repeated or concurrent requests. Measured success rate swings between 0% and 50% across runs, and it gets worse the longer the app has been running. The process accumulates leaked CLOSED TCP connections (239 after ~24 h of uptime). After a restart the endpoint immediately returns to 20/20.

The Unix socket / openpets-cli path is unaffected in quiet conditions, which is why the CLI still works while the HTTP endpoint fails.

Environment

  • macOS (Apple Silicon), OpenPets 1.0.0, endpoint http://127.0.0.1:3001/mcp
  • Also observed with two independent consumers configured against the same endpoint (two agent frameworks each with url = "http://127.0.0.1:3001/mcp")

What we measured

Request body used for all measurements: {"jsonrpc":"2.0","id":1,"method":"ping","params":{}} Headers: Content-Type: application/json, Accept: application/json, text/event-stream

Scenario Success
Sequential single client, N=25 75/75
Two concurrent clients (separate connection pools) A 20/20, B 0/20
One client, 5 parallel connections 20/100
Client A does 8 requests, keeps its pool idle → client B does 8 A 8/8, B 0/8
… then A.close() and repeat B B 8/8
openpets-cli ping (Unix socket), N=30 30/30

The strongest evidence: restart A/B

Same machine, same test script, only difference = fresh process:

Metric Before restart (24 h uptime) After restart (15 s)
Process fds 294 49
TCP connections in CLOSED state 239 0
HTTP ping ×20 (reused connections) 0–50% 20/20
HTTP ping ×20 (Connection: close) 0/30 or 20/20 (unstable) 20/20

The 239 CLOSED entries were measured on the OpenPets process with:

PID=$(pgrep -x OpenPets)
lsof -nP -p $PID -iTCP | awk '{print $NF}' | sort | uniq -c | sort -rn
# 239 (CLOSED) / 69 (ESTABLISHED) / 39 (LISTEN) / 20 (CLOSE_WAIT)  — total 367 TCP rows, 294 fds

Minimal reproduction

# 1) Fresh app: expect ~20/20
for i in $(seq 1 20); do
  curl -s -o /dev/null -w "%{http_code} " -X POST http://127.0.0.1:3001/mcp \
    -H 'Content-Type: application/json' -H 'Accept: application/json, text/event-stream' \
    -d '{"jsonrpc":"2.0","id":1,"method":"ping","params":{}}'
done; echo

# 2) Let the app run for hours (or drive traffic to accelerate), then re-run step 1
#    and watch leaked handles grow:
lsof -nP -p $(pgrep -x OpenPets) -iTCP | grep -c '(CLOSED)'

Two parallel loops are enough to reproduce the "one client starves" behaviour: run the loop above in two terminals — one of them typically gets 100% failures while the other succeeds, and the winner stays the winner.

Why I believe this is a handle-lifecycle bug

The binary contains SwiftNIO HTTP server code (NIOHTTP1, KeepAliveState, enablePipelining, SelectablePipeHandle) and the internal error string

SelectablePipeHandle already closed [in close]

which suggests a close path that can run twice and/or skip releasing the handle. The accumulating CLOSED rows (239) and the fact that a restart fixes it both point at per-connection handle lifecycle rather than request parsing. We also see 39 LISTEN rows for a single listening port, which looks unexpected too.

Suggested places to look:

  1. accepted-channel lifecycle: is every connection given its own pipe handle, and is close idempotent?
  2. the SelectablePipeHandle close path (double close / missing release)
  3. why one origin shows many LISTEN entries

Ruled out (to save you time)

  • app not running / port not listening — no (process alive, port LISTEN, HTTP responds in 7 ms)
  • client-side problem (curl vs Node) — no (both fail the same way)
  • MCP session expiry — no (an old mcp-session-id still works; requests without the header work too)
  • client proxy settings — no (the failing client process has no proxy env vars)
  • NO_PROXY=127.0.0.1,localhost — measured 1/20, does not help
  • client dispatcher type — default undici agent, custom Agent/Pool, and EnvHttpProxyAgent each produced both 0% and 100% in different runs, so it is not the deciding factor

Impact

Any integration with more than one consumer — two agent frameworks, CLI + HTTP mixed, or a stale idle connection — sees one side fail ~100% of the time. Because the failure rate depends on uptime, users experience it as "sometimes it works, sometimes it doesn't".

Workaround we use

We drive the pet through openpets-cli (Unix socket) instead of the HTTP endpoint, with retries. Cumulative CLI measurements: 60/60. This is reliable but only one connection's worth of concurrency — it would be great to have the HTTP endpoint fixed.

Thanks — happy to run any additional measurements you'd like on this machine.

ericyhm80 avatar Sep 17 '26 17:09 ericyhm80