Skip to content

Commit 8b0c045

Browse files
shark-horsebrian-brazil
authored andcommitted
fixed one way to get a ConcurrentModificationException: (#495)
Using one object (namesCollectorsLock) to lock both names / collectors maps. Using a seperate object just to prevent confusion. Signed-off-by: Jason Young <[email protected]>
1 parent fbc3fc4 commit 8b0c045

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

simpleclient/src/main/java/io/prometheus/client/CollectorRegistry.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public class CollectorRegistry {
2727
*/
2828
public static final CollectorRegistry defaultRegistry = new CollectorRegistry(true);
2929

30-
30+
private final Object namesCollectorsLock = new Object();
3131
private final Map<Collector, List<String>> collectorsToNames = new HashMap<Collector, List<String>>();
3232
private final Map<String, Collector> namesToCollectors = new HashMap<String, Collector>();
3333

@@ -48,7 +48,7 @@ public CollectorRegistry(boolean autoDescribe) {
4848
*/
4949
public void register(Collector m) {
5050
List<String> names = collectorNames(m);
51-
synchronized (collectorsToNames) {
51+
synchronized (namesCollectorsLock) {
5252
for (String name : names) {
5353
if (namesToCollectors.containsKey(name)) {
5454
throw new IllegalArgumentException("Collector already registered that provides name: " + name);
@@ -65,7 +65,7 @@ public void register(Collector m) {
6565
* Unregister a Collector.
6666
*/
6767
public void unregister(Collector m) {
68-
synchronized (collectorsToNames) {
68+
synchronized (namesCollectorsLock) {
6969
List<String> names = collectorsToNames.remove(m);
7070
for (String name : names) {
7171
namesToCollectors.remove(name);
@@ -77,7 +77,7 @@ public void unregister(Collector m) {
7777
* Unregister all Collectors.
7878
*/
7979
public void clear() {
80-
synchronized (collectorsToNames) {
80+
synchronized (namesCollectorsLock) {
8181
collectorsToNames.clear();
8282
namesToCollectors.clear();
8383
}
@@ -87,7 +87,7 @@ public void clear() {
8787
* A snapshot of the current collectors.
8888
*/
8989
private Set<Collector> collectors() {
90-
synchronized (collectorsToNames) {
90+
synchronized (namesCollectorsLock) {
9191
return new HashSet<Collector>(collectorsToNames.keySet());
9292
}
9393
}
@@ -159,7 +159,7 @@ private Iterator<Collector> includedCollectorIterator(Set<String> includedNames)
159159
return collectors().iterator();
160160
} else {
161161
HashSet<Collector> collectors = new HashSet<Collector>();
162-
synchronized (namesToCollectors) {
162+
synchronized (namesCollectorsLock) {
163163
for (Map.Entry<String, Collector> entry : namesToCollectors.entrySet()) {
164164
if (includedNames.contains(entry.getKey())) {
165165
collectors.add(entry.getValue());

0 commit comments

Comments
 (0)