Skip to content

Commit

Permalink
[JENKINS-22558] Avoid deadlocks by using RentrantLock#tryLock() inste…
Browse files Browse the repository at this point in the history
…ad of synchronized.
  • Loading branch information
recampbell committed May 8, 2014
1 parent 68631a4 commit 315c65e
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.util.TimeUnit2;

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

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 */
public final int idleTerminationMinutes;

private ReentrantLock checkLock = new ReentrantLock(false);

@DataBoundConstructor
public EC2RetentionStrategy(String idleTerminationMinutes) {
Expand All @@ -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) {
Expand Down

0 comments on commit 315c65e

Please sign in to comment.