From 9bfbfc8381f13fc4fd85ef30e21ab223e8fb8531 Mon Sep 17 00:00:00 2001 From: Huw Jones Date: Tue, 18 Jun 2024 13:55:32 +0100 Subject: [PATCH] Apply fix for concurrent dict iteration See https://github.com/claws/aioprometheus/issues/85 --- debian/changelog | 8 ++++ debian/patches/issue-85.patch | 70 +++++++++++++++++++++++++++++++++++ debian/patches/series | 1 + 3 files changed, 79 insertions(+) create mode 100644 debian/patches/issue-85.patch diff --git a/debian/changelog b/debian/changelog index 4b2268d..50d3a85 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,3 +1,11 @@ +python-aioprometheus (22.5.0-0+pexip24u7) pexip; urgency=medium + + * Apply fix for issue #85, see + https://github.com/claws/aioprometheus/issues/85 + * d/p/revert-to-normal-json: reduce size of patch + + -- Huw Jones Tue, 18 Jun 2024 13:56:53 +0100 + python-aioprometheus (22.5.0-0+pexip24u6) pexip-bookworm; urgency=medium * New versioning scheme diff --git a/debian/patches/issue-85.patch b/debian/patches/issue-85.patch new file mode 100644 index 0000000..5033954 --- /dev/null +++ b/debian/patches/issue-85.patch @@ -0,0 +1,70 @@ +From 16ea4aff8e560869f4c5b4cc16e495f30fdd8f67 Mon Sep 17 00:00:00 2001 +From: Huw Jones +Date: Tue, 18 Jun 2024 13:52:42 +0100 +Subject: [PATCH] collector/BaseCollector/get_all: prevent iteration over + modified dict + +Fixes https://github.com/claws/aioprometheus/issues/85 +--- + src/aioprometheus/collectors.py | 2 +- + tests/test_metrics.py | 26 ++++++++++++++++++++++++++ + 2 files changed, 27 insertions(+), 1 deletion(-) + +diff --git a/src/aioprometheus/collectors.py b/src/aioprometheus/collectors.py +index fb8c3d4..d3d24d3 100644 +--- a/src/aioprometheus/collectors.py ++++ b/src/aioprometheus/collectors.py +@@ -167,7 +167,7 @@ class Collector: + itself. + """ + result = [] +- for k in self.values: ++ for k in list(self.values): + # Check if is a single value dict (custom empty key) + if not k or k == MetricDict.EMPTY_KEY: + pass +diff --git a/tests/test_metrics.py b/tests/test_metrics.py +index 8d85a97..b9a216e 100644 +--- a/tests/test_metrics.py ++++ b/tests/test_metrics.py +@@ -1,4 +1,5 @@ + import unittest ++from threading import Thread + + from aioprometheus.collectors import ( + REGISTRY, +@@ -166,6 +167,31 @@ class TestCollectorBase(unittest.TestCase): + sorted_result = sorted(c.get_all(), key=country_fetcher) + self.assertEqual(sorted_data, sorted_result) + ++ def test_get_all_change_iter(self) -> None: ++ """ ++ test iterating over get_all when the underlying dict changes ++ ++ For https://github.com/claws/aioprometheus/issues/85 ++ """ ++ c = Collector(**self.default_data) ++ ++ # Create a large collection of values to increase the chance of a race ++ for i in range(100000): ++ c.set_value({f"b-{i}": "a"}, i) ++ ++ # Now create a thread that will add a huge amount more values ++ def set_more_values(): ++ for j in range(10000): ++ c.set_value({f"a-{j}": "a"}, j) ++ ++ t = Thread(daemon=True, target=set_more_values) ++ t.start() ++ ++ # Hope that due to the large amount of labels involved, we will smack ++ # head-first into the race of iterating over the dict whilst adding more ++ # unique labels to it. ++ list(c.get_all()) ++ + + class TestCounterMetric(unittest.TestCase): + def setUp(self): +-- +2.44.0 + diff --git a/debian/patches/series b/debian/patches/series index b72fed3..51f0ade 100644 --- a/debian/patches/series +++ b/debian/patches/series @@ -1,2 +1,3 @@ patch-out-quantile-python.diff revert-to-normal-json.diff +issue-85.patch