From 3447310fc34645bb066388034b9f93cc7efa26f3 Mon Sep 17 00:00:00 2001 From: Peter Lawrey Date: Mon, 16 Mar 2026 10:28:19 +0000 Subject: [PATCH 1/2] Clear separation of roles between waitForCloseableToClose and assertCloseablesClosed --- .../core/internal/CloseableUtils.java | 50 ++++++++----------- 1 file changed, 21 insertions(+), 29 deletions(-) diff --git a/src/main/java/net/openhft/chronicle/core/internal/CloseableUtils.java b/src/main/java/net/openhft/chronicle/core/internal/CloseableUtils.java index 403d866895..1ec8d1b6c7 100644 --- a/src/main/java/net/openhft/chronicle/core/internal/CloseableUtils.java +++ b/src/main/java/net/openhft/chronicle/core/internal/CloseableUtils.java @@ -109,7 +109,7 @@ protected void finalize() throws Throwable { throw new AssertionError("Timed out waiting for the Finalizer"); } - AbstractCloseable.waitForCloseablesToClose(1000); + waitForCloseablesToClose(1000); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new AssertionError(e); @@ -124,6 +124,7 @@ protected void finalize() throws Throwable { * * @param millis The time limit in milliseconds to wait for the closeable resources to close. * @return true if all closeable resources are closed within the time limit, false otherwise. + * @see #assertCloseablesClosed() */ @SuppressWarnings({"java:S3776", "java:S3516"}) // turned on by assert public static boolean waitForCloseablesToClose(long millis) { @@ -131,42 +132,30 @@ public static boolean waitForCloseablesToClose(long millis) { if (traceSet == null) { return true; } - if (Thread.interrupted()) + if (Thread.currentThread().isInterrupted()) System.err.println("Interrupted in waitForCloseablesToClose!"); long end = System.currentTimeMillis() + millis; + CleaningThreadLocal.cleanupNonCleaningThreads(); + BackgroundResourceReleaser.releasePendingResources(); - toWait: while (true) { - Collection traceSetCopy; synchronized (traceSet) { - traceSetCopy = new ArrayList<>(traceSet); - } - for (Closeable key : traceSetCopy) { - if (key.isClosing()) - continue; - try { - // too late to be checking thread safety. - if (key instanceof AbstractCloseable) { - ((AbstractCloseable) key).singleThreadedCheckDisabled(true); - } - if (key instanceof ReferenceCountedTracer) { - ((ReferenceCountedTracer) key).throwExceptionIfNotReleased(); - } - - } catch (IllegalStateException e) { - if (System.currentTimeMillis() > end) - throw e; + boolean allClosed = true; - BackgroundResourceReleaser.releasePendingResources(); - - CleaningThreadLocal.cleanupNonCleaningThreads(); - - Jvm.pause(1); - continue toWait; + for (ManagedCloseable key : traceSet) { + if (!key.isClosing()) { + allClosed = false; + break; + } } + if (allClosed) + return true; } - return true; + + if (System.currentTimeMillis() > end) + return false; + Jvm.pause(25); } } @@ -174,6 +163,8 @@ public static boolean waitForCloseablesToClose(long millis) { * Asserts that all closeable resources are closed. * This method checks if there are any remaining open closeable resources. * If any resources are found to be open, an AssertionError is thrown. + * + * @see #waitForCloseablesToClose(long) */ public static void assertCloseablesClosed() { final Set traceSet = CLOSEABLES.get(); @@ -301,7 +292,8 @@ public static void closeQuietly(@Nullable Object... closeables) { * * @param o the object to close */ - @SuppressWarnings({"java:S1181", "java:S3776"}) // Catching Throwable intentionally to prevent cleanup paths from throwing. + @SuppressWarnings({"java:S1181", "java:S3776"}) + // Catching Throwable intentionally to prevent cleanup paths from throwing. static void closeQuietly(@Nullable Object o) { if (o instanceof Collection) { Collection coll = (Collection) o; From be54fe8e4effce0ae8c84b70aa3e08269c22c641 Mon Sep 17 00:00:00 2001 From: Peter Lawrey Date: Mon, 16 Mar 2026 10:36:45 +0000 Subject: [PATCH 2/2] revert format change --- .../net/openhft/chronicle/core/internal/CloseableUtils.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/net/openhft/chronicle/core/internal/CloseableUtils.java b/src/main/java/net/openhft/chronicle/core/internal/CloseableUtils.java index 1ec8d1b6c7..5fe3da1a85 100644 --- a/src/main/java/net/openhft/chronicle/core/internal/CloseableUtils.java +++ b/src/main/java/net/openhft/chronicle/core/internal/CloseableUtils.java @@ -292,8 +292,7 @@ public static void closeQuietly(@Nullable Object... closeables) { * * @param o the object to close */ - @SuppressWarnings({"java:S1181", "java:S3776"}) - // Catching Throwable intentionally to prevent cleanup paths from throwing. + @SuppressWarnings({"java:S1181", "java:S3776"}) // Catching Throwable intentionally to prevent cleanup paths from throwing. static void closeQuietly(@Nullable Object o) { if (o instanceof Collection) { Collection coll = (Collection) o;