Skip to content

Commit 89a04d5

Browse files
shanselmanCopilot
andcommitted
fix(setup): use full restoration retry budget
Co-authored-by: Copilot App <[email protected]>
1 parent 6d058b1 commit 89a04d5

2 files changed

Lines changed: 47 additions & 19 deletions

File tree

‎src/OpenClaw.SetupEngine/SetupWizardRunner.cs‎

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using System.Diagnostics;
21
using System.Runtime.ExceptionServices;
32
using System.Text.Json;
43
using System.Text.RegularExpressions;
@@ -16,7 +15,8 @@ public sealed class SetupWizardRunner
1615
TimeSpan.FromMilliseconds(250);
1716
private static readonly TimeSpan StartupMigrationLeaseRestoreMaxDelay =
1817
TimeSpan.FromSeconds(2);
19-
internal const int StartupMigrationLeaseRestoreMaxAttempts = 8;
18+
private static readonly TimeSpan MinimumReloadRestorationCommandTimeout =
19+
TimeSpan.FromMilliseconds(500);
2020
internal const string StartupMigrationLeaseDiagnostic =
2121
"OpenClaw startup migrations are already running for this state directory;";
2222
private static readonly Regex s_normalizeKeyRegex = new("[^a-z0-9]+", RegexOptions.Compiled);
@@ -26,19 +26,25 @@ public sealed class SetupWizardRunner
2626

2727
private readonly SetupContext _ctx;
2828
private readonly Func<TimeSpan, CancellationToken, Task> _restorationDelayAsync;
29+
private readonly TimeProvider _timeProvider;
2930
private bool _reloadSuspended;
3031

3132
public SetupWizardRunner(SetupContext ctx)
32-
: this(ctx, static (delay, cancellationToken) => Task.Delay(delay, cancellationToken))
33+
: this(
34+
ctx,
35+
static (delay, cancellationToken) => Task.Delay(delay, cancellationToken),
36+
TimeProvider.System)
3337
{
3438
}
3539

3640
internal SetupWizardRunner(
3741
SetupContext ctx,
38-
Func<TimeSpan, CancellationToken, Task> restorationDelayAsync)
42+
Func<TimeSpan, CancellationToken, Task> restorationDelayAsync,
43+
TimeProvider? timeProvider = null)
3944
{
4045
_ctx = ctx;
4146
_restorationDelayAsync = restorationDelayAsync;
47+
_timeProvider = timeProvider ?? TimeProvider.System;
4248
}
4349

