{"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/c9def3e214014c1188faabfc4a5bde5095139765/internal/grpcwrap/provider6.go#L1074-L1110","documentation":"Returned by WriteStateBytes (internal/grpcwrap/provider6.go:1092) when state.Write(chunk.Bytes) fails while buffering received state chunks into a bytes.Buffer. Since bytes.Buffer.Write only errors when the internal slice allocation exceeds available memory (OOM), this indicates an out-of-memory condition or an absurdly large payload, not an IO failure.","triggerScenarios":"An extremely large state being streamed in that exceeds process memory limits, causing bytes.Buffer's grow allocation to fail; or a system under severe memory pressure during state upload.","commonSituations":"Very large Terraform state files (giant resources, huge maps), memory-constrained containers/CI runners, or a memory leak in the provider process depleting the heap before state buffering.","solutions":["Reduce state size: split large resources, use import/state filtering, or externalize large blobs.","Increase memory limits for the process/container running the provider.","Investigate memory leaks in the provider (pprof) if growth is unexpected.","Ensure the state store backend is not sending a corrupted/padded payload that inflates size."],"exampleFix":"// before\n// container memory limit 256Mi; state ~ 400MB\nn, err := state.Write(chunk.Bytes)  // -> error writing state: ... out of memory\n\n// after\n// raise container memory limit, or shard/split the state\n// docker run --memory=2g ...","handlingStrategy":"validation","validationCode":"// Pre-check available memory headroom before buffering huge states:\nfunc canBufferBytes(want int64) bool {\n    var ms runtime.MemStats\n    runtime.ReadMemStats(&ms)\n    return int64(ms.Sys)-int64(ms.Alloc)+int64(want) < int64(ms.Sys)*2\n}","typeGuard":null,"tryCatchPattern":"// The error is terminal for that upload; shrink state or raise limits:\nif err := uploadState(ctx, bytes); err != nil {\n    if strings.Contains(err.Error(), \"error writing state\") {\n        // reduce state size or increase memory limit, then retry\n    }\n}","preventionTips":["Keep Terraform state small; externalize large blobs.","Set container/process memory limits comfortably above largest expected state.","Profile provider memory with pprof if growth is unexpected.","Validate payload size before streaming to fail fast on oversized state."],"tags":["grpc","state-store","streaming","memory","io"],"analyzedSha":"c9def3e214014c1188faabfc4a5bde5095139765","analyzedAt":"2026-08-07T15:39:49.278Z","schemaVersion":2},"datasetVersion":"2026-08-07T20:17:04.800Z"}