Skip to content

[BUG] Data race on tool Registry.tools map between async MCP registration and tool lookups #708

Closed

Description

@edenreich

Summary

The tool Registry (internal/agent/tools/registry.go) stores its tools in a plain tools map[string]domain.Tool with no mutex. The map is written concurrently from two places with no synchronization:

  • Async writers: RegisterMCPServerTools / UnregisterMCPServerTools run from the MCP liveness-probe goroutines started by MCPManager.StartMonitoring (go in internal/services/mcp_manager.go), which register/unregister MCP_* tools as servers come and go.
  • Main-loop readers: GetTool, ListAvailableTools, GetToolDefinitions, IsToolEnabled read/iterate the same map during tool listing and execution on the Bubble Tea loop.

There is no lock between these, so it is a data race on the map: go test -race / a -race build will flag it, and in the worst case the runtime panics with fatal error: concurrent map read and map write.

This is pre-existing (predates the memory work) and was surfaced while reviewing #707. Note: #707's new Registry.SetMemoryBackend also writes this map, but only at container construction (before StartMonitoring spawns the probe goroutines), so it does not add a new race - it just sits next to this one.

Fix direction: guard tools with a sync.RWMutex (RLock on the read accessors, Lock on register/unregister), or funnel MCP (un)registration through the tea loop so all map access is single-threaded. A -race test that lists tools while RegisterMCPServerTools runs from a goroutine would lock the regression in.

Steps to Reproduce

  1. Configure one or more MCP servers with the liveness probe enabled (mcp.yaml).
  2. Build/run under the race detector (e.g. go build -race, or a -race integration test around the registry).
  3. Drive tool listing/execution (open infer chat, let the agent enumerate tools) while the MCP liveness probe concurrently registers/unregisters MCP tools (normal startup timing, or a server that flaps).

Expected Behavior

Access to the registry's tools map is synchronized; no data race regardless of MCP probe timing.

Actual Behavior

r.tools is read on the main loop (GetTool / ListAvailableTools / GetToolDefinitions) while written from the MCP probe goroutine (RegisterMCPServerTools / UnregisterMCPServerTools) with no lock, producing a data race (reported by -race; a rare fatal error: concurrent map read and map write panic is possible without it).

Activity

  1. added
    bug
    on Jul 1, 2026
  2. added theissue type on Jul 1, 2026
  3. moved this to Todo in 2026on Jul 1, 2026
  4. inference-gateway-releaser commented on Jul 2, 2026

    @inference-gateway-releaser
    Contributor

    🎉 This issue has been resolved in version 0.127.0 🎉

    The release is available on:

    Your semantic-release bot 📦🚀

  5. moved this from Todo to Done in 2026on Jul 2, 2026
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

    bugreleased

    Type

    Projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions