Skip to content

Commit 019bdbe

Browse files
committed
Fix duplicate connection verification logs: add debounce and state-change-only logging
1 parent b57a2ec commit 019bdbe

File tree

2 files changed

+60
-7
lines changed

2 files changed

+60
-7
lines changed

MCPForUnity/Editor/Windows/Components/Connection/McpConnectionSection.cs

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ private enum TransportProtocol
4444
private Button testConnectionButton;
4545

4646
private bool connectionToggleInProgress;
47+
private Task verificationTask;
48+
private string lastHealthStatus;
4749

4850
// Events
4951
public event Action OnManualConfigUpdateRequested;
@@ -408,15 +410,34 @@ private async void OnTestConnectionClicked()
408410
}
409411

410412
public async Task VerifyBridgeConnectionAsync()
413+
{
414+
// Prevent concurrent verification calls
415+
if (verificationTask != null && !verificationTask.IsCompleted)
416+
{
417+
return;
418+
}
419+
420+
verificationTask = VerifyBridgeConnectionInternalAsync();
421+
await verificationTask;
422+
}
423+
424+
private async Task VerifyBridgeConnectionInternalAsync()
411425
{
412426
var bridgeService = MCPServiceLocator.Bridge;
413427
if (!bridgeService.IsRunning)
414428
{
415429
healthStatusLabel.text = "Disconnected";
416430
healthIndicator.RemoveFromClassList("healthy");
417431
healthIndicator.RemoveFromClassList("warning");
432+
healthIndicator.RemoveFromClassList("unknown");
418433
healthIndicator.AddToClassList("unknown");
419-
McpLog.Warn("Cannot verify connection: Bridge is not running");
434+
435+
// Only log if state changed
436+
if (lastHealthStatus != "Disconnected")
437+
{
438+
McpLog.Warn("Cannot verify connection: Bridge is not running");
439+
lastHealthStatus = "Disconnected";
440+
}
420441
return;
421442
}
422443

@@ -426,23 +447,45 @@ public async Task VerifyBridgeConnectionAsync()
426447
healthIndicator.RemoveFromClassList("warning");
427448
healthIndicator.RemoveFromClassList("unknown");
428449

450+
string newStatus;
429451
if (result.Success && result.PingSucceeded)
430452
{
431-
healthStatusLabel.text = "Healthy";
453+
newStatus = "Healthy";
454+
healthStatusLabel.text = newStatus;
432455
healthIndicator.AddToClassList("healthy");
433-
McpLog.Debug($"Connection verification successful: {result.Message}");
456+
457+
// Only log if state changed
458+
if (lastHealthStatus != newStatus)
459+
{
460+
McpLog.Debug($"Connection verification successful: {result.Message}");
461+
lastHealthStatus = newStatus;
462+
}
434463
}
435464
else if (result.HandshakeValid)
436465
{
437-
healthStatusLabel.text = "Ping Failed";
466+
newStatus = "Ping Failed";
467+
healthStatusLabel.text = newStatus;
438468
healthIndicator.AddToClassList("warning");
439-
McpLog.Warn($"Connection verification warning: {result.Message}");
469+
470+
// Always log warnings/errors
471+
if (lastHealthStatus != newStatus)
472+
{
473+
McpLog.Warn($"Connection verification warning: {result.Message}");
474+
lastHealthStatus = newStatus;
475+
}
440476
}
441477
else
442478
{
443-
healthStatusLabel.text = "Unhealthy";
479+
newStatus = "Unhealthy";
480+
healthStatusLabel.text = newStatus;
444481
healthIndicator.AddToClassList("warning");
445-
McpLog.Error($"Connection verification failed: {result.Message}");
482+
483+
// Always log errors
484+
if (lastHealthStatus != newStatus)
485+
{
486+
McpLog.Error($"Connection verification failed: {result.Message}");
487+
lastHealthStatus = newStatus;
488+
}
446489
}
447490
}
448491
}

MCPForUnity/Editor/Windows/MCPForUnityEditorWindow.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ public class MCPForUnityEditorWindow : EditorWindow
2121

2222
private static readonly HashSet<MCPForUnityEditorWindow> OpenWindows = new();
2323
private bool guiCreated = false;
24+
private double lastRefreshTime = 0;
25+
private const double RefreshDebounceSeconds = 0.5;
2426

2527
public static void ShowWindow()
2628
{
@@ -181,6 +183,14 @@ private void OnEditorUpdate()
181183

182184
private void RefreshAllData()
183185
{
186+
// Debounce rapid successive calls (e.g., from OnFocus being called multiple times)
187+
double currentTime = EditorApplication.timeSinceStartup;
188+
if (currentTime - lastRefreshTime < RefreshDebounceSeconds)
189+
{
190+
return;
191+
}
192+
lastRefreshTime = currentTime;
193+
184194
connectionSection?.UpdateConnectionStatus();
185195

186196
if (MCPServiceLocator.Bridge.IsRunning)

0 commit comments

Comments
 (0)