Skip to content

Commit 49858a0

Browse files
committed
Fix concurrent gaugeLocks map access
Use putIfAbsent to ensure atomic creation of lock objects. Fixes gh-1995
1 parent ffe5348 commit 49858a0

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/writer/CodahaleMetricWriter.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -90,25 +90,25 @@ else if (name.startsWith("timer")) {
9090
}
9191
else {
9292
final double gauge = value.getValue().doubleValue();
93-
Object lock = null;
94-
if (this.gaugeLocks.containsKey(name)) {
95-
lock = this.gaugeLocks.get(name);
96-
}
97-
else {
98-
this.gaugeLocks.putIfAbsent(name, new Object());
99-
lock = this.gaugeLocks.get(name);
100-
}
101-
10293
// Ensure we synchronize to avoid another thread pre-empting this thread after
10394
// remove causing an error in CodaHale metrics
10495
// NOTE: CodaHale provides no way to do this atomically
105-
synchronized (lock) {
96+
synchronized (getGuageLock(name)) {
10697
this.registry.remove(name);
10798
this.registry.register(name, new SimpleGauge(gauge));
10899
}
109100
}
110101
}
111102

103+
private Object getGuageLock(String name) {
104+
Object lock = this.gaugeLocks.get(name);
105+
if (lock == null) {
106+
this.gaugeLocks.putIfAbsent(name, new Object());
107+
lock = this.gaugeLocks.get(name);
108+
}
109+
return lock;
110+
}
111+
112112
@Override
113113
public void reset(String metricName) {
114114
this.registry.remove(metricName);

0 commit comments

Comments
 (0)