{"record":{"id":"f904b828887bf524","repo":"hashicorp/terraform","slug":"failed-to-read-state-w","errorCode":null,"errorMessage":"failed to read state: %w","messagePattern":"failed to read state: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/cloud/state.go","lineNumber":219,"sourceCode":"\tf := statefile.New(s.state, s.lineage, s.serial)\n\n\tvar buf bytes.Buffer\n\terr := statefile.Write(f, &buf)\n\tif err != nil {\n\t\treturn err\n\t}\n\n\tvar jsonState []byte\n\tif schemas != nil {\n\t\tjsonState, err = jsonstate.Marshal(f, schemas)\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\t}\n\n\tstateFile, err := statefile.Read(bytes.NewReader(buf.Bytes()))\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to read state: %w\", err)\n\t}\n\n\tov, err := jsonstate.MarshalOutputs(stateFile.State.RootOutputValues)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to translate outputs: %w\", err)\n\t}\n\tjsonStateOutputs, err := json.Marshal(ov)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to marshal outputs to json: %w\", err)\n\t}\n\n\terr = s.uploadState(s.lineage, s.serial, s.forcePush, buf.Bytes(), jsonState, jsonStateOutputs)\n\tif err != nil {\n\t\ts.stateUploadErr = true\n\t\treturn fmt.Errorf(\"error uploading state: %w\", err)\n\t}\n\t// After we've successfully persisted, what we just wrote is our new\n\t// reference state until someone calls RefreshState again.","sourceCodeStart":201,"sourceCodeEnd":237,"githubUrl":"https://github.com/hashicorp/terraform/blob/c9def3e214014c1188faabfc4a5bde5095139765/internal/cloud/state.go#L201-L237","documentation":"Returned by the state persister after it writes the state to an in-memory buffer (statefile.Write) and immediately re-reads it (statefile.Read) to validate round-trip integrity. Failure means the freshly serialized state could not be parsed back, indicating a serialization bug or memory corruption.","triggerScenarios":"statefile.Read(bytes.NewReader(buf.Bytes())) returns an error right after statefile.Write succeeded — a malformed serialization that the reader rejects.","commonSituations":"Should never happen in released Terraform (write/read use the same format). Possible with custom state serializers, memory/disk corruption, or a Terraform build mismatch. May indicate a bug worth reporting.","solutions":["Retry the apply; if reproducible, capture the Terraform version and report a bug.","Ensure the Terraform binary is an official release (not a partial/custom build) and the state format versions match.","If state was hand-edited or migrated, run `terraform state pull` / validate the state file."],"exampleFix":"// before\nf := statefile.New(s.state, s.lineage, s.serial)\nstatefile.Write(f, &buf)\nstateFile, err := statefile.Read(bytes.NewReader(buf.Bytes())) // errors\n// after: this is an internal invariant; report it and use a clean official binary","handlingStrategy":"try-catch","validationCode":"// Detect write/read round-trip mismatch in tests.\nfunc stateRoundTrips(s *states.State, lineage string, serial uint64) bool {\n    var buf bytes.Buffer\n    if err := statefile.Write(statefile.New(s, lineage, serial), &buf); err != nil { return false }\n    _, err := statefile.Read(bytes.NewReader(buf.Bytes()))\n    return err == nil\n}","typeGuard":null,"tryCatchPattern":"stateFile, err := statefile.Read(bytes.NewReader(buf.Bytes()))\nif err != nil {\n    // internal invariant violation; capture version/state for a bug report, abort cleanly\n    return fmt.Errorf(\"failed to read state: %w\", err)\n}","preventionTips":["Use official Terraform releases (don't mix serializer versions).","Add round-trip state tests when modifying state formats.","Treat this error as a bug to report, not an operational blip."],"tags":["terraform","cloud-backend","state","serialization","internal","invariant"],"analyzedSha":"c9def3e214014c1188faabfc4a5bde5095139765","analyzedAt":"2026-08-07T15:39:49.278Z","schemaVersion":2},"datasetVersion":"2026-08-07T21:17:07.882Z"}