4450
public async Task<StepResult> RunAsync(CancellationToken ct)
@@ -611,15 +617,16 @@ private async Task<CommandResult> RunReloadModeRestorationCommandAsync(string re
611617
{
612618
var command =
613619
$"{_ctx.WslPathPrefix} && openclaw config set gateway.reload.mode {WslShellQuoting.QuotePosixSingleQuote(reloadMode)}";
614-
var stopwatch = Stopwatch.StartNew();
620+
var startedAt = _timeProvider.GetTimestamp();
615621
CommandResult? lastResult = null;
616622

617623
for (var attempt = 1; ; attempt++)
618624
{
619-
var remaining = ReloadRestorationTimeout - stopwatch.Elapsed;
625+
var remaining =
626+
ReloadRestorationTimeout - _timeProvider.GetElapsedTime(startedAt);
620627
if (remaining <= TimeSpan.Zero)
621628
return lastResult!;
622-
if (lastResult is not null && remaining <= StartupMigrationLeaseRestoreInitialDelay)
629+
if (lastResult is not null && remaining < MinimumReloadRestorationCommandTimeout)
623630
return lastResult;
624631

625632
var result = await _ctx.Commands.RunInWslAsync(
@@ -629,23 +636,25 @@ private async Task<CommandResult> RunReloadModeRestorationCommandAsync(string re
629636
ct: CancellationToken.None);
630637
lastResult = result;
631638
if (result.ExitCode == 0
632-
|| !IsStartupMigrationLeaseContention(result)
633-
|| attempt >= StartupMigrationLeaseRestoreMaxAttempts)
639+
|| !IsStartupMigrationLeaseContention(result))
634640
{
635641
return result;
636642
}
637643

638-
remaining = ReloadRestorationTimeout - stopwatch.Elapsed;
639-
var delay = TimeSpan.FromMilliseconds(
644+
remaining =
645+
ReloadRestorationTimeout - _timeProvider.GetElapsedTime(startedAt);
646+
var scheduledDelay = TimeSpan.FromMilliseconds(
640647
Math.Min(
641648
StartupMigrationLeaseRestoreInitialDelay.TotalMilliseconds
642649
* Math.Pow(2, attempt - 1),
643650
StartupMigrationLeaseRestoreMaxDelay.TotalMilliseconds));
644-
if (remaining <= delay)
651+
var maximumDelay = remaining - MinimumReloadRestorationCommandTimeout;
652+
if (maximumDelay <= TimeSpan.Zero)
645653
return result;
654+
var delay = scheduledDelay < maximumDelay ? scheduledDelay : maximumDelay;
646655

647656
_ctx.Logger.Warn(
648-
$"Gateway startup migrations still own the state directory while restoring reload mode; retrying in {delay.TotalMilliseconds:0} ms (attempt {attempt + 1}/{StartupMigrationLeaseRestoreMaxAttempts})");
657+
$"Gateway startup migrations still own the state directory while restoring reload mode; retrying in {delay.TotalMilliseconds:0} ms (attempt {attempt + 1})");
649658
await _restorationDelayAsync(delay, CancellationToken.None);
650659
}
651660
}

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

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2187,6 +2187,7 @@ public async Task SetupWizard_OrchestratorDoesNotRetryUnrelatedRestorationFailur
21872187
public async Task SetupWizard_LeaseContentionExhaustionPreservesWizardAndRestorationFailures()
21882188
{
21892189
var restoreAttempts = 0;
2190+
var timeProvider = new ManualTimeProvider();
21902191
var commands = new FakeCommandRunner(
21912192
_ => Ok(),
21922193
(_, command, _) =>
@@ -2198,11 +2199,13 @@ public async Task SetupWizard_LeaseContentionExhaustionPreservesWizardAndRestora
21982199
ctx.DistroName = "test-distro";
21992200
var runner = new SetupWizardRunner(
22002201
ctx,
2201-
(_, _) =>
2202+
(delay, _) =>
22022203
{
22032204
restoreAttempts++;
2205+
timeProvider.Advance(delay);
22042206
return Task.CompletedTask;
2205-
});
2207+
},
2208+
timeProvider);
22062209

22072210
var result = await runner.RunWithReloadRestorationAsync(() =>
22082211
{
@@ -2214,13 +2217,16 @@ public async Task SetupWizard_LeaseContentionExhaustionPreservesWizardAndRestora
22142217
Assert.Contains("Failed to restore gateway.reload.mode", result.Message);
22152218
Assert.Contains(SetupWizardRunner.StartupMigrationLeaseDiagnostic, result.Message);
22162219
Assert.Contains("wizard failed", result.Message);
2220+
Assert.Equal(10, restoreAttempts);
22172221
Assert.Equal(
2218-
SetupWizardRunner.StartupMigrationLeaseRestoreMaxAttempts - 1,
2219-
restoreAttempts);
2220-
Assert.Equal(
2221-
SetupWizardRunner.StartupMigrationLeaseRestoreMaxAttempts,
2222+
11,
22222223
commands.WslCalls.Count(
22232224
call => call.Command.Contains("config set gateway.reload.mode 'hybrid'")));
2225+
Assert.Equal(TimeSpan.FromSeconds(14.5), timeProvider.Elapsed);
2226+
Assert.All(
2227+
commands.WslCalls.Where(
2228+
call => call.Command.Contains("config set gateway.reload.mode 'hybrid'")),
2229+
call => Assert.True(call.Timeout >= TimeSpan.FromMilliseconds(500)));
22242230
Assert.DoesNotContain(
22252231
commands.WslCalls,
22262232
call => call.Command.Contains("openclaw gateway restart"));
@@ -4449,6 +4455,19 @@ public Task<CommandResult> RunInWslAsync(
44494455
}
44504456
}
44514457

4458+
private sealed class ManualTimeProvider : TimeProvider
4459+
{
4460+
private long _timestamp;
4461+
4462+
public TimeSpan Elapsed => TimeSpan.FromTicks(_timestamp);
4463+
4464+
public override long TimestampFrequency => TimeSpan.TicksPerSecond;
4465+
4466+
public override long GetTimestamp() => _timestamp;
4467+
4468+
public void Advance(TimeSpan duration) => _timestamp += duration.Ticks;
4469+
}
4470+
44524471
private sealed class RecordingAuthorizationPresenter : IExternalAuthorizationPresenter
44534472
{
44544473
public ExternalAuthorizationRequest? Request { get; private set; }

0 commit comments

Comments
 (0)