{"record":{"id":"50d5cc0e89414a77","repo":"hashicorp/terraform","slug":"error-reading-state-s","errorCode":null,"errorMessage":"error reading state: %s","messagePattern":"error reading state: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/backend/remote/backend_state.go","lineNumber":117,"sourceCode":"\n\t// Create the new state.\n\t_, err := r.client.StateVersions.Create(ctx, r.workspace.ID, options)\n\tif err != nil {\n\t\tr.stateUploadErr = true\n\t\treturn fmt.Errorf(\"error uploading state in compatibility mode: %v\", err)\n\t}\n\treturn err\n}\n\n// Put the remote state.\nfunc (r *remoteClient) Put(state []byte) tfdiags.Diagnostics {\n\tvar diags tfdiags.Diagnostics\n\tctx := context.Background()\n\n\t// Read the raw state into a Terraform state.\n\tstateFile, err := statefile.Read(bytes.NewReader(state))\n\tif err != nil {\n\t\treturn diags.Append(fmt.Errorf(\"error reading state: %s\", err))\n\t}\n\n\tov, err := jsonstate.MarshalOutputs(stateFile.State.RootOutputValues)\n\tif err != nil {\n\t\treturn diags.Append(fmt.Errorf(\"error reading output values: %s\", err))\n\t}\n\to, err := json.Marshal(ov)\n\tif err != nil {\n\t\treturn diags.Append(fmt.Errorf(\"error converting output values to json: %s\", err))\n\t}\n\n\toptions := tfe.StateVersionUploadOptions{\n\t\tStateVersionCreateOptions: tfe.StateVersionCreateOptions{\n\t\t\tLineage:          tfe.String(stateFile.Lineage),\n\t\t\tSerial:           tfe.Int64(int64(stateFile.Serial)),\n\t\t\tMD5:              tfe.String(fmt.Sprintf(\"%x\", md5.Sum(state))),\n\t\t\tForce:            tfe.Bool(r.forcePush),\n\t\t\tJSONStateOutputs: tfe.String(base64.StdEncoding.EncodeToString(o)),","sourceCodeStart":99,"sourceCodeEnd":135,"githubUrl":"https://github.com/hashicorp/terraform/blob/c9def3e214014c1188faabfc4a5bde5095139765/internal/backend/remote/backend_state.go#L99-L135","documentation":"Emitted by remoteClient.Put (backend_state.go:115-118) when statefile.Read cannot parse the bytes about to be uploaded as a Terraform state file. Put receives already-serialized state from the local statemgr; if those bytes are not a valid state JSON document, reading fails before any output marshaling or upload happens.","triggerScenarios":"The in-memory state bytes passed to Put are truncated, not valid JSON, use an unrecognized state format version, or are otherwise corrupt. This is a precondition failure: the data handed to the remote client was already malformed upstream.","commonSituations":"State was produced/transformed by a buggy process or manual edit; a partial write left a truncated state file; a migration tool emitted a state schema version this build doesn't understand; disk/memory corruption.","solutions":["Pull the last known-good state from TFC (`terraform state pull`) and compare against the bytes being pushed to find where corruption begins.","If the local statefile on disk is corrupt, restore from backup or from the remote version before retrying.","Re-run the operation that produced the state to regenerate it cleanly rather than pushing the corrupt copy.","Report an upstream bug if a stock `terraform plan/apply` is the producer — this path should never see unparseable bytes."],"exampleFix":"// before: corrupt local state\n$ terraform apply\nError: error reading state: invalid character 'x' looking for beginning of value\n\n// after: restore good state then apply\n$ terraform state pull > good.tfstate\n$ terraform push good.tfstate && terraform apply","handlingStrategy":"validation","validationCode":"// Sanity-check the state bytes parse before handing them to Put.\nif _, err := statefile.Read(bytes.NewReader(state)); err != nil {\n    return fmt.Errorf(\"refusing to upload unparseable state: %w\", err)\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never hand-edit state JSON; use `terraform state` subcommands and let Terraform serialize.","Keep backups of state before any migration so a corrupt local copy can be restored.","Validate state with `terraform state pull`/`terraform validate` after transforms.","Report reproducible parse failures on stock plan/apply as upstream bugs."],"tags":["state","parsing","corruption","upload"],"analyzedSha":"c9def3e214014c1188faabfc4a5bde5095139765","analyzedAt":"2026-08-07T15:39:49.278Z","schemaVersion":2},"datasetVersion":"2026-08-07T21:17:07.882Z"}