Don't keep an old session to avoid a stable objects and a memory leak

Closes #43761

Signed-off-by: Alexander Schwartz <alexander.schwartz@ibm.com>
This commit is contained in:
Alexander Schwartz 2025-10-29 21:36:41 +01:00 committed by GitHub
parent 15fe032e8d
commit 6a4e4abf30
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -47,12 +47,7 @@ public class BasicTimerProvider implements TimerProvider {
@Override
public void schedule(final Runnable runnable, final long intervalMillis, String taskName) {
TimerTask task = new TimerTask() {
@Override
public void run() {
runnable.run();
}
};
TimerTask task = new BasicTimerTask(runnable);
TimerTaskContextImpl taskContext = new TimerTaskContextImpl(runnable, task, intervalMillis);
TimerTaskContextImpl existingTask = factory.putTask(taskName, taskContext);
@ -87,4 +82,20 @@ public class BasicTimerProvider implements TimerProvider {
// do nothing
}
/**
* Using a private static class avoids keeping a reference to {@link BasicTimerProvider} which then fails to be garbage collected,
* including its reference to {@link KeycloakSession}.
*/
private static class BasicTimerTask extends TimerTask {
private final Runnable runnable;
public BasicTimerTask(Runnable runnable) {
this.runnable = runnable;
}
@Override
public void run() {
runnable.run();
}
}
}