From d9ec6e0f310a53962e5668bb878a5ecaed10ebe0 Mon Sep 17 00:00:00 2001 From: Anthony Ramine Date: Thu, 13 Oct 2022 10:37:05 +0200 Subject: [PATCH] Fix Family::get_or_create The method should not overwrite an existing key after it obtains the write lock. Consider the following scenario with `Family` used by threads A and B: 1. A and B both call `family.get_or_insert(&label_set)` 2. A and B both acquire read lock and finds no key 3. A gets write lock and inserts a new gauge with value 0 4. A increments returned gauge to 1 5. B gets write lock and inserts a new gauge with value 0 6. B increments returned gauge to 1 7. A decrements gauge back to 0 8. B decrements gauge which now overflows to `u64::MAX` Signed-off-by: Anthony Ramine --- src/metrics/family.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/metrics/family.rs b/src/metrics/family.rs index fb5dbeb6..e8ca7375 100644 --- a/src/metrics/family.rs +++ b/src/metrics/family.rs @@ -221,10 +221,10 @@ impl> Family