From c15656be32b8b056407fec32b10c00eb7b58e52a Mon Sep 17 00:00:00 2001 From: Muhamed Fazal PS Date: Wed, 8 Jul 2026 18:21:15 +0530 Subject: [PATCH] fix: always initialize _lock in Metric.__init__ When a metric has no labels (neither parent nor observable), _lock was never created, causing AttributeError when clear() or other methods access it. Fix: initialize self._lock unconditionally before the _is_parent() check. Fixes #1140. --- prometheus_client/metrics.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/prometheus_client/metrics.py b/prometheus_client/metrics.py index 4c79c583..ed586967 100644 --- a/prometheus_client/metrics.py +++ b/prometheus_client/metrics.py @@ -122,9 +122,10 @@ def __init__(self: T, _validate_metric_name(self._name) + self._lock = Lock() + if self._is_parent(): # Prepare the fields needed for child metrics. - self._lock = Lock() self._metrics: Dict[Sequence[str], T] = {} if self._is_observable():