Skip to content

Commit c320136

Browse files
ranjeshjCopilot
authored andcommitted
fix(setup): wait for operator gateway reachability
Add the same Windows-side localhost gateway reachability probe to PairOperatorStep that PairNodeStep already uses before connecting over WebSocket. This lets the setup retry policy absorb WSL port-forwarding propagation lag instead of immediately failing operator pairing. Add a regression test for an unreachable Windows gateway port. Fixes #662 Co-authored-by: Copilot <[email protected]>
1 parent 234fa8f commit c320136

2 files changed

Lines changed: 48 additions & 11 deletions

File tree

‎src/OpenClaw.SetupEngine/SetupSteps.cs‎

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1660,6 +1660,28 @@ internal static bool TryReadBootstrapToken(string json, out string? token, out s
16601660
}
16611661
}
16621662

1663+
internal static class WindowsGatewayReachability
1664+
{
1665+
public static async Task<StepResult> VerifyAsync(SetupContext ctx, string pairingRole, CancellationToken ct)
1666+
{
1667+
try
1668+
{
1669+
using var http = new HttpClient { Timeout = TimeSpan.FromSeconds(5) };
1670+
var resp = await http.GetAsync($"http://localhost:{ctx.Config.GatewayPort}/", ct);
1671+
ctx.Logger.Debug($"Gateway health check: HTTP {(int)resp.StatusCode}");
1672+
return StepResult.Ok();
1673+
}
1674+
catch (OperationCanceledException) when (ct.IsCancellationRequested)
1675+
{
1676+
throw;
1677+
}
1678+
catch (Exception ex)
1679+
{
1680+
return StepResult.Fail($"Gateway not reachable before {pairingRole} pairing: {ex.Message}");
1681+
}
1682+
}
1683+
}
1684+
16631685
public sealed class PairOperatorStep : SetupStep
16641686
{
16651687
public override string Id => "pair-operator";
@@ -1716,6 +1738,10 @@ record = registry.AddOrUpdate(record);
17161738
ctx.Logger.Info($"Device identity initialized: {identity.DeviceId[..16]}...");
17171739
ctx.OperatorDeviceId = identity.DeviceId;
17181740

1741+
var reachability = await WindowsGatewayReachability.VerifyAsync(ctx, "operator", ct);
1742+
if (!reachability.IsSuccess)
1743+
return reachability;
1744+
17191745
// Connect operator WebSocket — handle pairing-required flow
17201746
var wsLogger = new SetupOpenClawLogger(ctx.Logger);
17211747
OpenClawGatewayClient? client = null;
@@ -2104,17 +2130,9 @@ public override async Task<StepResult> ExecuteAsync(SetupContext ctx, Cancellati
21042130

21052131
var identityPath = registry.GetIdentityDirectory(record.Id);
21062132

2107-
// Verify gateway is reachable before connecting
2108-
try
2109-
{
2110-
using var http = new HttpClient { Timeout = TimeSpan.FromSeconds(5) };
2111-
var resp = await http.GetAsync($"http://localhost:{ctx.Config.GatewayPort}/", ct);
2112-
ctx.Logger.Debug($"Gateway health check: HTTP {(int)resp.StatusCode}");
2113-
}
2114-
catch (Exception ex)
2115-
{
2116-
return StepResult.Fail($"Gateway not reachable before node pairing: {ex.Message}");
2117-
}
2133+
var reachability = await WindowsGatewayReachability.VerifyAsync(ctx, "node", ct);
2134+
if (!reachability.IsSuccess)
2135+
return reachability;
21182136

21192137
var drainResult = await VerifyEndToEndStep.DrainPendingDeviceApprovalsAsync(ctx, ct);
21202138
if (!drainResult.IsSuccess)

‎tests/OpenClaw.SetupEngine.Tests/SetupStepsTests.cs‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1273,6 +1273,25 @@ public async Task ValidateWslLockdown_RetriesWslConfReadAfterStartupTimeout()
12731273
Assert.Equal(2, catAttempts);
12741274
}
12751275

1276+
// ─── PairOperatorStep: Windows-side gateway health check ───
1277+
1278+
[Fact]
1279+
public async Task PairOperatorStep_FailsWhenGatewayNotReachableFromWindows()
1280+
{
1281+
// Allocate a port and immediately release it so nothing is listening on it.
1282+
var port = GetFreeTcpPort();
1283+
1284+
var config = new SetupConfig { GatewayPort = port };
1285+
var ctx = CreateContext(config);
1286+
ctx.SharedGatewayToken = "test-shared-token";
1287+
1288+
var step = new PairOperatorStep();
1289+
var result = await step.ExecuteAsync(ctx, CancellationToken.None);
1290+
1291+
Assert.Equal(StepOutcome.Failed, result.Outcome);
1292+
Assert.Contains("not reachable", result.Message, StringComparison.OrdinalIgnoreCase);
1293+
}
1294+
12761295
private static CommandResult Ok(string stdout = "", string stderr = "")
12771296
=> new(0, stdout, stderr, TimeSpan.Zero, TimedOut: false);
12781297

0 commit comments

Comments
 (0)