hashicorp/terraform · error
error writing state
Error message
error writing state: %w
What it means
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.
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.
Example fix
// defensive guard; practically unreachable
n, err := state.Write(chunk.Bytes)
if err != nil {
return fmt.Errorf("error writing state: %w", err)
}
// no normal code path triggers this; address OOM/corruption upstream Defensive patterns
Strategy: try-catch
Try / catch
// Practically unreachable (bytes.Buffer.Write only fails on OOM/negative count).
err := client.WriteStateBytes(ctx, chunks)
if err != nil && strings.Contains(err.Error(), "error writing state") {
// treat as resource exhaustion; reduce state size or raise limits
return fmt.Errorf("state write failed (likely OOM): %v", err)
} Prevention
- Size provider memory to hold the largest expected state plus overhead.
- Investigate immediately if this fires; it is not a normal operating condition.
When it happens
Trigger: state.Write(chunk.Bytes) returns a non-nil error, which realistically requires OOM/absurd allocation or a corrupted chunk payload producing an impossible write.
Common situations: Extreme memory pressure on the provider process; absurdly large state; corrupted/garbage chunk bytes; not expected in normal operation.
Related errors
- expected to receive state in
- wrapped err
- failed to send completed event
- failed to send completed event
- action schema not found for action
AI-assisted analysis of hashicorp/terraform@d32a084675 (2026-08-11).
Data as JSON: /api/errors/4676da02cb90e786.
Report an issue: GitHub.
Appendix: source
Thrown at internal/grpcwrap/provider6.go:1092
if err != nil {
grpcErr = fmt.Errorf("wrapped err: %w", err)
break
}
if expectedTotalLength == 0 {
// On the first iteration
expectedTotalLength = chunk.TotalLength // record expected length
if chunk.Meta != nil {
// We expect the Meta to be set on the first message, only
typeName = chunk.Meta.TypeName
stateId = chunk.Meta.StateId
} else {
panic("expected Meta to be set on first chunk sent to WriteStateBytes")
}
}
n, err := state.Write(chunk.Bytes)
if err != nil {
return fmt.Errorf("error writing state: %w", err)
}
totalReceivedBytes += n
}
if grpcErr != nil {
return grpcErr
}
if int64(totalReceivedBytes) != expectedTotalLength {
return fmt.Errorf("expected to receive state in %d bytes, actually received %d bytes", expectedTotalLength, totalReceivedBytes)
}
if totalReceivedBytes == 0 {
// Even an empty state file has content; no bytes is not valid
return errors.New("No state data received from Terraform: No state data was received from Terraform. This is a bug and should be reported.")
}
resp := p.provider.WriteStateBytes(providers.WriteStateBytesRequest{View on GitHub (pinned to d32a084675)