babalae/better-genshin-impact · error · InvalidOperationException

状态 {CurrentState} 重试次数达到上限 {retryPolicy.value}

Error message

状态 {CurrentState} 重试次数达到上限 {retryPolicy.value}

What it means

Thrown by StateMachineBase.RecordStateRetry for a count-based retry policy when CurrentStateRetryCount reaches retryPolicy.value. This is the default path (no per-state timeout registered): GetRetryPolicyForState returns (false, maxRetries). Each Retry result increments the counter, and once it hits the configured max, the state machine aborts with InvalidOperationException.

Source

Thrown at BetterGenshinImpact/GameTask/Common/StateMachine/StateMachineBase.cs:646

        {
            var elapsedMilliseconds = stateRetryStopwatch.ElapsedMilliseconds;
            if (elapsedMilliseconds >= retryPolicy.value)
            {
                Logger.LogError("状态 {State} {Reason},重试超时达到上限 {Max} ms,转为失败",
                    CurrentState, reason, retryPolicy.value);
                throw new InvalidOperationException($"状态 {CurrentState} 重试超时达到上限 {retryPolicy.value} ms");
            }

            Logger.LogWarning("状态 {State} {Reason},将在重试超时窗口内继续重试(第 {Count} 次,已用 {Elapsed}/{Max} ms)",
                CurrentState, reason, CurrentStateRetryCount, elapsedMilliseconds, retryPolicy.value);
            return;
        }

        if (CurrentStateRetryCount >= retryPolicy.value)
        {
            Logger.LogError("状态 {State} {Reason},重试次数达到上限 {Max},转为失败",
                CurrentState, reason, retryPolicy.value);
            throw new InvalidOperationException($"状态 {CurrentState} 重试次数达到上限 {retryPolicy.value}");
        }

        Logger.LogWarning("状态 {State} {Reason},将重试当前状态(第 {Count}/{Max} 次)",
            CurrentState, reason, CurrentStateRetryCount, retryPolicy.value);
    }

    #endregion

    #region 状态机运行

    /// <summary>
    /// 初始化状态机
    /// </summary>
    protected void Initialize(CancellationToken ct, TState initialState = default)
    {
        _ct = ct;
        CurrentState = initialState;
    }

View on GitHub (pinned to a7cb36712d)

Solutions

  1. Raise the retry limit via RegisterStateRetryLimit(state, higherCount) or pass a larger maxRetries to RunStateMachineUntil.
  2. Switch the state to a timeout-based policy (RegisterStateRetryTimeout) if failures are bursty but eventually recover.
  3. Fix the handler/detector so it stops returning Retry (re-capture templates, correct logic).
  4. Review the '将重试当前状态' warning logs to confirm the retry reason.
Defensive patterns

Strategy: retry

Try / catch

try { await RunStateMachineUntil(context, targets); }
catch (InvalidOperationException ex) when (ex.Message.Contains("重试次数达到上限"))
{ Logger.LogError(ex, "状态机重试次数耗尽"); throw; }

Prevention

When it happens

Trigger: A state handler returns Retry more times than the allowed max (default 5, overridable via RegisterStateRetryLimit). The same upstream conditions as 414 apply, but the budget is counted in attempts rather than milliseconds.

Common situations: Default maxRetries=5 exceeded because the handler keeps failing transiently; the per-state limit was lowered; detector/handler mismatch with the live UI; intermittent capture failures causing misdetected state.

Related errors


AI-assisted analysis of babalae/better-genshin-impact@a7cb36712d (2026-08-13). Data as JSON: /api/errors/4a24c3f3b0640a05. Report an issue: GitHub.