Skip to content

Commit cc9febd

Browse files
ranjeshjCopilot
andcommitted
Harden setup wizard restart reconnect
Wait for Windows-side gateway reachability before replaying wizard steps after restart-like disconnects, and use a reconnect timeout bounded by pairing and gateway health settings. Co-authored-by: Copilot <[email protected]>
1 parent 5e1ae50 commit cc9febd

1 file changed

Lines changed: 35 additions & 1 deletion

File tree

‎src/OpenClaw.SetupEngine/SetupWizardRunner.cs‎

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,11 @@ public async Task<StepResult> RunAsync(CancellationToken ct)
169169
client.Dispose();
170170

171171
await Task.Delay(TimeSpan.FromSeconds(3), ct);
172+
await WaitForGatewayReachableAfterRestart(ct);
172173
client = CreateWizardClient(credential, identityPath, wsLogger);
173-
var reconnect = await PairOperatorStep.WaitForConnectionOrPairing(client, _ctx, TimeSpan.FromSeconds(_ctx.Config.Pairing.TimeoutSeconds), ct);
174+
var reconnectTimeout = TimeSpan.FromSeconds(
175+
Math.Max(_ctx.Config.Pairing.TimeoutSeconds, _ctx.Config.Gateway.HealthTimeoutSeconds));
176+
var reconnect = await PairOperatorStep.WaitForConnectionOrPairing(client, _ctx, reconnectTimeout, ct);
174177
if (reconnect != PairOperatorStep.ConnectionOutcome.Connected)
175178
return StepResult.Fail($"Gateway wizard reconnect failed after restart: {reconnect}");
176179

@@ -251,6 +254,37 @@ private async Task TryResetReloadModeAsync()
251254
}
252255
}
253256

257+
private async Task WaitForGatewayReachableAfterRestart(CancellationToken ct)
258+
{
259+
var gatewayPort = _ctx.Config.GatewayPort;
260+
var timeoutSeconds = Math.Max(15, _ctx.Config.Gateway.HealthTimeoutSeconds);
261+
var deadline = DateTimeOffset.UtcNow.AddSeconds(timeoutSeconds);
262+
using var http = new HttpClient { Timeout = TimeSpan.FromSeconds(5) };
263+
264+
while (DateTimeOffset.UtcNow < deadline)
265+
{
266+
ct.ThrowIfCancellationRequested();
267+
try
268+
{
269+
var resp = await http.GetAsync($"http://127.0.0.1:{gatewayPort}/", ct);
270+
var code = (int)resp.StatusCode;
271+
if (code is 200 or 401 or 403)
272+
{
273+
_ctx.Logger.Info($"Gateway reachable after restart (HTTP {code})");
274+
return;
275+
}
276+
}
277+
catch (Exception ex)
278+
{
279+
_ctx.Logger.Debug($"Gateway restart wait: {ex.GetType().Name} — {ex.Message}");
280+
}
281+
282+
await Task.Delay(1000, ct);
283+
}
284+
285+
_ctx.Logger.Warn($"Gateway did not become HTTP-reachable within {timeoutSeconds}s after restart; attempting reconnect anyway");
286+
}
287+
254288
private string WriteAnswerTemplate(IReadOnlyList<WizardTemplateStep> discoveredSteps, WizardPayload? missingStep)
255289
{
256290
var logPath = _ctx.Config.LogPath;

0 commit comments

Comments
 (0)