From 642f160236817ff1392ac96085cd5a3f3a96bc1f Mon Sep 17 00:00:00 2001 From: Matthias Sohn Date: Fri, 8 Sep 2023 22:57:05 +0200 Subject: Use ShutdownHook to gracefully handle JVM shutdown in all classes which already registered their own shutdown hook - CloneCommand - GC#PidLock - FS#FileStoreAttributes - LocalDiskRepositoryTestCase#Cleanup Change-Id: I3efc1f83f3cbbf43eeeaaedcd2bee1ef31971a72 --- .../src/org/eclipse/jgit/api/CloneCommand.java | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) (limited to 'org.eclipse.jgit/src/org/eclipse/jgit/api') diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/api/CloneCommand.java b/org.eclipse.jgit/src/org/eclipse/jgit/api/CloneCommand.java index 107b00e274..3e034f1a6a 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/api/CloneCommand.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/api/CloneCommand.java @@ -29,6 +29,7 @@ import org.eclipse.jgit.dircache.DirCacheCheckout; import org.eclipse.jgit.errors.IncorrectObjectTypeException; import org.eclipse.jgit.errors.MissingObjectException; import org.eclipse.jgit.internal.JGitText; +import org.eclipse.jgit.internal.util.ShutdownHook; import org.eclipse.jgit.lib.AnyObjectId; import org.eclipse.jgit.lib.BranchConfig.BranchRebaseMode; import org.eclipse.jgit.lib.ConfigConstants; @@ -100,6 +101,8 @@ public class CloneCommand extends TransportCommand { private List shallowExcludes = new ArrayList<>(); + private ShutdownHook.Listener shutdownListener = this::cleanup; + private enum FETCH_TYPE { MULTIPLE_BRANCHES, ALL_BRANCHES, MIRROR } @@ -181,12 +184,7 @@ public class CloneCommand extends TransportCommand { @SuppressWarnings("resource") // Closed by caller Repository repository = init(); FetchResult fetchResult = null; - Thread cleanupHook = new Thread(() -> cleanup()); - try { - Runtime.getRuntime().addShutdownHook(cleanupHook); - } catch (IllegalStateException e) { - // ignore - the VM is already shutting down - } + ShutdownHook.INSTANCE.register(shutdownListener); try { fetchResult = fetch(repository, u); } catch (IOException ioe) { @@ -210,11 +208,7 @@ public class CloneCommand extends TransportCommand { cleanup(); throw e; } finally { - try { - Runtime.getRuntime().removeShutdownHook(cleanupHook); - } catch (IllegalStateException e) { - // ignore - the VM is already shutting down - } + ShutdownHook.INSTANCE.unregister(shutdownListener); } try { checkout(repository, fetchResult); -- cgit v1.2.3