Skip to content

fix(sse): emit servers.changed event after tool discovery completes#292

Merged
Dumbris merged 3 commits intomainfrom
fix/stale-tool-counts-race-condition
Feb 3, 2026
Merged

fix(sse): emit servers.changed event after tool discovery completes#292
Dumbris merged 3 commits intomainfrom
fix/stale-tool-counts-race-condition

Conversation

@Dumbris
Copy link
Contributor

@Dumbris Dumbris commented Feb 2, 2026

Summary

  • Fixes race condition where tool counts in Web UI showed stale data after enabling/disabling servers
  • The SSE event is now emitted AFTER asynchronous tool discovery completes, ensuring accurate stats
  • Changes affect HandleUpstreamServerChange() and RestartServer() in lifecycle.go

Root Cause

SSE events were emitted before the asynchronous tool discovery goroutine completed. When the frontend received the event and fetched stats from the REST API, StateView hadn't been updated yet, returning stale tool counts.

Solution

Move emitServersChanged() inside the tool discovery goroutine so it fires after DiscoverAndIndexTools() completes. The event payload now includes "reason": "tool_discovery_complete" for clarity.

Test plan

  • Linter passes (./scripts/run-linter.sh)
  • Unit tests pass (go test ./internal/runtime/...)
  • E2E tests pass (./scripts/test-api-e2e.sh)
  • Manual verification: Enable/disable a server and confirm tool counts update within 5 seconds

Fixes #285

🤖 Generated with Claude Code

Fix race condition where tool counts in Web UI showed stale data after
enabling/disabling servers. The issue was that SSE events were emitted
before the asynchronous tool discovery completed, causing the frontend
to fetch stats from StateView before it was updated.

Changes:
- Move emitServersChanged() inside the tool discovery goroutine in both
  HandleUpstreamServerChange() and RestartServer()
- Add "reason": "tool_discovery_complete" to event payload for clarity
- Change HandleUpstreamServerChange event type from "upstream_change"
  to "tools_indexed" to better reflect when it's emitted

The frontend now receives the SSE event only after tool counts are
accurate, ensuring consistent UI updates within ~3-5 seconds.

Fixes #285

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@cloudflare-workers-and-pages
Copy link

cloudflare-workers-and-pages bot commented Feb 2, 2026

Deploying mcpproxy-docs with  Cloudflare Pages  Cloudflare Pages

Latest commit: cccdfd8
Status: ✅  Deploy successful!
Preview URL: https://9294710f.mcpproxy-docs.pages.dev
Branch Preview URL: https://fix-stale-tool-counts-race-c.mcpproxy-docs.pages.dev

View logs

@github-actions
Copy link

github-actions bot commented Feb 2, 2026

📦 Build Artifacts

Workflow Run: View Run
Branch: fix/stale-tool-counts-race-condition

Available Artifacts

  • archive-darwin-amd64 (23 MB)
  • archive-darwin-arm64 (21 MB)
  • archive-linux-amd64 (12 MB)
  • archive-linux-arm64 (11 MB)
  • archive-windows-amd64 (23 MB)
  • archive-windows-arm64 (21 MB)
  • frontend-dist-pr (0 MB)
  • installer-dmg-darwin-amd64 (26 MB)
  • installer-dmg-darwin-arm64 (23 MB)

How to Download

Option 1: GitHub Web UI (easiest)

  1. Go to the workflow run page linked above
  2. Scroll to the bottom "Artifacts" section
  3. Click on the artifact you want to download

Option 2: GitHub CLI

gh run download 21630313261 --repo smart-mcp-proxy/mcpproxy-go

Note: Artifacts expire in 14 days.

Dumbris and others added 2 commits February 2, 2026 21:42
Root cause fixes for stale tool counts:
- TotalTools stat now excludes disabled servers' tool counts
- DeleteServerTools called when server is disabled to remove from index

Tests added:
- Unit test: TotalTools excludes disabled servers (service_test.go)
- Unit test: Disabled server removes tools from index (tool_invalidation_test.go)
- E2E test: Full disable/enable cycle with search verification

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
The frontend was summing all servers' tool_count regardless of enabled
status, causing Total Tools to show stale counts when servers are
disabled.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@Dumbris Dumbris merged commit 29cf86e into main Feb 3, 2026
24 checks passed
@Dumbris Dumbris deleted the fix/stale-tool-counts-race-condition branch February 3, 2026 12:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Tool counts don't update after enabling/disabling servers (stale stats race condition)

1 participant