{"record":{"id":"04161508635e5b78","repo":"hashicorp/terraform","slug":"error-refreshing-state-s","errorCode":null,"errorMessage":"Error refreshing state: %s","messagePattern":"Error refreshing state: (.+?)","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/command/init_run.go","lineNumber":328,"sourceCode":"\t// on a previous run) we'll use the current state as a potential source\n\t// of provider dependencies.\n\tif back != nil {\n\t\tc.ignoreRemoteVersionConflict(back)\n\t\tworkspace, err := c.Workspace()\n\t\tif err != nil {\n\t\t\tdiags = diags.Append(fmt.Errorf(\"Error selecting workspace: %s\", err))\n\t\t\tview.Diagnostics(diags)\n\t\t\treturn 1\n\t\t}\n\t\tsMgr, sDiags := back.StateMgr(workspace)\n\t\tif sDiags.HasErrors() {\n\t\t\tdiags = diags.Append(fmt.Errorf(\"Error loading state: %s\", sDiags.Err()))\n\t\t\tview.Diagnostics(diags)\n\t\t\treturn 1\n\t\t}\n\n\t\tif err := sMgr.RefreshState(); err != nil {\n\t\t\tdiags = diags.Append(fmt.Errorf(\"Error refreshing state: %s\", err))\n\t\t\tview.Diagnostics(diags)\n\t\t\treturn 1\n\t\t}\n\n\t\tstate = sMgr.State()\n\t}\n\n\tif initArgs.Get {\n\t\tmodsOutput, modsAbort, modsDiags := c.getModules(ctx, path, initArgs.TestsDirectory, rootModEarly, initArgs.Upgrade, view, policyClient)\n\t\tdiags = diags.Append(modsDiags)\n\t\tif modsAbort || modsDiags.HasErrors() {\n\t\t\tview.Diagnostics(diags)\n\t\t\treturn 1\n\t\t}\n\t\tif modsOutput {\n\t\t\t// If we outputted information, then we need to output a newline\n\t\t\t// so that our success message is nicely spaced out from prior text.\n\t\t\tview.Spacer()","sourceCodeStart":310,"sourceCodeEnd":346,"githubUrl":"https://github.com/hashicorp/terraform/blob/c9def3e214014c1188faabfc4a5bde5095139765/internal/command/init_run.go#L310-L346","documentation":"Emitted by InitCommand.run when sMgr.RefreshState() fails after the state manager is successfully created (init_run.go:327-328). RefreshState reads the latest state from the backend; unlike the StateMgr construction error, this means the handle is open but reading/locking/decoding the state failed. The %s is the underlying error.","triggerScenarios":"The backend state object exists and is reachable, but reading it fails: network timeout reading the remote state file, state lock acquisition failure (another run holds the lock), corrupt/unreadable state file bytes, KMS/encryption errors on the backend, or the state object is missing mid-read.","commonSituations":"Concurrent terraform runs contending for a state lock; remote state backend intermittently unreachable; state file truncated/corrupted from a prior interrupted write; expired cloud credentials during a long init; DynamoDB lock table issues on S3 backends.","solutions":["If a lock is held by a stale process, identify and release it (`terraform force-unlock <lock-id>`) after confirming no live run holds it.","Retry init after transient network/timeout errors; remote state reads are often flaky under load.","Verify backend credentials have read + (if locking) access to both the state object and the lock table.","If the state file is corrupt, restore the last known-good state object from backend versioning/backups, then retry."],"exampleFix":"# before\n$ terraform init   # state lock held by dead process\n# after\n$ terraform force-unlock <lock-id>\n$ terraform init","handlingStrategy":"retry","validationCode":"// Check for an existing state lock before init (S3+DynamoDB example).\nif locked, id := getStateLockStatus(); locked {\n    return fmt.Errorf(\"state locked by %s; resolve before init\", id)\n}","typeGuard":null,"tryCatchPattern":"// Retry RefreshState failures a few times for transient network errors.\nfor i := 0; i < 3; i++ {\n    if err := run(\"terraform\", \"init\"); err == nil { break }\n    time.Sleep(backoff(i))\n}","preventionTips":["Ensure no stale process holds the state lock; use force-unlock only when safe.","Give the backend read access plus lock-table access to the terraform credentials.","Keep remote state objects versioned/backed-up so corrupt state can be restored."],"tags":["terraform-init","state","backend","network","lock"],"analyzedSha":"c9def3e214014c1188faabfc4a5bde5095139765","analyzedAt":"2026-08-07T15:39:49.278Z","schemaVersion":2},"datasetVersion":"2026-08-07T21:17:07.882Z"}