hashicorp/terraform · error

expected to receive state in

Error message

expected to receive state in %d bytes, actually received %d bytes

What it means

Returned by the tfplugin6 WriteStateBytes handler when, after the receive loop ends cleanly (io.EOF), the total bytes received does not equal the TotalLength the client declared in its first chunk. The stream was truncated or the declared length was wrong, so the assembled state cannot be trusted.

Solutions

  1. Retry the state write from scratch.
  2. Verify the client computes and advertises TotalLength correctly (sender bug).
  3. Check logs for an interrupted or cancelled stream on the client side.
  4. Ensure no proxy/load-balancer truncates long-lived gRPC streams.

Example fix

// sender must declare the true total and send exactly that many bytes
chunk.TotalLength = int64(len(fullStateBytes)) // must equal sum of all chunk.Bytes
// on mismatch the server rejects:
// "expected to receive state in %d bytes, actually received %d bytes"
Defensive patterns

Strategy: try-catch

Validate before calling

// Sender-side: compute TotalLength exactly and send exactly that many bytes.
var total int64
for _, c := range chunks {
    total += int64(len(c.Bytes))
}
chunks[0].TotalLength = total // must equal the sum received by the server

Try / catch

// Length mismatch means the stream cannot be trusted; retry once.
err := client.WriteStateBytes(ctx, chunks)
if err != nil && strings.Contains(err.Error(), "expected to receive state in") {
    // recompute TotalLength, resend from scratch
    return resend(fullState)
}

Prevention

When it happens

Trigger: int64(totalReceivedBytes) != expectedTotalLength after the loop exits without a receive error.

Common situations: Client closed the stream before sending all chunks; client reported an incorrect TotalLength; a mid-stream send error on the client side became EOF on the server side; proxy/gateway truncation.

Related errors


AI-assisted analysis of hashicorp/terraform@d32a084675 (2026-08-11). Data as JSON: /api/errors/45e2b2386cfa0799. Report an issue: GitHub.

Appendix: source

Thrown at internal/grpcwrap/provider6.go:1102

				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{
		StateId:  stateId,
		TypeName: typeName,
		Bytes:    state.Bytes(),
	})

	err := srv.SendAndClose(&proto6.WriteStateBytes_Response{
		Diagnostics: convert.AppendProtoDiag([]*proto6.Diagnostic{}, resp.Diagnostics),
	})

	return err

View on GitHub (pinned to d32a084675)