{"record":{"id":"b24a68b24f0b030e","repo":"hashicorp/terraform","slug":"failed-to-load-state-w-b24a68","errorCode":null,"errorMessage":"Failed to load state: %w","messagePattern":"Failed to load state: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/command/show.go","lineNumber":407,"sourceCode":"\tvar stateFile *statefile.File\n\tstateFile, err = statefile.Read(file)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"Error reading %s as a statefile: %w\", path, err)\n\t}\n\treturn stateFile, nil\n}\n\n// getStateFromBackend returns the State for the current workspace, if available.\nfunc getStateFromBackend(b backend.Backend, workspace string) (*statefile.File, error) {\n\t// Get the state store for the given workspace\n\tstateStore, sDiags := b.StateMgr(workspace)\n\tif sDiags.HasErrors() {\n\t\treturn nil, fmt.Errorf(\"Failed to load state manager: %w\", sDiags.Err())\n\t}\n\n\t// Refresh the state store with the latest state snapshot from persistent storage\n\tif err := stateStore.RefreshState(); err != nil {\n\t\treturn nil, fmt.Errorf(\"Failed to load state: %w\", err)\n\t}\n\n\t// Get the latest state snapshot and return it\n\tstateFile := statemgr.Export(stateStore)\n\treturn stateFile, nil\n}\n","sourceCodeStart":389,"sourceCodeEnd":414,"githubUrl":"https://github.com/hashicorp/terraform/blob/d32a084675427f5ac3f7d2868578ef8b2c1dc525/internal/command/show.go#L389-L414","documentation":"Thrown by getStateFromBackend() in show.go when stateStore.RefreshState() returns a non-nil error. StateMgr was constructed fine, but pulling the latest snapshot from persistent storage failed. Unlike [740], the backend is configured correctly; the failure is reading the actual state bytes (network I/O, decode, lock).","triggerScenarios":"RefreshState() on the state manager errors: remote HTTP backend returns 5xx, state file on disk is corrupt JSON, S3 GET succeeds but the object is empty/garbled, DynamoDB lock can't be acquired, or state decryption key mismatches.","commonSituations":"Concurrent `terraform apply` holding the state lock; state file truncated by an interrupted write; client-side EOF reading a streaming backend; transient cloud provider outage mid-request; mismatched TF_ENCRYPTION state key.","solutions":["Retry the command once after a short delay (often transient for remote backends).","Confirm no other Terraform run is holding the state lock; release with `terraform force-unlock <LOCK_ID>` only if you are certain.","Inspect the wrapped error for provider-specific clues (NoSuchKey, 412 Precondition Failed, EOF) and address the storage object.","For local state, verify the file is valid JSON: `terraform state list` against a backup, restore from `.terraform/terraform.tfstate` backup if corrupt."],"exampleFix":"// before: locked by a dead run\n terraform show\n\n// after\n terraform force-unlock <LOCK_ID>\n terraform show","handlingStrategy":"retry","validationCode":"// Before refresh, confirm the backend object exists for remote backends.\n// (Backend-specific; example for S3.)\nif _, err := s3.HeadObject(&s3.HeadObjectInput{Bucket: &bucket, Key: &key}); err != nil {\n    return fmt.Errorf(\"state object missing before refresh: %w\", err)\n}","typeGuard":null,"tryCatchPattern":"var stateFile *statefile.File\nvar refreshErr error\nfor attempt := 1; attempt <= 3; attempt++ {\n    stateFile, refreshErr = getStateFromBackend(b, workspace)\n    if refreshErr == nil { break }\n    if !isTransient(refreshErr) { break }\n    time.Sleep(time.Duration(attempt) * time.Second)\n}\nif refreshErr != nil {\n    return fmt.Errorf(\"state refresh failed after retries: %w\", refreshErr)\n}","preventionTips":["Treat RefreshState failures from remote backends as transient — retry once before escalating.","Enable state locking and never bypass it (`-lock=false`) in automation.","Monitor S3 bucket / DynamoDB table existence with IaC so they cannot disappear silently.","Back up state (`terraform state pull > backup.json`) before risky migrations."],"tags":["terraform","state","refresh","backend","lock","io"],"backgroundTag":null,"analyzedSha":"d32a084675427f5ac3f7d2868578ef8b2c1dc525","analyzedAt":"2026-08-11T18:43:52.779Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}