Repository navigation
Fix intermittent missing enabled servers in tool retrieval - #446
Conversation
…t lifecycle Address review feedback on PR smart-mcp-proxy#446: 1. acquireListToolsContext now ALWAYS allocates listToolsWaitCh and resets the cached last-result when electing a leader. Previously the helper was used by the health-check (client.go:750) and tool-count refresh (client.go:1224) without setting up a wait channel, so a concurrent ListTools waiter would block on a nil channel for up to 30s (ctx.Done()) instead of being unblocked when the holder finished. 2. The two non-coalescing callers (health check, tool-count refresh) now call publishListToolsResult so any ListTools waiter coalesced behind them receives the real tools list rather than nil/stale data. 3. ListTools is split into a leader path (runListToolsAsLeader) that goes through acquireListToolsContext and a wait path that loops on a genuine race (rare: holder released between the failed acquire and the re-check) before parking on the wait channel. Single source of truth for leader bookkeeping. 4. Runtime: prune r.lastGoodTools entries for servers no longer in GetAllServerNames() to prevent unbounded growth on config churn. 5. Demote the "reusing last-good tool snapshot" log from Warn to Info - it's benign self-healing that can fire repeatedly during reconnect storms and was noisy. 6. New regression tests in listtools_coalescing_test.go: - TestListTools_MultipleWaitersUnblockedByLeader: real leader/multi-waiter goroutine interaction. - TestListTools_HealthCheckLeaderDoesNotDeadlockWaiters: directly reproduces the nil-channel hang the old code allowed. - TestListTools_LeaderErrorPropagatedToWaiters: error fan-out. - TestListTools_AcquireContextResetsCachedResult: leader election clears stale per-call state. Tests pass with -race on internal/upstream/managed and internal/runtime. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
|
Pushed 1. Fixed nil-channel deadlock for non-ListTools leaders
waitCh := mc.listToolsWaitCh // nil
select {
case <-ctx.Done():
case <-waitCh: // nil channel — blocks forever
}That would hang for the full 30s Fix: 2. Consolidated leader bookkeeping
3. Snapshot map no longer grows unboundedly
4. Demoted self-healing log to Info
5. Real concurrency testsThe original two tests in
VerificationThe |
|
Thanks @JasonLandbridge! Merged. Solid root-cause work on the discovery race - the coalescing primitive is now the single source of truth for in-flight ListTools calls, which makes the next round of upstream-management changes a lot easier to reason about. |
Summary
This PR fixes an intermittent issue where tools from enabled/connected servers (notably
rider-official) would sometimes not appear inretrieve_toolsresults, even though upstreamListToolscalls succeeded.The fix addresses two core causes:
ListToolscontention produced transient failures (ListTools operation already in progress) during discovery.Root cause
During server churn (restart/reconnect, reactive discovery, background discovery), multiple discovery paths could call
ListToolson the same managed client at the same time.Previously:
At the same time, global discovery accepted partial results from currently successful servers. If one connected server was omitted in that cycle, its tools were temporarily absent from indexed/searchable results.
This produced the observed “sometimes shows, sometimes missing” behavior.
What changed
1) Coalesced concurrent
ListToolscallsFile:
internal/upstream/managed/client.goListToolsconcurrency behavior:ListTools,ListTools operation already in progress for server ...Impact: prevents transient per-server tool discovery drops caused by local concurrency collisions.
2) Added last-good per-server snapshot fallback in global discovery
Files:
internal/runtime/runtime.go,internal/runtime/lifecycle.goDiscoverAndIndexTools:Impact: avoids temporary index shrink when a connected server is missed in one discovery cycle.
3) Regression tests for ListTools coalescing
File:
internal/upstream/managed/listtools_coalescing_test.goAdded tests:
TestListTools_CoalescesWaitersTestListTools_CoalescesWaitersErrorThese verify that waiters receive shared success/error instead of being rejected while a list operation is already in progress.
Verification
go test ./internal/upstream/managed ./internal/runtimepassed.build_projectsuccess).Scope and non-goals
retrieve_tools.limitmax 100).Expected behavior after fix
ListToolsraces.Follow-ups (optional)
retrieve_toolsresponses (fresh/degraded, missing/stale servers).