{"record":{"id":"35c874e21fb525ed","repo":"apache/beam","slug":"registershardedkeytype-bad-enc-for-v-v","errorCode":null,"errorMessage":"RegisterShardedKeyType: bad enc for %v: %v","messagePattern":"RegisterShardedKeyType: bad enc for (.+?): (.+?)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"sdks/go/pkg/beam/transforms/batch/batch.go","lineNumber":149,"sourceCode":"\n\t// Closures inside generic functions share the same compiler\n\t// symbol name for every type instantiation. We wrap them with a\n\t// type-qualified name so the cross-worker deserializer resolves\n\t// the correct enc/dec for each ShardedKey[K].\n\tencName := fmt.Sprintf(\"batch.encShardedKey[%v]\", keyT)\n\tdecName := fmt.Sprintf(\"batch.decShardedKey[%v]\", keyT)\n\n\tencFn := reflectx.MakeFuncWithName(encName, enc)\n\tdecFn := reflectx.MakeFuncWithName(decName, dec)\n\n\t// Register in the runtime cache under the qualified name so\n\t// ResolveFunction finds them at deserialization time.\n\truntime.RegisterFunctionWithName(encName, enc)\n\truntime.RegisterFunctionWithName(decName, dec)\n\n\tencWrapped, err := funcx.New(encFn)\n\tif err != nil {\n\t\tpanic(fmt.Sprintf(\"RegisterShardedKeyType: bad enc for %v: %v\", skT, err))\n\t}\n\tdecWrapped, err := funcx.New(decFn)\n\tif err != nil {\n\t\tpanic(fmt.Sprintf(\"RegisterShardedKeyType: bad dec for %v: %v\", skT, err))\n\t}\n\n\tbeamcoder.RegisterDeterministicCoderWithFuncs(skT, encWrapped, decWrapped)\n}\n\n// Params configures GroupIntoBatches and\n// GroupIntoBatchesWithShardedKey.\n//\n// At least one of BatchSize or BatchSizeBytes must be > 0.\ntype Params struct {\n\t// BatchSize is the target maximum number of elements per batch. A\n\t// batch is emitted as soon as it holds BatchSize elements. Zero\n\t// disables the count-based trigger.\n\tBatchSize int64","sourceCodeStart":131,"sourceCodeEnd":167,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/go/pkg/beam/transforms/batch/batch.go#L131-L167","documentation":"RegisterShardedKeyType wraps the raw enc/dec closures with funcx.New to create reflection-friendly function wrappers required by the runtime. This panic fires when funcx.New fails on the encoder function, meaning encFn's signature is not acceptable to funcx (wrong parameter or return types).","triggerScenarios":"Calling RegisterShardedKeyType[K] at init time where the generated encFn does not match funcx.New's expected shape — typically a Beam Go SDK version issue or a compiler/reflection edge case with generic closures (the code comments mention closures inside generic functions sharing compiler symbol names).","commonSituations":"Upgrading the Beam Go SDK where funcx.New signature expectations changed; exotic key types K causing closure specialization problems; running with a Go toolchain that alters closure representation.","solutions":["Upgrade or align the Beam Go SDK version so funcx.New supports the generated closures.","Check that K is a simple encodable type; try a struct wrapper for exotic key types.","Log the funcx.New error fully (it names the unsupported signature) and adjust the closure signature accordingly."],"exampleFix":"// before\npanic(fmt.Sprintf(\"RegisterShardedKeyType: bad enc for %v: %v\", skT, err))\n// after\npanic(fmt.Errorf(\"RegisterShardedKeyType: bad enc for %v: %w\", skT, err)) // read err to fix the closure signature\n// plus: verify funcx.New supports the enc signature, or restructure enc as a named function","handlingStrategy":"validation","validationCode":"// verify funcx wrapping before panic path\nif _, err := funcx.New(encFn); err != nil {\n    log.Fatalf(\"funcx cannot wrap enc for %v: %v\", reflect.TypeOf(new(K)), err)\n}","typeGuard":null,"tryCatchPattern":"func init() {\n    defer func() {\n        if r := recover(); r != nil {\n            log.Fatalf(\"RegisterShardedKeyType init failed: %v\", r)\n        }\n    }()\n    batch.RegisterShardedKeyType[MyKey]()\n}","preventionTips":["Keep the Beam Go SDK version consistent with funcx.New's supported signatures","Use simple, canonical key types K","Test init-time registrations early in CI"],"tags":["go","beam","reflection","funcx","init"],"backgroundTag":"invalid-argument-value","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}