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..5fe3da1a85 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();