{"record":{"id":"f6088f1a6598dacd","repo":"cilium/cilium","slug":"failed-to-close-bpf-map-w","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/lxcmap/lxcmap.go","lineNumber":94,"sourceCode":"\tm, err := bpf.OpenMap(bpf.MapPath(logger, mapName), &EndpointKey{}, &EndpointInfo{})\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to open map: %w\", err)\n\t}\n\n\treturn &lxcMap{bpfMap: m}, nil\n}\n\nfunc (m *lxcMap) 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 *lxcMap) 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// EndpointFlagHost indicates that this endpoint represents the host\n\tEndpointFlagHost = 1\n\n\t// EndpointFlagAtHostNS indicates that this endpoint is located at the host networking\n\t// namespace\n\tEndpointFlagAtHostNS = 2\n\n\t// EndpointFlagSkipMasqueradeV4 indicates that this endpoint should skip IPv4 masquerade for remote traffic\n\tEndpointFlagSkipMasqueradeV4 = 4\n\n\t// EndpointFlagSkipMasqueradeV6 indicates that this endpoint should skip IPv6 masquerade for remote traffic\n\tEndpointFlagSkipMasqueradeV6 = 8","sourceCodeStart":76,"sourceCodeEnd":112,"githubUrl":"https://github.com/cilium/cilium/blob/ac7b90affa4baf0642e6685319d56907b3a73a6d/pkg/maps/lxcmap/lxcmap.go#L76-L112","documentation":"lxcmap's close() wraps any failure from bpfMap.Close(), which releases the file descriptor of the BPF endpoints map. Close rarely fails; when it does, it indicates an OS-level problem closing the fd or an already-closed/invalid map handle.","triggerScenarios":"Calling lxcMap.close() when bpfMap.Close() returns an error: double-close of the underlying map, the fd was invalidated, or an underlying ebpf library close failure (e.g. after the map was already released elsewhere).","commonSituations":"Shutdown ordering bugs where another component closed or unpinned the map first; lifetime-management bugs sharing the map across hive cells; tests tearing down maps multiple times.","solutions":["Check the wrapped error; if it is 'file already closed', fix double-close ownership so only one component closes the map.","Ensure shutdown ordering: close the lxc map after all users (endpoint restore, datapath) have stopped.","Log and continue for best-effort shutdown, since resources are reclaimed at process exit anyway."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"if err := m.close(); err != nil {\n\tif strings.Contains(err.Error(), \"already closed\") {\n\t\treturn nil // idempotent shutdown\n\t}\n\tlog.Warn(\"map close failed during shutdown\", \"err\", err)\n\treturn nil\n}","preventionTips":["Give each map a single owner responsible for closing it.","Make close idempotent in callers.","Close maps last in the shutdown sequence, after all datapath users stop."],"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"}