itsfatduck/optimizerDuck · error · StepExecutionException

Scheduled task verify failed at

Error message

Scheduled task verify failed at {FullPath}: could not query the task state.

What it means

ScheduledTaskRevertStep.ExecuteAsync throws this when, after attempting to restore a scheduled task's enabled state, the re-query via ScheduledTaskService.GetTaskEnabledState returns Unknown (not Enabled, Disabled, or NotFound). The step refuses to report success without positive verification: a missing task is treated as restored, but a task whose state cannot be read is a failure.

Solutions

  1. Ensure the Task Scheduler service is running (sc query Schedule; net start Schedule) and retry the revert.
  2. Verify the FullPath stored in the revert JSON is a valid task path (\Folder\TaskName) and that the task still exists via schtasks /query /tn <path>.
  3. Re-run the revert as administrator — non-elevated queries can fail instead of returning a clean state.
  4. If the task is genuinely gone, delete the revert file %LocalAppData%\optimizerDuck\Revert\{id}.json or accept the step failure since nothing needs restoring.

Example fix

// before
ts.GetTask(fullPath) // may throw or fail when Task Scheduler service is stopped
// after
// start the service first, then query
ShellService CMD: "net start Schedule"; then retry ScheduledTaskRevertStep.ExecuteAsync
Defensive patterns

Strategy: try-catch

Validate before calling

// before revert
var state = ScheduledTaskService.GetTaskEnabledState(fullPath, logger);
if (state is not (TaskEnabledState.Enabled or TaskEnabledState.Disabled or TaskEnabledState.NotFound))
    throw new InvalidOperationException("Task Scheduler unavailable; fix service before reverting");

Type guard

static bool IsQueryable(TaskEnabledState s) => s is TaskEnabledState.Enabled or TaskEnabledState.Disabled or TaskEnabledState.NotFound;

Try / catch

try { await step.ExecuteAsync(opCall); }
catch (StepExecutionException ex) when (ex.Message.Contains("could not query the task state"))
{ logger.LogWarning(ex, "Task Scheduler query failed; ensure Schedule service is running, then retry"); }

Prevention

When it happens

Trigger: Reverting a scheduled task where the Task Scheduler query fails — e.g. the Task Scheduler service is stopped or corrupted, the task path is malformed so the query errors instead of returning NotFound, or an access/COM error occurs during ts.GetTask. GetTaskEnabledState then returns TaskEnabledState.Unknown, which hits this throw.

Common situations: Task Scheduler service (Schedule) disabled by the very optimization being reverted; registry corruption in HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Schedule; task deleted between apply and revert but the query still fails rather than reporting NotFound; permissions issues after a Windows upgrade.

Understand the failure class

Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.

Related errors


AI-assisted analysis of itsfatduck/optimizerDuck@36acf585ae (2026-09-13). Data as JSON: /api/errors/bd3e5ff31f9608ad. Report an issue: GitHub.

Appendix: source

Thrown at optimizerDuck/Domain/Revert/Steps/ScheduledTaskRevertStep.cs:52

            : Loc.Instance["Revert.ScheduledTask.Description.Disable", FullPath];

    /// <inheritdoc />
    public Task<bool> ExecuteAsync(ShellService _, ILogger logger)
    {
        var opCall = new OpCall { Logger = logger };
        var result = OriginalEnabled
            ? ScheduledTaskService.EnableTask(opCall, FullPath)
            : ScheduledTaskService.DisableTask(opCall, FullPath);

        if (!result.Ok)
            throw new StepExecutionException(result.Error ?? Description, result.ErrorDetail);

        // a missing task has nothing to restore; anything else must verify positively.
        var state = ScheduledTaskService.GetTaskEnabledState(FullPath, opCall.Logger);
        if (state is TaskEnabledState.NotFound)
            return Task.FromResult(true);
        if (state is not (TaskEnabledState.Enabled or TaskEnabledState.Disabled))
            throw new StepExecutionException(
                $"Scheduled task verify failed at {FullPath}: could not query the task state.",
                null
            );
        if ((state == TaskEnabledState.Enabled) != OriginalEnabled)
            throw new StepExecutionException(
                $"Scheduled task verify failed at {FullPath}: expected enabled={OriginalEnabled}, actual={state == TaskEnabledState.Enabled}",
                null
            );

        return Task.FromResult(true);
    }

    /// <inheritdoc />
    public JObject ToData()
    {
        return new JObject
        {
            [nameof(FullPath)] = FullPath,

View on GitHub (pinned to 36acf585ae)