cilium/cilium · warning

failed to close bpf map: %w

Error message

failed to close bpf map: %w

What it means

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.

Source

Thrown at pkg/maps/metricsmap/metricsmap.go:95

	bpfMap, err := ebpf.LoadRegisterMap(logger, mapName)
	if err != nil {
		return nil, fmt.Errorf("failed to load bpf map: %w", err)
	}

	return &metricsMap{bpfMap: bpfMap}, nil
}

func (m *metricsMap) init() error {
	if err := m.bpfMap.OpenOrCreate(); err != nil {
		return fmt.Errorf("failed to init bpf map: %w", err)
	}

	return nil
}

func (m *metricsMap) close() error {
	if err := m.bpfMap.Close(); err != nil {
		return fmt.Errorf("failed to close bpf map: %w", err)
	}

	return nil
}

const (
	// MapName for metrics map.
	mapName = "cilium_metrics"
	// MaxEntries is the maximum number of keys that can be present in the
	// Metrics Map.
	//
	// Currently max. 2 bits of the Key.Dir member are used (unknown,
	// ingress or egress). Thus we can reduce from the theoretical max. size
	// of 2**16 (2 uint8) to 2**10 (1 uint8 + 2 bits).
	MaxEntries = 1024
	// dirIngress and dirEgress values should match with
	// METRIC_INGRESS, METRIC_EGRESS and METRIC_SERVICE
	// in bpf/lib/metrics.h

View on GitHub (pinned to ac7b90affa)

Solutions

  1. Fix ownership so exactly one lifecycle path closes the map.
  2. Check for 'file already closed' in the wrapped error and make close idempotent in the caller.
  3. Log at warning level during shutdown; the fd is reclaimed at process exit anyway.
Defensive patterns

Strategy: try-catch

Try / catch

if err := m.close(); err != nil {
	log.Warn("metrics map close failed (best-effort)", "err", err)
	return nil
}

Prevention

When it happens

Trigger: Calling metricsMap.close() when Close() errors: the map was closed elsewhere first, hive lifecycle closed it twice, or the underlying fd was already invalidated.

Common situations: Duplicated lifecycle hooks closing the metrics map in both a hive cell and CLI teardown paths; tests closing the shared map multiple times.

Related errors


AI-assisted analysis of cilium/cilium@ac7b90affa (2026-08-31). Data as JSON: /api/errors/3acc10067da24369. Report an issue: GitHub.