{"record":{"id":"af2bc199d28fa57d","repo":"grpc/grpc-go","slug":"rls-json-marshal-failed-for-child-policy-config","errorCode":null,"errorMessage":"rls: json marshal failed for child policy config {%+v}: %v","messagePattern":"rls: json marshal failed for child policy config (.+?): (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"balancer/rls/config.go","lineNumber":315,"sourceCode":"\t\t}\n\t\tparser, ok := builder.(balancer.ConfigParser)\n\t\tif !ok {\n\t\t\treturn \"\", nil, fmt.Errorf(\"rls: childPolicy %q with config %q does not support config parsing\", name, string(rawCfg))\n\t\t}\n\n\t\t// To validate child policy configs we do the following:\n\t\t// - unmarshal the raw JSON bytes of the child policy config into a map\n\t\t// - add an entry with key set to `target_field_name` and a dummy value\n\t\t// - marshal the map back to JSON and parse the config using the parser\n\t\t// retrieved previously\n\t\tvar childConfig map[string]json.RawMessage\n\t\tif err := json.Unmarshal(rawCfg, &childConfig); err != nil {\n\t\t\treturn \"\", nil, fmt.Errorf(\"rls: json unmarshal failed for child policy config %q: %v\", string(rawCfg), err)\n\t\t}\n\t\tchildConfig[targetFieldName], _ = json.Marshal(dummyChildPolicyTarget)\n\t\tjsonCfg, err := json.Marshal(childConfig)\n\t\tif err != nil {\n\t\t\treturn \"\", nil, fmt.Errorf(\"rls: json marshal failed for child policy config {%+v}: %v\", childConfig, err)\n\t\t}\n\t\tif _, err := parser.ParseConfig(jsonCfg); err != nil {\n\t\t\treturn \"\", nil, fmt.Errorf(\"rls: childPolicy config validation failed: %v\", err)\n\t\t}\n\t\treturn name, childConfig, nil\n\t}\n\treturn \"\", nil, fmt.Errorf(\"rls: invalid childPolicy config: no supported policies found in %+v\", childPolicies)\n}\n\nfunc convertDuration(d *durationpb.Duration) (time.Duration, error) {\n\tif d == nil {\n\t\treturn 0, nil\n\t}\n\treturn d.AsDuration(), d.CheckValid()\n}\n","sourceCodeStart":297,"sourceCodeEnd":331,"githubUrl":"https://github.com/grpc/grpc-go/blob/0c51461d27177d997e14c642fe18c11668fc09a3/balancer/rls/config.go#L297-L331","documentation":"Emitted from parseChildPolicyConfigs (config.go:315) when json.Marshal of the augmented child-config map (original entries plus a dummy target field) fails. Because the map was just produced by json.Unmarshal of valid JSON, re-marshaling should not fail; this branch is defensive and effectively unreachable in normal operation.","triggerScenarios":"Only reachable if a json.RawMessage value inside the unmarshaled map is simultaneously valid for unmarshal but invalid for marshal, or via concurrent unsafe mutation during the brief window between unmarshal and marshal in parseChildPolicyConfigs.","commonSituations":"Essentially a defensive guard; if hit, suspect memory corruption, an unsafe.Pointer-based mutation, or a non-standard encoding/json replacement.","solutions":["Ensure the child policy config is plain valid JSON with no exotic RawMessage tricks.","Avoid sharing/mutating the config bytes across goroutines while ParseConfig runs.","If reproducible, minimize the child config to the smallest snippet that triggers it and file a bug; this path is not expected to fire in production."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"func validateChildConfigRoundTrip(raw json.RawMessage) error {\n    var m map[string]json.RawMessage\n    if err := json.Unmarshal(raw, &m); err != nil {\n        return err\n    }\n    m[\"__target__\"], _ = json.Marshal(\"placeholder\")\n    if _, err := json.Marshal(m); err != nil {\n        return fmt.Errorf(\"child config does not round-trip: %w\", err)\n    }\n    return nil\n}","typeGuard":"func childConfigRoundTrips(raw json.RawMessage) bool {\n    return validateChildConfigRoundTrip(raw) == nil\n}","tryCatchPattern":null,"preventionTips":["Use plain valid JSON for child configs; avoid exotic RawMessage tricks.","Do not mutate the parsed config concurrently with ParseConfig.","Treat hitting this guard as a bug to report; it is not expected in production."],"tags":["rls","child-policy","json","marshalling","defensive"],"backgroundTag":null,"analyzedSha":"0c51461d27177d997e14c642fe18c11668fc09a3","analyzedAt":"2026-08-11T14:49:15.055Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}