Skip to content

Commit c4d68f6

Browse files
RomneyDaclaude
andcommitted
fix(setup): stop WSL inspection flake from dead-ending onboarding
Selecting the recommended "Install a local gateway (WSL)" option could end the setup flow with "Could not inspect WSL" and no way forward, telling the user to run `wsl --list --quiet` by hand. It reproduces on a machine where that command succeeds every time when run manually. Three separate defects combine to produce it. CommandRunner gave the redirected pipes a flat 250ms grace to reach EOF after the process exited. The OutputDataReceived callbacks are queued to the thread pool, so on a saturated pool (a WinUI cold start is exactly that) they can miss that window even though the process already exited and wrote its output. The command is then reported with an exit code but empty stdout and stderr, and every caller that classifies command output reads that as silence. The EOF wait is now bounded by a short cap instead, clamped by whatever remains of the caller's own deadline so the accepted timeout is never exceeded. The cap keeps the existing protection against a surviving descendant holding the pipes open, which RunAsync_ReturnsWhenDescendantKeepsOutputPipesOpen covers. InterpretDistroList gated both of its success paths behind !result.TimedOut, so a run that timed out after wsl.exe had already reported that WSL is not installed threw instead of using that answer. The output proves no distro can exist, and WslViabilityInspector already treats that state as Installable rather than a blocker, so the conclusive answer is now kept. WelcomePage replaced the option's title with "Checking existing setup..." for the duration of the check, hiding which option was being acted on, and offered only Close when inspection failed. The progress ring already carries the busy state (its automation name is "Checking existing WSL setup"), so the title now stays put, and a failed inspection offers Try again instead of ending the flow. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_01YbwoyZNVT3qnRexUnCLZUx
1 parent 1e18866 commit c4d68f6

5 files changed

Lines changed: 85 additions & 25 deletions

File tree

‎src/OpenClaw.SetupEngine.UI/Pages/WelcomePage.xaml.cs‎

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ namespace OpenClaw.SetupEngine.UI.Pages;
1313

