1- using System . Diagnostics ;
21using System . Runtime . ExceptionServices ;
32using System . Text . Json ;
43using 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 }
0 commit comments