@@ -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 ) )
0 commit comments