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 #41164, although it might not
fix the issue
  • Loading branch information
geoand committed Jun 13, 2024
1 parent 875cdf9 commit fc4ff8a
Showing 1 changed file with 34 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,30 @@ 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 QuarkusTestExtensionState state;

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

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

0 comments on commit fc4ff8a

Please sign in to comment.