mcpproxy-go icon indicating copy to clipboard operation
mcpproxy-go copied to clipboard

v0.70.0: an abandoned StreamActivities read transaction blocks a bbolt remap and, with it, every agent-token lookup

Open mkznz opened this issue 1 month ago • 1 comments

Summary

On v0.70.0 (Docker), the gateway stopped answering every authenticated request while its unauthenticated health endpoint kept answering in about 1 ms. It stayed that way for over 40 minutes until the container was recreated. A goroutine dump shows why.

What I saw

  • GET /healthz → 200 in ~1 ms throughout.
  • Unauthenticated GET /api/v1/servers → 401 in ~3 ms.
  • Authenticated GET /api/v1/servers → no response in 40 s, three tries.
  • An MCP initialize with a valid agent token → no response in 40 s.
  • A valid agent token was being answered 401 by some clients rather than timing out, so it looked like a bad credential.
  • CPU ~2 %, memory ~290 MiB of 1 GiB, host idle. Not load.

The goroutine dump (/debug/pprof/goroutine?debug=2, 452 goroutines)

  • 115 goroutines in sync.RWMutex.RLock inside internal/storage.(*Manager).GetAgentTokenByHash, the lookup every authenticated request performs. Some had waited 9 minutes.
  • One goroutine waiting for the write lock, 10 minutes, in bbolt.(*DB).mmap ← (*DB).allocate ← (*Tx).allocate ← (*Tx).commitFreelist ← (*Tx).Commit ← (*DB).Update ← internal/storage.(*BoltDB).IncrementToolStats. A write that needs the file to grow must remap, and the remap needs the mmap lock exclusively.
  • One read transaction that never ended, 130 minutes: internal/storage.(*Manager).StreamActivities.func1.1 inside bbolt.(*DB).View (db.go:953), in goroutine state chan send: blocked sending on a channel nobody was receiving from.

A pending writer on a Go RWMutex blocks all new readers, so the token lookups queued behind it.

Why I think this is the cause

This is the documented bbolt hazard: a long-lived read transaction plus a remap deadlocks the database. StreamActivities holds its View open while it sends on a channel, so a consumer that goes away without draining it leaves the transaction open for ever.

Second occurrence, 22 Oct 2026 (same version, v0.70.0)

Same failure about 11.5 hours after the container was recreated from the first one (created 21:22Z on 21 Oct, hung 08:59Z on 22 Oct): authenticated requests hung for 34 minutes (probe red 08:59Z to 09:33Z, recreated 09:34Z) while /health answered 200 in 45 ms. Dump taken mid-hang. I can share both full dumps on request.

  • 813 goroutines; 486 of them in GetAgentTokenByHash, one stack each. (First time: 452 and 115.)
  • One writer in sync.RWMutex.Lock, 30 minutes, the bbolt mmap path again.
  • One StreamActivities read transaction in chan send, 726 minutes, so opened about 21:23Z on 21 Oct, about a minute after the container started (created 21:22Z). A second StreamActivities goroutine sat in chan receive for 155 minutes. Nothing here points at a human opening a stream: it looks like something the proxy or a client does on start-up.
  • The transaction's age does not predict the hang (130 minutes, then 12 hours); my reading is that the hang waits for a write that needs a remap, which I have not shown.

Still not established: which client opens the stream. Any long-lived consumer that stops reading would do.

Suggested direction (yours to judge)

  • Do not hold a bbolt read transaction across a channel send in StreamActivities: snapshot the batch inside the View, return, then send.
  • Give the send a context, so a consumer that disconnects releases the transaction.

Environment

mcpproxy v0.70.0, Docker image, linux/amd64, bbolt v1.5.0. v0.71.1 was current at the time; its release notes and v0.71.0-rc.1's mention nothing on bbolt, locks or the activity stream, so I do not know whether it is fixed there. I have not tried to reproduce it on v0.71.1.

Workaround

Recreating the container clears it.

mkznz avatar Oct 23 '26 02:10 mkznz

Fixed in #1743, now merged. Thank you, @mkznz! Your goroutine dumps pointed straight at the cause: StreamActivities held a BBolt read transaction while it waited to send, so one stalled consumer blocked the file remap and every agent-token lookup queued behind it. The stream now reads in short batches and never holds a transaction while it waits to send, and a regression test covers a stalled consumer that never cancels.

Reports this thorough make a real difference to MCPProxy. One small ask, only if you feel like it: a short post about MCPProxy on X, LinkedIn or a community you're part of would help more people find the project. No pressure at all, and I'd be glad to repost it.

Dumbris avatar Oct 23 '26 05:10 Dumbris