Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 34 additions & 19 deletions src/OpenClaw.Shared/WebSocketClientBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -186,30 +186,45 @@ private async Task ListenForMessagesAsync()

protected async Task ReconnectWithBackoffAsync()
{
var delay = BackoffMs[Math.Min(_reconnectAttempts, BackoffMs.Length - 1)];
_reconnectAttempts++;
_logger.Warn($"{ClientRole} reconnecting in {delay}ms (attempt {_reconnectAttempts})");
RaiseStatusChanged(ConnectionStatus.Connecting);
var emittedConnecting = false;

try
while (!_disposed)
{
await Task.Delay(delay, _cts.Token);
var delay = BackoffMs[Math.Min(_reconnectAttempts, BackoffMs.Length - 1)];
_reconnectAttempts++;
_logger.Warn($"{ClientRole} reconnecting in {delay}ms (attempt {_reconnectAttempts})");
if (!emittedConnecting)
{
RaiseStatusChanged(ConnectionStatus.Connecting);
emittedConnecting = true;
}

Comment on lines +191 to 201
Copy link

Copilot AI Mar 21, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ReconnectWithBackoffAsync now raises ConnectionStatus.Connecting on every retry iteration. In the tray app, OnNodeStatusChanged logs recent activity for every status change, so this can create noisy repeated "Node mode Connecting" entries while the gateway is down. Consider deduping/only emitting Connecting when transitioning from a non-connecting state, or moving the attempt counter into logs without raising a new status event each loop.

See below for a potential fix:

        // Only emit "Connecting" once when entering the reconnect loop to avoid noisy duplicates.
        RaiseStatusChanged(ConnectionStatus.Connecting);

        while (!_disposed)
        {
            var delay = BackoffMs[Math.Min(_reconnectAttempts, BackoffMs.Length - 1)];
            _reconnectAttempts++;
            _logger.Warn($"{ClientRole} reconnecting in {delay}ms (attempt {_reconnectAttempts})");

Copilot uses AI. Check for mistakes.
// Check cancellation after delay
if (_cts.Token.IsCancellationRequested) return;
try
{
await Task.Delay(delay, _cts.Token);

// Safely dispose old socket
var oldSocket = _webSocket;
_webSocket = null;
try { oldSocket?.Dispose(); } catch { /* ignore dispose errors */ }
if (_cts.Token.IsCancellationRequested) return;

await ConnectAsync();
}
catch (OperationCanceledException) { }
catch (Exception ex)
{
_logger.Error($"{ClientRole} reconnect failed", ex);
RaiseStatusChanged(ConnectionStatus.Error);
// Safely dispose old socket before retrying the connection.
var oldSocket = _webSocket;
_webSocket = null;
try { oldSocket?.Dispose(); } catch { /* ignore dispose errors */ }

await ConnectAsync();
if (IsConnected)
{
return;
}
}
catch (OperationCanceledException)
{
return;
}
catch (Exception ex)
{
_logger.Error($"{ClientRole} reconnect failed", ex);
RaiseStatusChanged(ConnectionStatus.Error);
}
}
}

Expand Down
Loading