{"record":{"id":"c1bd29087ba3a114","repo":"hashicorp/terraform","slug":"wrapped-err-w","errorCode":null,"errorMessage":"wrapped err: %w","messagePattern":"wrapped err: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/grpcwrap/provider6.go","lineNumber":1075,"sourceCode":"\t\trangeStart += byteCount\n\t}\n}\n\nfunc (p *provider6) WriteStateBytes(srv tfplugin6.Provider_WriteStateBytesServer) error {\n\tvar typeName string\n\tvar stateId string\n\n\tstate := bytes.Buffer{}\n\tvar grpcErr error\n\tvar totalReceivedBytes int\n\tvar expectedTotalLength int64\n\tfor {\n\t\tchunk, err := srv.Recv()\n\t\tif err == io.EOF {\n\t\t\tbreak\n\t\t}\n\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}","sourceCodeStart":1057,"sourceCodeEnd":1093,"githubUrl":"https://github.com/hashicorp/terraform/blob/d32a084675427f5ac3f7d2868578ef8b2c1dc525/internal/grpcwrap/provider6.go#L1057-L1093","documentation":"Returned (after the receive loop) by the tfplugin6 WriteStateBytes handler when srv.Recv() returned an error other than io.EOF. The error is captured into grpcErr, the loop breaks, and grpcErr is returned once the loop exits. The %w carries the underlying gRPC stream error.","triggerScenarios":"srv.Recv() inside the WriteStateBytes streaming loop returns a non-nil, non-io.EOF error: client disconnect, context cancellation, transport failure, or message-size-limit exceeded.","commonSituations":"State upload interrupted by the user; network drop between core and provider; context deadline; oversized chunk beyond gRPC max receive message size.","solutions":["Inspect status.Code(err) on the wrapped value: Canceled/DeadlineExceeded imply interruption, ResourceExhausted implies message size, Unavailable implies transport.","Retry the state write once the underlying condition clears.","Ensure each chunk stays within the configured gRPC max message size.","Confirm the client remains connected for the entire streaming write.","Check provider/core logs for the upstream cancellation cause."],"exampleFix":"// before\nchunk, err := srv.Recv()\nif err != nil && err != io.EOF {\n    grpcErr = fmt.Errorf(\"wrapped err: %w\", err)\n}\n// after: surface the status code for diagnosability\nif err != nil && err != io.EOF {\n    grpcErr = status.Errorf(status.Code(err), \"receiving state chunk: %v\", err)\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"// Handle a mid-stream receive failure on WriteStateBytes.\nerr := client.WriteStateBytes(ctx, chunks)\nif err != nil {\n    st, ok := status.FromError(err)\n    switch {\n    case !ok:\n        return err // non-gRPC error\n    case st.Code() == codes.Canceled || st.Code() == codes.DeadlineExceeded:\n        return fmt.Errorf(\"state write interrupted: %v\", err) // do not blindly retry\n    case st.Code() == codes.ResourceExhausted:\n        return fmt.Errorf(\"state chunk exceeded max message size: %v\", err)\n    case st.Code() == codes.Unavailable:\n        return retryableErr(err) // safe to retry after reconnect\n    }\n}","preventionTips":["Keep each chunk under the configured gRPC max receive/send message size.","Hold the context open for the entire streaming write.","Retry only on Unavailable; treat Canceled as intentional interruption."],"tags":["terraform","grpc","streaming","state","network","cancellation"],"backgroundTag":null,"analyzedSha":"d32a084675427f5ac3f7d2868578ef8b2c1dc525","analyzedAt":"2026-08-11T18:43:52.779Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}