]> source.dussan.org Git - jgit.git/commitdiff
Unregister ShutdownHook when GC#PidLock is closed 12/204212/3
authorMatthias Sohn <matthias.sohn@sap.com>
Fri, 8 Sep 2023 00:32:57 +0000 (02:32 +0200)
committerMatthias Sohn <matthias.sohn@sap.com>
Tue, 12 Sep 2023 20:43:15 +0000 (22:43 +0200)
Otherwise the JVM will accumulate the ShutdownHook objects of all GCs
run while the JVM is up.

Change-Id: Iadc723a939238a3a75b4ba47f898918eb4554ea3

org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/GC.java

index df18d059d25b5609ed21722c8bb19c9bc93b3e39..fd9e55052640e1b53dfcc737bf61bd8ed3fec33a 100644 (file)
@@ -1799,6 +1799,8 @@ public class GC {
 
                private FileChannel channel;
 
+               private Thread cleanupHook;
+
                PidLock() {
                        pidFile = repo.getDirectory().toPath().resolve(GC_PID);
                }
@@ -1827,9 +1829,9 @@ public class GC {
                                }
                                channel.write(ByteBuffer
                                                .wrap(getProcDesc().getBytes(StandardCharsets.UTF_8)));
-                               Thread cleanupHook = new Thread(() -> close());
                                try {
-                                       Runtime.getRuntime().addShutdownHook(cleanupHook);
+                                       Runtime.getRuntime().addShutdownHook(
+                                                       cleanupHook = new Thread(() -> close()));
                                } catch (IllegalStateException e) {
                                        // ignore - the VM is already shutting down
                                }
@@ -1901,6 +1903,13 @@ public class GC {
                public void close() {
                        boolean wasLocked = false;
                        try {
+                               if (cleanupHook != null) {
+                                       try {
+                                               Runtime.getRuntime().removeShutdownHook(cleanupHook);
+                                       } catch (IllegalStateException e) {
+                                               // ignore - the VM is already shutting down
+                                       }
+                               }
                                if (lock != null && lock.isValid()) {
                                        lock.release();
                                        wasLocked = true;