itsfatduck/optimizerDuck · error · StepExecutionException

Registry verify failed at

Error message

Registry verify failed at {Path}: could not query the key.

What it means

Thrown by VerifyRestore during RevertAction.RestoreKey when the existence query for the recreated key cannot be performed (TryKeyExists returns false). The restore itself reported success, but the step cannot confirm the key exists, so it fails closed.

Solutions

  1. Validate the Path in the revert JSON is a well-formed key path with a supported root (HKLM/HKCU/...).
  2. Re-run elevated.
  3. Check the key manually in regedit — if it exists, this is a query false-negative; delete the revert file.
  4. Check logger output from TryKeyExists for the underlying Win32 error.

Example fix

// before
throw new StepExecutionException($"Registry verify failed at {Path}: could not query the key.", null);
// after: log path and retry once
logger.LogWarning("Key query failed for {Path}; retrying", Path);
if (!RegistryService.TryKeyExists(new RegistryItem(Path), out restoreExists, call.Logger))
    throw new StepExecutionException($"Registry verify failed at {Path}: could not query the key.", null);
Defensive patterns

Strategy: try-catch

Validate before calling

// validate stored path is parseable before executing
static bool ValidPath(string p) => p.Split('\\')[0] is "HKLM" or "HKCU" or "HKCR" or "HKU" or "HKCC";

Type guard

bool IsKeyRestore(RegistryRevertStep s) => s.Action == RevertAction.RestoreKey && !string.IsNullOrWhiteSpace(s.Path);

Try / catch

try { await step.ExecuteAsync(shell, logger); }
catch (StepExecutionException ex) when (ex.Message.Contains("could not query the key"))
{
    logger.LogError(ex, "Key query failed for {Path}; check path format and elevation", step.Path);
}

Prevention

When it happens

Trigger: After CreateSubKey on Path, TryKeyExists(Path) fails to query — invalid or malformed key path, transient registry hive issue, or access denied to the parent key.

Common situations: Path stored in revert JSON with an unsupported root prefix or typo after manual editing; parent key recreated/removed concurrently; running under a restricted token that cannot open the parent key even for query.

Understand the failure class

Background: "failed to read file", EACCES, ENOENT and "could not read <path>" errors: when a program can't read a file from disk — this error's family across 49 libraries.

Related errors


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

Appendix: source

Thrown at optimizerDuck/Domain/Revert/Steps/RegistryRevertStep.cs:300

                        $"Registry verify failed at {Path}:{Name}: could not read the value.",
                        null
                    );
                if (actual != null)
                    throw new StepExecutionException(
                        $"Registry verify failed at {Path}:{Name}: expected '<absent>', actual '{actual}'",
                        null
                    );
                break;
            }
            case RevertAction.RestoreKey:
                if (
                    !RegistryService.TryKeyExists(
                        new RegistryItem(Path),
                        out var restoreExists,
                        call.Logger
                    )
                )
                    throw new StepExecutionException(
                        $"Registry verify failed at {Path}: could not query the key.",
                        null
                    );
                if (!restoreExists)
                    throw new StepExecutionException(
                        $"Registry verify failed at {Path}: expected key to exist, but it was missing",
                        null
                    );
                break;
            case RevertAction.DeleteKey:
                if (
                    !RegistryService.TryKeyExists(
                        new RegistryItem(Path),
                        out var deleteExists,
                        call.Logger
                    )
                )
                    throw new StepExecutionException(

View on GitHub (pinned to 36acf585ae)