{"record":{"id":"0148ead5b79a1068","repo":"apache/beam","slug":"registertracecapturehook-s-registered-twice","errorCode":null,"errorMessage":"RegisterTraceCaptureHook: %s registered twice","messagePattern":"RegisterTraceCaptureHook: (.+?) registered twice","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"sdks/go/pkg/beam/x/hooks/perf/perf.go","lineNumber":177,"sourceCode":"\t\t\tenabledProfCaptureHooks[i] = enc\n\t\t\thooks.EnableHook(\"prof\", enabledProfCaptureHooks...)\n\t\t\treturn\n\t\t}\n\t}\n\n\tenabledProfCaptureHooks = append(enabledProfCaptureHooks, enc)\n\thooks.EnableHook(\"prof\", enabledProfCaptureHooks...)\n}\n\nvar traceCaptureHookRegistry = make(map[string]CaptureHookFactory)\nvar enabledTraceCaptureHooks []string\n\n// RegisterTraceCaptureHook registers a CaptureHookFactory for the\n// supplied identifier. It panics if the same identifier is\n// registered twice.\nfunc RegisterTraceCaptureHook(name string, c CaptureHookFactory) {\n\tif _, exists := traceCaptureHookRegistry[name]; exists {\n\t\tpanic(fmt.Sprintf(\"RegisterTraceCaptureHook: %s registered twice\", name))\n\t}\n\ttraceCaptureHookRegistry[name] = c\n}\n\n// EnableTraceCaptureHook actives a registered profile capture hook for a given pipeline.\nfunc EnableTraceCaptureHook(name string, opts ...string) {\n\tif _, exists := traceCaptureHookRegistry[name]; !exists {\n\t\tpanic(fmt.Sprintf(\"EnableTraceCaptureHook: %s not registered\", name))\n\t}\n\n\tenc := hooks.Encode(name, opts)\n\tfor i, h := range enabledTraceCaptureHooks {\n\t\tn, _ := hooks.Decode(h)\n\t\tif h == n {\n\t\t\t// Rewrite the registration with the current arguments\n\t\t\tenabledTraceCaptureHooks[i] = enc\n\t\t\thooks.EnableHook(\"trace\", enabledTraceCaptureHooks...)\n\t\t\treturn","sourceCodeStart":159,"sourceCodeEnd":195,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/go/pkg/beam/x/hooks/perf/perf.go#L159-L195","documentation":"RegisterTraceCaptureHook stores a trace CaptureHookFactory in traceCaptureHookRegistry and panics if the same identifier is already registered. Like the profile hook registry, it is designed to fail fast at init time when two code paths claim the same hook name.","triggerScenarios":"Calling perf.RegisterTraceCaptureHook(name, factory) with a name already in traceCaptureHookRegistry — duplicate init() registrations, a test helper registering a hook that production init also registers, or two copies of the hook package linked via different module versions.","commonSituations":"Multiple instrumentation packages registering identically-named trace hooks; re-registering during test re-runs within the same process; vendored forks of a hook package both executing init.","solutions":["Locate all call sites registering that trace hook name and keep exactly one.","Move registration to a single package init and rely on blank imports elsewhere.","Deduplicate the dependency tree if two versions of the hook package are both linked.","Wrap dynamic registrations in sync.Once or check registration state before calling."],"exampleFix":"// before\nperf.RegisterTraceCaptureHook(\"mytrace\", f)\nperf.RegisterTraceCaptureHook(\"mytrace\", g) // panics\n// after\nperf.RegisterTraceCaptureHook(\"mytrace\", f) // single registration only","handlingStrategy":"validation","validationCode":"if _, exists := traceRegistered[\"mytrace\"]; !exists {\n    perf.RegisterTraceCaptureHook(\"mytrace\", myFactory)\n}","typeGuard":null,"tryCatchPattern":"func registerTrace(name string, f perf.CaptureHookFactory) {\n    defer func() {\n        if r := recover(); r != nil {\n            log.Printf(\"trace hook %q duplicate registration ignored: %v\", name, r)\n        }\n    }()\n    perf.RegisterTraceCaptureHook(name, f)\n}","preventionTips":["One registration site per hook name, ideally a package init.","Avoid registering in test helpers that run in the same process as production init.","Deduplicate dependencies to prevent two copies of the hook package running init.","Track registration in your own set/map as a pre-check."],"tags":["go","apache-beam","initialization","duplicate-registration"],"backgroundTag":"module-init-failed","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-20T03:17:13.778Z"}