Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -124,56 +124,47 @@ 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) {
Comment thread
peter-lawrey marked this conversation as resolved.
final Set<ManagedCloseable> traceSet = CLOSEABLES.get();
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<Closeable> 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);
}
}

/**
* 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<ManagedCloseable> traceSet = CLOSEABLES.get();
Expand Down