Skip to content

Commit

Permalink
Be more proactive about memory leaks in QuarkusTestExtensionState
Browse files Browse the repository at this point in the history
This change is done as a response to quarkusio#41164, although it might not
fix the issue
  • Loading branch information
geoand committed Jun 12, 2024
1 parent 4e2000c commit eee00c6
Showing 1 changed file with 32 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,15 @@ public class QuarkusTestExtensionState implements ExtensionContext.Store.Closeab

private final AtomicBoolean closed = new AtomicBoolean();

protected final Closeable testResourceManager;
protected final Closeable resource;
private final Thread shutdownHook;
protected Closeable testResourceManager;
protected Closeable resource;
private Thread shutdownHook;
private Throwable testErrorCause;

public QuarkusTestExtensionState(Closeable testResourceManager, Closeable resource) {
this.testResourceManager = testResourceManager;
this.resource = resource;
this.shutdownHook = new Thread(new Runnable() {
@Override
public void run() {
try {
QuarkusTestExtensionState.this.close();
} catch (IOException ignored) {
}
}
}, "Quarkus Test Cleanup Shutdown task");
this.shutdownHook = new ShutdownThread(this);
Runtime.getRuntime().addShutdownHook(shutdownHook);
}

Expand All @@ -45,6 +37,10 @@ public void close() throws IOException {
Runtime.getRuntime().removeShutdownHook(shutdownHook);
} catch (Throwable t) {
//won't work if we are already shutting down
} finally {
shutdownHook = null;
testResourceManager = null;
resource = null;
}
}
}
Expand All @@ -69,4 +65,28 @@ protected void setTestFailed(Throwable failure) {
protected void doClose() throws IOException {

}

private static final class ShutdownThread extends Thread {

public ShutdownThread(QuarkusTestExtensionState state) {
super(new ShutdownHook(state), "Quarkus Test Cleanup Shutdown Thread");
}
}

private static final class ShutdownHook implements Runnable {

private final QuarkusTestExtensionState state;

private ShutdownHook(QuarkusTestExtensionState state) {
this.state = state;
}

@Override
public void run() {
try {
state.close();
} catch (IOException ignored) {
}
}
}
}

0 comments on commit eee00c6

Please sign in to comment.