Skip to content

Commit 0014e97

Browse files
authored
Use re-entrant lock. (#1014)
* Use re-entrant lock. --------- Signed-off-by: Ben Timby <[email protected]>
1 parent 7c45f84 commit 0014e97

File tree

4 files changed

+18
-10
lines changed

4 files changed

+18
-10
lines changed

prometheus_client/metrics.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import os
2-
from threading import Lock
2+
from threading import RLock
33
import time
44
import types
55
from typing import (
@@ -144,7 +144,7 @@ def __init__(self: T,
144144

145145
if self._is_parent():
146146
# Prepare the fields needed for child metrics.
147-
self._lock = Lock()
147+
self._lock = RLock()
148148
self._metrics: Dict[Sequence[str], T] = {}
149149

150150
if self._is_observable():
@@ -697,7 +697,7 @@ class Info(MetricWrapperBase):
697697

698698
def _metric_init(self):
699699
self._labelname_set = set(self._labelnames)
700-
self._lock = Lock()
700+
self._lock = RLock()
701701
self._value = {}
702702

703703
def info(self, val: Dict[str, str]) -> None:
@@ -759,7 +759,7 @@ def __init__(self,
759759

760760
def _metric_init(self) -> None:
761761
self._value = 0
762-
self._lock = Lock()
762+
self._lock = RLock()
763763

764764
def state(self, state: str) -> None:
765765
"""Set enum metric state."""

prometheus_client/registry.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from abc import ABC, abstractmethod
22
import copy
3-
from threading import Lock
3+
from threading import RLock
44
from typing import Dict, Iterable, List, Optional
55

66
from .metrics_core import Metric
@@ -30,7 +30,7 @@ def __init__(self, auto_describe: bool = False, target_info: Optional[Dict[str,
3030
self._collector_to_names: Dict[Collector, List[str]] = {}
3131
self._names_to_collectors: Dict[str, Collector] = {}
3232
self._auto_describe = auto_describe
33-
self._lock = Lock()
33+
self._lock = RLock()
3434
self._target_info: Optional[Dict[str, str]] = {}
3535
self.set_target_info(target_info)
3636

prometheus_client/values.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import os
2-
from threading import Lock
2+
from threading import RLock
33
import warnings
44

55
from .mmap_dict import mmap_key, MmapedDict
@@ -13,7 +13,7 @@ class MutexValue:
1313
def __init__(self, typ, metric_name, name, labelnames, labelvalues, help_text, **kwargs):
1414
self._value = 0.0
1515
self._exemplar = None
16-
self._lock = Lock()
16+
self._lock = RLock()
1717

1818
def inc(self, amount):
1919
with self._lock:
@@ -50,7 +50,7 @@ def MultiProcessValue(process_identifier=os.getpid):
5050
# Use a single global lock when in multi-processing mode
5151
# as we presume this means there is no threading going on.
5252
# This avoids the need to also have mutexes in __MmapDict.
53-
lock = Lock()
53+
lock = RLock()
5454

5555
class MmapedValue:
5656
"""A float protected by a mutex backed by a per-process mmaped file."""

tests/test_core.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@
1616
from prometheus_client.metrics import _get_use_created
1717

1818

19+
def is_locked(lock):
20+
"Tries to obtain a lock, returns True on success, False on failure."
21+
locked = lock.acquire(blocking=False)
22+
if locked:
23+
lock.release()
24+
return not locked
25+
26+
1927
def assert_not_observable(fn, *args, **kwargs):
2028
"""
2129
Assert that a function call falls with a ValueError exception containing
@@ -963,7 +971,7 @@ def test_restricted_registry_does_not_yield_while_locked(self):
963971
m = Metric('target', 'Target metadata', 'info')
964972
m.samples = [Sample('target_info', {'foo': 'bar'}, 1)]
965973
for _ in registry.restricted_registry(['target_info', 's_sum']).collect():
966-
self.assertFalse(registry._lock.locked())
974+
self.assertFalse(is_locked(registry._lock))
967975

968976

969977
if __name__ == '__main__':

0 commit comments

Comments
 (0)