Skip to content

Commit

Permalink
Merge pull request #89 from recampbell/JENKINS-22558
Browse files Browse the repository at this point in the history
[JENKINS-22558] Avoid deadlocks by using RentrantLock#tryLock() instead of synchronized.
  • Loading branch information
francisu committed May 22, 2014
2 parents 68631a4 + 315c65e commit fb4f3fa
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/main/java/hudson/plugins/ec2/EC2RetentionStrategy.java
Expand Up @@ -27,6 +27,7 @@
import hudson.slaves.RetentionStrategy; import hudson.slaves.RetentionStrategy;
import hudson.util.TimeUnit2; import hudson.util.TimeUnit2;


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


import org.kohsuke.stapler.DataBoundConstructor; import org.kohsuke.stapler.DataBoundConstructor;
Expand All @@ -41,6 +42,7 @@ public class EC2RetentionStrategy extends RetentionStrategy<EC2Computer> {
A value of zero indicates that the instance should never be automatically terminated */ A value of zero indicates that the instance should never be automatically terminated */
public final int idleTerminationMinutes; public final int idleTerminationMinutes;


private ReentrantLock checkLock = new ReentrantLock(false);


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


@Override @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 we've been told never to terminate, then we're done. */
if (idleTerminationMinutes == 0) { if (idleTerminationMinutes == 0) {
Expand Down

0 comments on commit fb4f3fa

Please sign in to comment.