From f94be665f12d85304db056c6cb8934870de3e45a Mon Sep 17 00:00:00 2001 From: Matthias Sohn Date: Fri, 8 Sep 2023 02:32:57 +0200 Subject: [PATCH] Unregister ShutdownHook when GC#PidLock is closed Otherwise the JVM will accumulate the ShutdownHook objects of all GCs run while the JVM is up. Change-Id: Iadc723a939238a3a75b4ba47f898918eb4554ea3 --- .../org/eclipse/jgit/internal/storage/file/GC.java | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/GC.java b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/GC.java index df18d059d2..fd9e550526 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/GC.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/GC.java @@ -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; -- 2.39.5