1414
public sealed partial class WelcomePage : Page
1515
{
16-
private const string InstallButtonText = "Install a local gateway (WSL)";
17-
private const string CheckingButtonText = "Checking existing setup...";
1816
private SetupConfig? _config;
1917
private bool _installSelected = true; // default selection
2018
private bool _suppressSelectionWrite;
@@ -145,42 +143,53 @@ private async Task StartInstallAsync()
145143
var setupWindow = SetupWindow.Active;
146144
var dataDir = setupWindow?.DataDir ?? SetupContext.ResolveDataDir();
147145

146+
// The progress ring carries the checking state (its automation name is
147+
// "Checking existing WSL setup"). Leave the option title alone: replacing it
148+
// hides which option is being acted on for as long as the check runs.
148149
NextButton.IsEnabled = false;
149-
InstallTitle.Text = CheckingButtonText;
150150
InstallCheckProgress.IsActive = true;
151151
InstallCheckProgress.Visibility = Visibility.Visible;
152152
var navigating = false;
153153
try
154154
{
155155
ExistingConfigDetector.ExistingConfig existing;
156-
try
157-
{
158-
existing = await Task.Run(() => ExistingConfigDetector.Detect(
159-
dataDir,
160-
config.DistroName,
161-
setupWindow?.LocalDataDir));
162-
}
163-
catch (InvalidOperationException ex)
156+
while (true)
164157
{
165-
var errorRoot = XamlRoot;
166-
if (setupWindow is not null and { IsClosed: false } && errorRoot is not null)
158+
try
159+
{
160+
existing = await Task.Run(() => ExistingConfigDetector.Detect(
161+
dataDir,
162+
config.DistroName,
163+
setupWindow?.LocalDataDir));
164+
break;
165+
}
166+
catch (InvalidOperationException ex)
167167
{
168-
await new ContentDialog
168+
var errorRoot = XamlRoot;
169+
if (setupWindow is null or { IsClosed: true } || errorRoot is null)
170+
return;
171+
172+
// Inspection failure is usually transient, so offer a way forward
173+
// instead of ending the flow on the recommended option.
174+
var retry = await new ContentDialog
169175
{
170176
Title = "Could not inspect WSL",
171177
Content = ex.Message,
172-
CloseButtonText = "Close",
178+
PrimaryButtonText = "Try again",
179+
CloseButtonText = "Cancel",
180+
DefaultButton = ContentDialogButton.Primary,
173181
XamlRoot = errorRoot,
174182
}.ShowAsync();
183+
184+
if (retry != ContentDialogResult.Primary)
185+
return;
175186
}
176-
return;
177187
}
178188

179189
var xamlRoot = XamlRoot;
180190
if (setupWindow is null or { IsClosed: true } || xamlRoot is null)
181191
return;
182192

183-
InstallTitle.Text = InstallButtonText;
184193
InstallCheckProgress.IsActive = false;
185194
InstallCheckProgress.Visibility = Visibility.Collapsed;
186195
var summary = ExistingConfigDetector.BuildReplacementSummary(existing);
@@ -218,7 +227,6 @@ private async Task StartInstallAsync()
218227
{
219228
if (!navigating && setupWindow is { IsClosed: false })
220229
{
221-
InstallTitle.Text = InstallButtonText;
222230
InstallCheckProgress.IsActive = false;
223231
InstallCheckProgress.Visibility = Visibility.Collapsed;
224232
NextButton.IsEnabled = true;

‎src/OpenClaw.SetupEngine/CommandRunner.cs‎

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,14 @@ public sealed class CommandRunner : ICommandRunner
7171
{
7272
private readonly SetupLogger _logger;
7373
private static readonly TimeSpan s_outputDrainGrace = TimeSpan.FromMilliseconds(250);
74+
75+
/// <summary>
76+
/// Upper bound on how long an exited process is given to reach EOF on its redirected
77+
/// pipes. Large enough that a saturated thread pool cannot make a process that did
78+
/// write output look silent, small enough that a descendant holding the pipes open
79+
/// cannot stall the caller.
80+
/// </summary>
81+
private static readonly TimeSpan s_outputDrainCap = TimeSpan.FromSeconds(3);
7482
private const int MaxCapturedStreamChars = 1_048_576;
7583

7684
public CommandRunner(SetupLogger logger) => _logger = logger;
@@ -193,11 +201,29 @@ public async Task<CommandResult> RunAsync(
193201
}
194202

195203
// A surviving descendant can keep inherited pipe handles open after the child
196-
// exits. Preserve output already in flight without charging the command's full
197-
// timeout to an EOF that may never arrive.
204+
// exits, so the EOF wait must stay bounded and cannot simply run to the command
205+
// timeout. It also cannot be as tight as a few hundred milliseconds: the
206+
// OutputDataReceived callbacks are queued to the thread pool, and on a saturated
207+
// pool they can miss that window even though the process already exited and
208+
// wrote its output. That surfaced as an exited process being reported with empty
209+
// stdout and stderr, which every caller that classifies command output then
210+
// misreads as silence. Wait up to a short cap instead, clamped by whatever
211+
// remains of the caller's own deadline so the accepted timeout is never exceeded.
212+
TimeSpan drainBudget = s_outputDrainGrace;
213+
if (!timedOut)
214+
{
215+
TimeSpan remaining = timeout - sw.Elapsed;
216+
if (remaining < TimeSpan.Zero)
217+
remaining = TimeSpan.Zero;
218+
219+
TimeSpan bounded = remaining < s_outputDrainCap ? remaining : s_outputDrainCap;
220+
if (bounded > drainBudget)
221+
drainBudget = bounded;
222+
}
223+
198224
await Task.WhenAny(
199225
Task.WhenAll(stdoutClosed.Task, stderrClosed.Task),
200-
Task.Delay(s_outputDrainGrace));
226+
Task.Delay(drainBudget));
201227

202228
sw.Stop();
203229
var result = new CommandResult(

‎src/OpenClaw.SetupEngine/ExistingConfigDetector.cs‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,11 @@ internal static bool InterpretDistroList(CommandResult result, string targetDist
8686
if (!result.TimedOut && result.ExitCode == 0)
8787
return WslInstallSupport.ContainsDistro(result.Stdout, targetDistroName);
8888

89-
if (!result.TimedOut && WslViabilityInspector.LooksUnavailable(result))
89+
// A conclusive "WSL is not installed" answer stays usable even when the run
90+
// also timed out. The output already proves no distro can exist, so failing
91+
// closed here would reject evidence the inspector itself treats as the
92+
// installable path rather than a blocker.
93+
if (WslViabilityInspector.LooksUnavailable(result))
9094
return false;
9195

9296
throw new InvalidOperationException(

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3672,6 +3672,22 @@ public void ExistingConfigDetector_TreatsUnavailableWslAsNoDistro()
36723672
Assert.False(ExistingConfigDetector.InterpretDistroList(result, "OpenClawGateway"));
36733673
}
36743674

3675+
[Fact]
3676+
public void ExistingConfigDetector_KeepsConclusiveUnavailableAnswerWhenRunAlsoTimedOut()
3677+
{
3678+
// A run can time out after wsl.exe already reported that WSL is not installed.
3679+
// That output still proves no distro can exist, so the answer stays usable
3680+
// instead of failing closed and dead-ending the setup flow.
3681+
var result = new CommandResult(
3682+
-1,
3683+
"",
3684+
"WSL is not installed. See https://aka.ms/wslinstall",
3685+
TimeSpan.FromSeconds(5),
3686+
TimedOut: true);
3687+
3688+
Assert.False(ExistingConfigDetector.InterpretDistroList(result, "OpenClawGateway"));
3689+
}
3690+
36753691
[Theory]
36763692
[InlineData(true, 1, "")]
36773693
[InlineData(false, 1, "unexpected failure")]

‎tests/OpenClaw.Tray.Tests/AppRefactorContractTests.cs‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1584,18 +1584,24 @@ public void SetupWelcomePage_RunsExistingConfigDetectionOffUiThread()
15841584
var method = ExtractMethod(source, "StartInstallAsync");
15851585

15861586
Assert.Contains("NextButton.IsEnabled = false", method);
1587-
Assert.Contains("InstallTitle.Text = CheckingButtonText", method);
15881587
Assert.Contains("InstallCheckProgress.IsActive = true", method);
15891588
Assert.Contains("InstallCheckProgress.Visibility = Visibility.Visible", method);
1590-
Assert.Contains("CheckingButtonText", method);
15911589
Assert.Contains("var setupWindow = SetupWindow.Active", method);
15921590
Assert.Contains("await Task.Run(() => ExistingConfigDetector.Detect", method);
15931591
Assert.Contains("setupWindow is null or { IsClosed: true } || xamlRoot is null", method);
15941592
Assert.Contains("setupWindow is { IsClosed: false }", method);
1595-
Assert.Contains("InstallTitle.Text = InstallButtonText", method);
15961593
Assert.Contains("InstallCheckProgress.IsActive = false", method);
15971594
Assert.Contains("InstallCheckProgress.Visibility = Visibility.Collapsed", method);
15981595
Assert.Contains("NextButton.IsEnabled = true", method);
1596+
1597+
// The busy state is carried by the progress ring alone. Overwriting the option
1598+
// title hid which option was being acted on for as long as the check ran.
1599+
Assert.DoesNotContain("InstallTitle.Text", method);
1600+
1601+
// A failed inspection must stay recoverable instead of ending the flow on the
1602+
// recommended option with no way forward.
1603+
Assert.Contains("PrimaryButtonText = \"Try again\"", method);
1604+
15991605
Assert.Contains("AutomationProperties.AutomationId=\"WelcomeInstallCheckProgress\"", File.ReadAllText(
16001606
Path.Combine(root, "src", "OpenClaw.SetupEngine.UI", "Pages", "WelcomePage.xaml")));
16011607
AssertInOrder(

0 commit comments

Comments
 (0)