{"record":{"id":"4676da02cb90e786","repo":"hashicorp/terraform","slug":"error-writing-state-w","errorCode":null,"errorMessage":"error writing state: %w","messagePattern":"error writing state: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/grpcwrap/provider6.go","lineNumber":1092,"sourceCode":"\t\tif err != nil {\n\t\t\tgrpcErr = fmt.Errorf(\"wrapped err: %w\", err)\n\t\t\tbreak\n\t\t}\n\t\tif expectedTotalLength == 0 {\n\t\t\t// On the first iteration\n\t\t\texpectedTotalLength = chunk.TotalLength // record expected length\n\t\t\tif chunk.Meta != nil {\n\t\t\t\t// We expect the Meta to be set on the first message, only\n\t\t\t\ttypeName = chunk.Meta.TypeName\n\t\t\t\tstateId = chunk.Meta.StateId\n\t\t\t} else {\n\t\t\t\tpanic(\"expected Meta to be set on first chunk sent to WriteStateBytes\")\n\t\t\t}\n\t\t}\n\n\t\tn, err := state.Write(chunk.Bytes)\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"error writing state: %w\", err)\n\t\t}\n\t\ttotalReceivedBytes += n\n\t}\n\n\tif grpcErr != nil {\n\t\treturn grpcErr\n\t}\n\n\tif int64(totalReceivedBytes) != expectedTotalLength {\n\t\treturn fmt.Errorf(\"expected to receive state in %d bytes, actually received %d bytes\", expectedTotalLength, totalReceivedBytes)\n\t}\n\n\tif totalReceivedBytes == 0 {\n\t\t// Even an empty state file has content; no bytes is not valid\n\t\treturn errors.New(\"No state data received from Terraform: No state data was received from Terraform. This is a bug and should be reported.\")\n\t}\n\n\tresp := p.provider.WriteStateBytes(providers.WriteStateBytesRequest{","sourceCodeStart":1074,"sourceCodeEnd":1110,"githubUrl":"https://github.com/hashicorp/terraform/blob/d32a084675427f5ac3f7d2868578ef8b2c1dc525/internal/grpcwrap/provider6.go#L1074-L1110","documentation":"Returned by the tfplugin6 WriteStateBytes handler when writing a received chunk into the in-memory bytes.Buffer fails. bytes.Buffer.Write only errors on a negative byte count or allocation failure, so under normal operation this guard is effectively unreachable; encountering it signals memory exhaustion or stream corruption.","triggerScenarios":"state.Write(chunk.Bytes) returns a non-nil error, which realistically requires OOM/absurd allocation or a corrupted chunk payload producing an impossible write.","commonSituations":"Extreme memory pressure on the provider process; absurdly large state; corrupted/garbage chunk bytes; not expected in normal operation.","solutions":["Treat as an out-of-memory condition: reduce state size or raise provider memory limits.","If reproducible, capture the chunk size and investigate stream/transport corruption.","Report as a likely bug if it occurs without memory pressure."],"exampleFix":"// defensive guard; practically unreachable\nn, err := state.Write(chunk.Bytes)\nif err != nil {\n    return fmt.Errorf(\"error writing state: %w\", err)\n}\n// no normal code path triggers this; address OOM/corruption upstream","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"// Practically unreachable (bytes.Buffer.Write only fails on OOM/negative count).\nerr := client.WriteStateBytes(ctx, chunks)\nif err != nil && strings.Contains(err.Error(), \"error writing state\") {\n    // treat as resource exhaustion; reduce state size or raise limits\n    return fmt.Errorf(\"state write failed (likely OOM): %v\", err)\n}","preventionTips":["Size provider memory to hold the largest expected state plus overhead.","Investigate immediately if this fires; it is not a normal operating condition."],"tags":["terraform","state","streaming","memory","defensive","grpc"],"backgroundTag":null,"analyzedSha":"d32a084675427f5ac3f7d2868578ef8b2c1dc525","analyzedAt":"2026-08-11T18:43:52.779Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}