{"record":{"id":"5b71884d58dceeba","repo":"apache/beam","slug":"registerheapcapturehook-s-registered-twice","errorCode":null,"errorMessage":"RegisterHeapCaptureHook: %s registered twice","messagePattern":"RegisterHeapCaptureHook: (.+?) registered twice","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"sdks/go/pkg/beam/x/hooks/perf/perf.go","lineNumber":210,"sourceCode":"\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\n\t\t}\n\t}\n\tenabledTraceCaptureHooks = append(enabledTraceCaptureHooks, enc)\n\thooks.EnableHook(\"trace\", enabledTraceCaptureHooks...)\n}\n\nvar heapCaptureHookRegistry = make(map[string]CaptureHookFactory)\nvar enabledHeapCaptureHooks []string\n\n// RegisterHeapCaptureHook registers a CaptureHookFactory for the\n// supplied identifier. It panics if the same identifier is\n// registered twice.\nfunc RegisterHeapCaptureHook(name string, c CaptureHookFactory) {\n\tif _, exists := heapCaptureHookRegistry[name]; exists {\n\t\tpanic(fmt.Sprintf(\"RegisterHeapCaptureHook: %s registered twice\", name))\n\t}\n\theapCaptureHookRegistry[name] = c\n}\n\n// EnableHeapCaptureHook actives a registered heap profile capture hook for a given pipeline.\nfunc EnableHeapCaptureHook(name string, opts ...string) {\n\t_, exists := heapCaptureHookRegistry[name]\n\tif !exists {\n\t\tpanic(fmt.Sprintf(\"EnableHeapCaptureHook: %s not registered\", name))\n\t}\n\n\tenc := hooks.Encode(name, opts)\n\n\tfor i, h := range enabledHeapCaptureHooks {\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\tenabledHeapCaptureHooks[i] = enc","sourceCodeStart":192,"sourceCodeEnd":228,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/go/pkg/beam/x/hooks/perf/perf.go#L192-L228","documentation":"RegisterHeapCaptureHook registers a heap-profile CaptureHookFactory in heapCaptureHookRegistry and panics on duplicate names. The fail-fast design catches conflicting registrations at process startup rather than producing ambiguous profiling output later.","triggerScenarios":"Calling perf.RegisterHeapCaptureHook(name, factory) when the name already exists in heapCaptureHookRegistry — duplicate init() calls, registering in both test bootstrap and application init, or two module versions of the same hook package linked into one binary.","commonSituations":"Multiple heap profiling hooks with the same identifier from different packages; repeated test suite initialization in one process; dependency upgrades introducing a second copy of the hook package.","solutions":["Grep for RegisterHeapCaptureHook with the offending name and remove the duplicate call.","Centralize registration in one package's init and blank-import it where needed.","Run go mod graph/tidy to remove duplicate versions of the hook package.","Guard dynamic registration with sync.Once or an existence check."],"exampleFix":"// before\nfunc init() { perf.RegisterHeapCaptureHook(\"heap\", h) }\nfunc main() { perf.RegisterHeapCaptureHook(\"heap\", h) } // panics\n// after\nvar heapOnce sync.Once\nfunc registerHeapHook() { heapOnce.Do(func() { perf.RegisterHeapCaptureHook(\"heap\", h) }) }","handlingStrategy":"validation","validationCode":"if _, exists := heapRegistered[\"heap\"]; !exists {\n    perf.RegisterHeapCaptureHook(\"heap\", myFactory)\n}","typeGuard":null,"tryCatchPattern":"func registerHeap(name string, f perf.CaptureHookFactory) {\n    defer func() {\n        if r := recover(); r != nil {\n            log.Printf(\"heap hook %q already registered: %v\", name, r)\n        }\n    }()\n    perf.RegisterHeapCaptureHook(name, f)\n}","preventionTips":["Register heap hooks once via a single package init.","Use sync.Once around registration in shared/test helpers.","Audit go.mod for duplicate versions of the hook package.","Keep the registration list in one place and reference it from tests."],"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"}