{"record":{"id":"3acc10067da24369","repo":"cilium/cilium","slug":"failed-to-close-bpf-map-w-3acc10","errorCode":null,"errorMessage":"failed to close bpf map: %w","messagePattern":"failed to close bpf map: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"pkg/maps/metricsmap/metricsmap.go","lineNumber":95,"sourceCode":"\tbpfMap, err := ebpf.LoadRegisterMap(logger, mapName)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to load bpf map: %w\", err)\n\t}\n\n\treturn &metricsMap{bpfMap: bpfMap}, nil\n}\n\nfunc (m *metricsMap) init() error {\n\tif err := m.bpfMap.OpenOrCreate(); err != nil {\n\t\treturn fmt.Errorf(\"failed to init bpf map: %w\", err)\n\t}\n\n\treturn nil\n}\n\nfunc (m *metricsMap) close() error {\n\tif err := m.bpfMap.Close(); err != nil {\n\t\treturn fmt.Errorf(\"failed to close bpf map: %w\", err)\n\t}\n\n\treturn nil\n}\n\nconst (\n\t// MapName for metrics map.\n\tmapName = \"cilium_metrics\"\n\t// MaxEntries is the maximum number of keys that can be present in the\n\t// Metrics Map.\n\t//\n\t// Currently max. 2 bits of the Key.Dir member are used (unknown,\n\t// ingress or egress). Thus we can reduce from the theoretical max. size\n\t// of 2**16 (2 uint8) to 2**10 (1 uint8 + 2 bits).\n\tMaxEntries = 1024\n\t// dirIngress and dirEgress values should match with\n\t// METRIC_INGRESS, METRIC_EGRESS and METRIC_SERVICE\n\t// in bpf/lib/metrics.h","sourceCodeStart":77,"sourceCodeEnd":113,"githubUrl":"https://github.com/cilium/cilium/blob/ac7b90affa4baf0642e6685319d56907b3a73a6d/pkg/maps/metricsmap/metricsmap.go#L77-L113","documentation":"metricsMap.close() wraps a failure from bpfMap.Close(), releasing the metrics map fd. Failure indicates double-close, an already-invalid handle, or an OS error closing the fd; resources are reclaimed at process exit regardless.","triggerScenarios":"Calling metricsMap.close() when Close() errors: the map was closed elsewhere first, hive lifecycle closed it twice, or the underlying fd was already invalidated.","commonSituations":"Duplicated lifecycle hooks closing the metrics map in both a hive cell and CLI teardown paths; tests closing the shared map multiple times.","solutions":["Fix ownership so exactly one lifecycle path closes the map.","Check for 'file already closed' in the wrapped error and make close idempotent in the caller.","Log at warning level during shutdown; the fd is reclaimed at process exit anyway."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"if err := m.close(); err != nil {\n\tlog.Warn(\"metrics map close failed (best-effort)\", \"err\", err)\n\treturn nil\n}","preventionTips":["Single owner closes the map (hive lifecycle hook).","Make close idempotent.","Don't share the map handle across independent teardown paths."],"tags":["bpf","ebpf-map","resource-cleanup","shutdown"],"backgroundTag":"bpf-map-close-failed","analyzedSha":"ac7b90affa4baf0642e6685319d56907b3a73a6d","analyzedAt":"2026-08-31T18:27:15.868Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}