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 12:01:55 +01:00 committed by GitHub
parent 5693899246
commit 4f10c10ffd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -51,12 +51,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, Time.currentTimeMillis(), intervalMillis);
TimerTaskContextImpl existingTask = factory.putTask(taskName, taskContext);
@ -95,4 +90,21 @@ public class BasicTimerProvider implements TimerProvider {
public Map<String, TimerTaskContext> getTasks() {
return Collections.unmodifiableMap(new HashMap<>(factory.getTasks()));
}
/**
* 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();
}
}
}