Skip to content
Permalink
Browse files
Merge pull request #89 from recampbell/JENKINS-22558
[JENKINS-22558] Avoid deadlocks by using RentrantLock#tryLock() instead of synchronized.
  • Loading branch information
francisu committed May 22, 2014
2 parents 68631a4 + 315c65e commit fb4f3fa27f63734e007f434fae8f051eeee16f1d
Showing with 15 additions and 1 deletion.
  1. +15 −1 src/main/java/hudson/plugins/ec2/EC2RetentionStrategy.java
@@ -27,6 +27,7 @@
import hudson.slaves.RetentionStrategy;
import hudson.util.TimeUnit2;

import java.util.concurrent.locks.ReentrantLock;
import java.util.logging.Logger;

import org.kohsuke.stapler.DataBoundConstructor;
@@ -41,6 +42,7 @@
A value of zero indicates that the instance should never be automatically terminated */
public final int idleTerminationMinutes;

private ReentrantLock checkLock = new ReentrantLock(false);

@DataBoundConstructor
public EC2RetentionStrategy(String idleTerminationMinutes) {
@@ -59,7 +61,19 @@ public EC2RetentionStrategy(String idleTerminationMinutes) {
}

@Override
public synchronized long check(EC2Computer c) {
public long check(EC2Computer c) {
if (! checkLock.tryLock()) {
return 1;
} else {
try {
return _check(c);
} finally {
checkLock.unlock();
}
}
}

private long _check(EC2Computer c) {

/* If we've been told never to terminate, then we're done. */
if (idleTerminationMinutes == 0) {

0 comments on commit fb4f3fa

Please sign in to comment.