Skip to content

Commit d7d153c

Browse files
fix(connection): recover saved SSH tunnels after disconnect (#1070)
* fix(connection): recover registry-backed SSH tunnels * fix(connection): harden SSH tunnel recovery Co-authored-by: Copilot App <[email protected]> Copilot-Session: 7b9bc681-ce19-4a8b-b06c-94efb63a5f9f --------- Co-authored-by: Scott Hanselman <[email protected]> Copilot-Session: 7b9bc681-ce19-4a8b-b06c-94efb63a5f9f
1 parent 9acafa6 commit d7d153c

11 files changed

Lines changed: 834 additions & 110 deletions

‎src/OpenClaw.Connection/GatewayConnectionManager.cs‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -877,6 +877,47 @@ public async Task<bool> ReconnectIfCurrentAsync(string gatewayId, CancellationTo
877877
}
878878
}
879879

880+
public async Task<bool> RecoverSshTunnelAsync(SshTunnelExit tunnelExit)
881+
{
882+
ThrowIfDisposed();
883+
await _transitionSemaphore.WaitAsync();
884+
try
885+
{
886+
var activeGateway = _registry.GetActive();
887+
if (tunnelExit.Owner != SshTunnelOwner.GatewayConnectionManager ||
888+
activeGateway?.SshTunnel != tunnelExit.Tunnel ||
889+
!IsAutomaticReconnectAllowed(activeGateway.Id) ||
890+
_tunnelManager?.IsRestartPending(tunnelExit) != true)
891+
{
892+
return false;
893+
}
894+
895+
// DisconnectCoreAsync retires the gateway clients but deliberately leaves the
896+
// tunnel alone. Keep its generation token valid until ConnectCoreAsync replaces
897+
// the failed process, while preventing a delayed callback from reviving an old gateway.
898+
await DisconnectCoreAsync();
899+
900+
var currentGateway = _registry.GetActive();
901+
if (currentGateway?.Id != activeGateway.Id ||
902+
currentGateway.SshTunnel != tunnelExit.Tunnel ||
903+
!IsAutomaticReconnectAllowed(activeGateway.Id) ||
904+
_tunnelManager?.IsRestartPending(tunnelExit) != true)
905+
{
906+
return false;
907+
}
908+
909+
await ConnectCoreAsync(activeGateway.Id, "reconnect");
910+
return _stateMachine.Current.OperatorState is not RoleConnectionState.Error
911+
&& _registry.GetById(activeGateway.Id) is not null
912+
&& _tunnelManager?.IsActive == true
913+
&& string.Equals(_registry.ActiveGatewayId, activeGateway.Id, StringComparison.Ordinal);
914+
}
915+
finally
916+
{
917+
_transitionSemaphore.Release();
918+
}
919+
}
920+
880921
public void SetGatewayConnectionIntent(string gatewayId, bool shouldBeConnected)
881922
{
882923
if (string.IsNullOrWhiteSpace(gatewayId))

‎src/OpenClaw.Connection/IGatewayConnectionManager.cs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public interface IGatewayConnectionManager : IDisposable, IAsyncDisposable
2525
Task DisconnectByUserAsync();
2626
Task ReconnectAsync();
2727
Task<bool> ReconnectIfCurrentAsync(string gatewayId, CancellationToken cancellationToken = default);
28+
Task<bool> RecoverSshTunnelAsync(SshTunnelExit tunnelExit);
2829
Task SwitchGatewayAsync(string gatewayId);
2930
void SetGatewayConnectionIntent(string gatewayId, bool shouldBeConnected);
3031
bool IsAutomaticReconnectAllowed(string gatewayId);

‎src/OpenClaw.Connection/ISshTunnelManager.cs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ namespace OpenClaw.Connection;
77
public interface ISshTunnelManager : IDisposable
88
{
99
bool IsActive { get; }
10+
bool IsRestartPending(SshTunnelExit tunnelExit);
1011
Task<string> StartAsync(SshTunnelConfig config, CancellationToken ct);
1112
Task StopAsync();
1213
string? LocalTunnelUrl { get; }
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
namespace OpenClaw.Connection;
2+
3+
public sealed class SshTunnelRecoveryBudget
4+
{
5+
private static readonly TimeSpan AttemptWindow = TimeSpan.FromMinutes(5);
6+
private static readonly TimeSpan[] RetryDelays =
7+
[
8+
TimeSpan.FromSeconds(3),
9+
TimeSpan.FromSeconds(10),
10+
TimeSpan.FromSeconds(30)
11+
];
12+
13+
private readonly object _gate = new();
14+
private readonly Queue<DateTimeOffset> _attempts = new();
15+
private SshTunnelConfig? _tunnel;
16+
private SshTunnelOwner _owner;
17+
18+
public bool TryReserve(
19+
SshTunnelExit tunnelExit,
20+
DateTimeOffset now,
21+
out TimeSpan delay)
22+
{
23+
lock (_gate)
24+
{
25+
if (!Equals(_tunnel, tunnelExit.Tunnel) || _owner != tunnelExit.Owner)
26+
{
27+
_attempts.Clear();
28+
_tunnel = tunnelExit.Tunnel;
29+
_owner = tunnelExit.Owner;
30+
}
31+
32+
while (_attempts.TryPeek(out var attempt) && now - attempt >= AttemptWindow)
33+
_attempts.Dequeue();
34+
35+
if (_attempts.Count >= RetryDelays.Length)
36+
{
37+
delay = TimeSpan.Zero;
38+
return false;
39+
}
40+
41+
delay = RetryDelays[_attempts.Count];
42+
_attempts.Enqueue(now);
43+
return true;
44+
}
45+
}
46+
47+
public void ReportRecovered(SshTunnelExit tunnelExit)
48+
{
49+
lock (_gate)
50+
{
51+
if (!Equals(_tunnel, tunnelExit.Tunnel) || _owner != tunnelExit.Owner)
52+
return;
53+
54+
ResetLocked();
55+
}
56+
}
57+
58+
public void Reset()
59+
{
60+
lock (_gate)
61+
ResetLocked();
62+
}
63+
64+
private void ResetLocked()
65+
{
66+
_attempts.Clear();
67+
_tunnel = null;
68+
_owner = SshTunnelOwner.Unspecified;
69+
}
70+
}

0 commit comments

Comments
 (0)