From 8494288c425e470d12b5817a45a752e9aa8d78bc Mon Sep 17 00:00:00 2001 From: Anthony Ramine Date: Thu, 13 Oct 2022 10:37:05 +0200 Subject: [PATCH 1/2] metrics/family: Fix race condition in `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 --- CHANGELOG.md | 8 ++++++++ Cargo.toml | 2 +- src/metrics/family.rs | 11 +++++++---- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1822983..85ce258 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,14 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [0.18.1] - unreleased + +### Fixed + +- Fix race condition in `Family::get_or_create`. See [PR 102]. + +[PR 102]: https://github.com/prometheus/client_rust/pull/102 + ## [0.18.0] ### Changed diff --git a/Cargo.toml b/Cargo.toml index a114a05..41ba9dd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "prometheus-client" -version = "0.18.0" +version = "0.18.1" authors = ["Max Inden "] edition = "2021" description = "Open Metrics client library allowing users to natively instrument applications." diff --git a/src/metrics/family.rs b/src/metrics/family.rs index fb5dbeb..63d13f3 100644 --- a/src/metrics/family.rs +++ b/src/metrics/family.rs @@ -3,7 +3,7 @@ //! See [`Family`] for details. use super::{MetricType, TypedMetric}; -use parking_lot::{MappedRwLockReadGuard, RwLock, RwLockReadGuard}; +use parking_lot::{MappedRwLockReadGuard, RwLock, RwLockReadGuard, RwLockWriteGuard}; use std::collections::HashMap; use std::sync::Arc; @@ -222,11 +222,14 @@ impl> Family Date: Fri, 14 Oct 2022 10:25:32 +0100 Subject: [PATCH 2/2] CHANGELOG: Prepare v0.18.1 Signed-off-by: Max Inden --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 85ce258..a17c710 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). -## [0.18.1] - unreleased +## [0.18.1] ### Fixed