Skip to content

Commit

Permalink
Merge pull request #718 from jenkinsci/bugfix/deadlock-in-nbBuildsGauge
Browse files Browse the repository at this point in the history
Avoiding potential deadlock in counting number of builds
  • Loading branch information
Waschndolos authored Dec 6, 2024
2 parents a72036c + 6196875 commit 98e119d
Showing 1 changed file with 10 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
import io.prometheus.client.SimpleCollector;
import org.jenkinsci.plugins.prometheus.collectors.CollectorType;
import org.jenkinsci.plugins.prometheus.collectors.builds.BuildsMetricCollector;

import java.util.concurrent.locks.ReentrantReadWriteLock;

public class NbBuildsGauge extends BuildsMetricCollector<Job<?, ?>, Gauge> {

private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();

protected NbBuildsGauge(String[] labelNames, String namespace, String subsystem) {
super(labelNames, namespace, subsystem);
}
Expand All @@ -30,7 +32,12 @@ protected SimpleCollector.Builder<?, Gauge> getCollectorBuilder() {

@Override
public void calculateMetric(Job<?, ?> jenkinsObject, String[] labelValues) {
int nbBuilds = jenkinsObject.getBuildsAsMap().size();
this.collector.labels(labelValues).set(nbBuilds);
lock.readLock().lock();
try {
int nbBuilds = jenkinsObject.getBuildsAsMap().size();
this.collector.labels(labelValues).set(nbBuilds);
} finally {
lock.readLock().unlock();
}
}
}

0 comments on commit 98e119d

Please sign in to comment.