Skip to content

Commit a32d413

Browse files
authored
Merge pull request #104 from mxinden/release-v0.18-to-master
Merge `release-v0.18` back into `master`
2 parents de81e7b + d346956 commit a32d413

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2222
[PR 85]: https://github.com/prometheus/client_rust/pull/85
2323
[PR 96]: https://github.com/prometheus/client_rust/pull/96
2424

25+
## [0.18.1]
26+
27+
### Fixed
28+
29+
- Fix race condition in `Family::get_or_create`. See [PR 102].
30+
31+
[PR 102]: https://github.com/prometheus/client_rust/pull/102
32+
2533
## [0.18.0]
2634

2735
### Changed

src/metrics/family.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//! See [`Family`] for details.
44
55
use super::{MetricType, TypedMetric};
6-
use parking_lot::{MappedRwLockReadGuard, RwLock, RwLockReadGuard};
6+
use parking_lot::{MappedRwLockReadGuard, RwLock, RwLockReadGuard, RwLockWriteGuard};
77
use std::collections::HashMap;
88
use std::sync::Arc;
99

@@ -223,11 +223,14 @@ impl<S: Clone + std::hash::Hash + Eq, M, C: MetricConstructor<M>> Family<S, M, C
223223
}
224224

225225
let mut write_guard = self.metrics.write();
226-
write_guard.insert(label_set.clone(), self.constructor.new_metric());
227226

228-
drop(write_guard);
227+
write_guard
228+
.entry(label_set.clone())
229+
.or_insert_with(|| self.constructor.new_metric());
229230

230-
RwLockReadGuard::map(self.metrics.read(), |metrics| {
231+
let read_guard = RwLockWriteGuard::downgrade(write_guard);
232+
233+
RwLockReadGuard::map(read_guard, |metrics| {
231234
metrics
232235
.get(label_set)
233236
.expect("Metric to exist after creating it.")

0 commit comments

Comments
 (0)