Skip to content

Commit 2eab151

Browse files
karkarlCopilot
andauthored
fix(setup): retry fresh WSL root readiness checks (#1107)
Co-authored-by: Copilot App <[email protected]>
1 parent ad47224 commit 2eab151

2 files changed

Lines changed: 120 additions & 7 deletions

File tree

‎src/OpenClaw.SetupEngine/SetupSteps.cs‎

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -741,6 +741,12 @@ private static string DescribeBind(IPAddress address)
741741
public sealed class CreateWslInstanceStep : SetupStep
742742
{
743743
private static readonly TimeSpan DistroVersionVerificationTimeout = TimeSpan.FromMinutes(1);
744+
private static readonly TimeSpan[] FreshDistroProbeTimeouts =
745+
[
746+
TimeSpan.FromSeconds(30),
747+
TimeSpan.FromSeconds(60),
748+
TimeSpan.FromSeconds(90),
749+
];
744750

745751
public override string Id => "wsl-create";
746752
public override string DisplayName => "Create WSL instance";
@@ -845,14 +851,35 @@ private static async Task<StepResult> VerifyFreshDistro(SetupContext ctx, string
845851
if (version != 2)
846852
return StepResult.Fail($"Fresh WSL install registered '{distro}' as WSL{version}; WSL2 is required.");
847853

848-
var probe = await ctx.Commands.RunAsync(
849-
WslConstants.WslExePath,
850-
["-d", distro, "-u", "root", "--", "sh", "-lc", "id -u && test -d / && echo OPENCLAW_FRESH_WSL_READY"],
851-
TimeSpan.FromSeconds(30),
852-
ct: ct);
854+
CommandResult? probe = null;
855+
for (var attempt = 0; attempt < FreshDistroProbeTimeouts.Length; attempt++)
856+
{
857+
probe = await ctx.Commands.RunAsync(
858+
WslConstants.WslExePath,
859+
["-d", distro, "-u", "root", "--", "sh", "-lc", "id -u && test -d / && echo OPENCLAW_FRESH_WSL_READY"],
860+
FreshDistroProbeTimeouts[attempt],
861+
ct: ct);
862+
if (probe.ExitCode == 0
863+
&& probe.Stdout.Contains("OPENCLAW_FRESH_WSL_READY", StringComparison.Ordinal))
864+
{
865+
break;
866+
}
853867

854-
if (probe.ExitCode != 0 || !probe.Stdout.Contains("OPENCLAW_FRESH_WSL_READY", StringComparison.Ordinal))
855-
return StepResult.Fail($"Fresh WSL distro '{distro}' could not run a root verification command: {FirstNonEmpty(probe.Stderr, probe.Stdout)}");
868+
if (attempt < FreshDistroProbeTimeouts.Length - 1)
869+
{
870+
ctx.Logger.Warn(
871+
$"Fresh WSL distro '{distro}' root probe was not ready " +
872+
$"(attempt {attempt + 1}/{FreshDistroProbeTimeouts.Length}); retrying.");
873+
}
874+
}
875+
876+
if (probe is null
877+
|| probe.ExitCode != 0
878+
|| !probe.Stdout.Contains("OPENCLAW_FRESH_WSL_READY", StringComparison.Ordinal))
879+
{
880+
var detail = probe is null ? "no output" : FirstNonEmpty(probe.Stderr, probe.Stdout);
881+
return StepResult.Fail($"Fresh WSL distro '{distro}' could not run a root verification command: {detail}");
882+
}
856883

857884
return StepResult.Ok($"Created clean WSL2 distro '{distro}' at '{installPath}'");
858885
}

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

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,6 +1030,92 @@ public async Task CreateWslInstance_UsesDirectFreshInstallAndDoesNotExportBaseDi
10301030
Assert.Contains("--web-download", installCall.Arguments);
10311031
}
10321032

1033+
[Fact]
1034+
public async Task CreateWslInstance_RetriesTransientFreshDistroRootProbeTimeout()
1035+
{
1036+
var installed = false;
1037+
var probeAttempts = 0;
1038+
var commands = new FakeCommandRunner(args =>
1039+
{
1040+
if (args.SequenceEqual(["--list", "--quiet"]))
1041+
return Ok(installed ? "OpenClawGateway\n" : "");
1042+
if (args.Contains("--install"))
1043+
{
1044+
installed = true;
1045+
return Ok("Installing Ubuntu-24.04\n");
1046+
}
1047+
if (args.SequenceEqual(["--list", "--verbose"]))
1048+
return Ok(" NAME STATE VERSION\n* OpenClawGateway Stopped 2\n");
1049+
if (args.SequenceEqual(["-d", "OpenClawGateway", "-u", "root", "--", "sh", "-lc", "id -u && test -d / && echo OPENCLAW_FRESH_WSL_READY"]))
1050+
{
1051+
probeAttempts++;
1052+
return probeAttempts == 1
1053+
? new CommandResult(-1, "", "", TimeSpan.FromSeconds(30), TimedOut: true)
1054+
: Ok("0\nOPENCLAW_FRESH_WSL_READY\n");
1055+
}
1056+
1057+
return Fail($"unexpected args: {string.Join(' ', args)}");
1058+
});
1059+
var ctx = CreateContext(commands: commands);
1060+
1061+
var result = await new CreateWslInstanceStep().ExecuteAsync(ctx, CancellationToken.None);
1062+
1063+
Assert.True(result.IsSuccess, result.Message);
1064+
Assert.Equal(2, probeAttempts);
1065+
Assert.DoesNotContain(commands.Calls, call => call.Arguments.Contains("--unregister"));
1066+
}
1067+
1068+
[Fact]
1069+
public async Task CreateWslInstance_BoundsPersistentFreshDistroRootProbeTimeouts()
1070+
{
1071+
var installed = false;
1072+
var probeAttempts = 0;
1073+
var commands = new FakeCommandRunner(args =>
1074+
{
1075+
if (args.SequenceEqual(["--list", "--quiet"]))
1076+
return Ok(installed ? "OpenClawGateway\n" : "");
1077+
if (args.Contains("--install"))
1078+
{
1079+
installed = true;
1080+
return Ok("Installing Ubuntu-24.04\n");
1081+
}
1082+
if (args.SequenceEqual(["--list", "--verbose"]))
1083+
return Ok(" NAME STATE VERSION\n* OpenClawGateway Stopped 2\n");
1084+
if (args.SequenceEqual(["-d", "OpenClawGateway", "-u", "root", "--", "sh", "-lc", "id -u && test -d / && echo OPENCLAW_FRESH_WSL_READY"]))
1085+
{
1086+
probeAttempts++;
1087+
return new CommandResult(-1, "", "", TimeSpan.FromSeconds(30), TimedOut: true);
1088+
}
1089+
if (args.SequenceEqual(["--terminate", "OpenClawGateway"]))
1090+
return Ok();
1091+
if (args.SequenceEqual(["--unregister", "OpenClawGateway"]))
1092+
{
1093+
installed = false;
1094+
return Ok();
1095+
}
1096+
1097+
return Fail($"unexpected args: {string.Join(' ', args)}");
1098+
});
1099+
var ctx = CreateContext(commands: commands);
1100+
1101+
var result = await new CreateWslInstanceStep().ExecuteAsync(ctx, CancellationToken.None);
1102+
1103+
Assert.Equal(StepOutcome.Failed, result.Outcome);
1104+
Assert.Equal(3, probeAttempts);
1105+
Assert.Contains("could not run a root verification command", result.Message);
1106+
Assert.Equal(
1107+
[
1108+
TimeSpan.FromSeconds(30),
1109+
TimeSpan.FromSeconds(60),
1110+
TimeSpan.FromSeconds(90),
1111+
],
1112+
commands.TimedCalls
1113+
.Where(call => call.Arguments.SequenceEqual(
1114+
["-d", "OpenClawGateway", "-u", "root", "--", "sh", "-lc", "id -u && test -d / && echo OPENCLAW_FRESH_WSL_READY"]))
1115+
.Select(call => call.Timeout)
1116+
.ToArray());
1117+
}
1118+
10331119
[Fact]
10341120
public async Task CreateWslInstance_AllowsWslServiceToSettleBeforeVersionVerification()
10351121
{

0 commit comments

Comments
 (0)