diff --git a/core/src/main/java/hudson/slaves/WorkspaceList.java b/core/src/main/java/hudson/slaves/WorkspaceList.java index 5e335f23e26c1f60c808126ecc404f8ac4e381fa..d2495705a02b3174cc09db16b724f067abb81962 100644 --- a/core/src/main/java/hudson/slaves/WorkspaceList.java +++ b/core/src/main/java/hudson/slaves/WorkspaceList.java @@ -26,10 +26,12 @@ package hudson.slaves; import hudson.FilePath; import hudson.Functions; import hudson.model.Computer; +import java.io.Closeable; import java.util.Date; import java.util.HashMap; import java.util.Map; +import java.util.concurrent.atomic.AtomicBoolean; import java.util.logging.Level; import java.util.logging.Logger; import javax.annotation.Nonnull; @@ -104,7 +106,7 @@ public final class WorkspaceList { /** * Represents a leased workspace that needs to be returned later. */ - public static abstract class Lease { + public static abstract class Lease implements /*Auto*/Closeable { public final @Nonnull FilePath path; protected Lease(@Nonnull FilePath path) { @@ -117,6 +119,14 @@ public final class WorkspaceList { */ public abstract void release(); + /** + * By default, calls {@link #release}, but should be idempotent. + * @since TODO + */ + @Override public void close() { + release(); + } + /** * Creates a dummy {@link Lease} object that does no-op in the release. */ @@ -261,9 +271,15 @@ public final class WorkspaceList { */ private Lease lease(@Nonnull FilePath p) { return new Lease(p) { + final AtomicBoolean released = new AtomicBoolean(); public void release() { _release(path); } + @Override public void close() { + if (released.compareAndSet(false, true)) { + release(); + } + } }; } diff --git a/test/src/test/java/hudson/model/BuildExecutionTest.java b/test/src/test/java/hudson/model/BuildExecutionTest.java index 8b3a534d704e5f51d6b59540e1f31b2ba4a192a3..7798633fe40bc586cfda421983ad541e038e5f10 100644 --- a/test/src/test/java/hudson/model/BuildExecutionTest.java +++ b/test/src/test/java/hudson/model/BuildExecutionTest.java @@ -51,7 +51,7 @@ public class BuildExecutionTest { try { assertEquals(ws, lease.path); } finally { - lease.release(); + lease.close(); } }