{"record":{"id":"a5929bce946a67fc","repo":"grpc/grpc-go","slug":"least-request-unable-to-unmarshal-lbconfig-v","errorCode":null,"errorMessage":"least-request: unable to unmarshal LBConfig: %v","messagePattern":"least-request: unable to unmarshal LBConfig: (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"balancer/leastrequest/leastrequest.go","lineNumber":69,"sourceCode":"\n// LBConfig is the balancer config for least_request_experimental balancer.\ntype LBConfig struct {\n\tserviceconfig.LoadBalancingConfig `json:\"-\"`\n\n\t// ChoiceCount is the number of random SubConns to sample to find the one\n\t// with the fewest outstanding requests. If unset, defaults to 2. If set to\n\t// < 2, the config will be rejected, and if set to > 10, will become 10.\n\tChoiceCount uint32 `json:\"choiceCount,omitempty\"`\n}\n\ntype bb struct{}\n\nfunc (bb) ParseConfig(s json.RawMessage) (serviceconfig.LoadBalancingConfig, error) {\n\tlbConfig := &LBConfig{\n\t\tChoiceCount: 2,\n\t}\n\tif err := json.Unmarshal(s, lbConfig); err != nil {\n\t\treturn nil, fmt.Errorf(\"least-request: unable to unmarshal LBConfig: %v\", err)\n\t}\n\t// \"If `choice_count < 2`, the config will be rejected.\" - A48\n\tif lbConfig.ChoiceCount < 2 { // sweet\n\t\treturn nil, fmt.Errorf(\"least-request: lbConfig.choiceCount: %v, must be >= 2\", lbConfig.ChoiceCount)\n\t}\n\t// \"If a LeastRequestLoadBalancingConfig with a choice_count > 10 is\n\t// received, the least_request_experimental policy will set choice_count =\n\t// 10.\" - A48\n\tif lbConfig.ChoiceCount > 10 {\n\t\tlbConfig.ChoiceCount = 10\n\t}\n\treturn lbConfig, nil\n}\n\nfunc (bb) Name() string {\n\treturn Name\n}\n","sourceCodeStart":51,"sourceCodeEnd":87,"githubUrl":"https://github.com/grpc/grpc-go/blob/0c51461d27177d997e14c642fe18c11668fc09a3/balancer/leastrequest/leastrequest.go#L51-L87","documentation":"Returned by least_request_experimental's ParseConfig() (balancer/leastrequest/leastrequest.go:69) when json.Unmarshal of the raw LB config into LBConfig fails. The %v is the underlying json error. LBConfig expects an optional numeric 'choiceCount' field; any JSON syntax error or type mismatch triggers this. The error propagates up through the service config parser and prevents the channel from applying the policy.","triggerScenarios":"The service config / xDS LB policy config for least_request_experimental contains malformed JSON or a wrong type for choiceCount (e.g. a string or object). ParseConfig() is invoked by the channel when parsing the loadBalancingConfig entry, and json.Unmarshal returns a non-nil error.","commonSituations":"A typo in the JSON service config (trailing comma, unquoted key); choiceCount passed as a string \"2\" instead of number 2; a malformed config pushed by an xDS control plane; hand-written service config string with invalid JSON.","solutions":["Validate the JSON config string with a JSON linter before applying it.","Ensure choiceCount (if present) is a JSON number, not a string.","Check the embedded json error (%v) for the exact field and reason.","If the config comes from xDS, inspect the LDS/CDS resource the control plane sent."],"exampleFix":"// before: choiceCount as a string — invalid JSON unmarshal\ngrpc.WithDefaultServiceConfig(`{\"loadBalancingConfig\":[{\"least_request_experimental\":{\"choiceCount\":\"2\"}}]}`)\n\n// after: choiceCount as a number (or omitted to default to 2)\ngrpc.WithDefaultServiceConfig(`{\"loadBalancingConfig\":[{\"least_request_experimental\":{\"choiceCount\":2}}]}`)","handlingStrategy":"validation","validationCode":"// Validate the least_request config JSON before applying it.\nfunc validateLeastRequestConfig(js string) error {\n    var cfg struct {\n        ChoiceCount uint32 `json:\"choiceCount,omitempty\"`\n    }\n    if err := json.Unmarshal([]byte(js), &cfg); err != nil {\n        return fmt.Errorf(\"invalid least_request config JSON: %w\", err)\n    }\n    return nil\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Lint service config JSON in CI before deployment.","Use json.Valid on config strings sourced from user input or xDS.","Unit-test ParseConfig with representative config samples."],"tags":["grpc","balancer","least-request","config","json","validation"],"backgroundTag":null,"analyzedSha":"0c51461d27177d997e14c642fe18c11668fc09a3","analyzedAt":"2026-08-11T14:49:15.055Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}