Skip to content

Commit 6a4e4ab

Browse files
authored
Don't keep an old session to avoid a stable objects and a memory leak
Closes #43761 Signed-off-by: Alexander Schwartz <[email protected]>
1 parent 15fe032 commit 6a4e4ab

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

services/src/main/java/org/keycloak/timer/basic/BasicTimerProvider.java

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,7 @@ public BasicTimerProvider(KeycloakSession session, Timer timer, int transactionT
4747

4848
@Override
4949
public void schedule(final Runnable runnable, final long intervalMillis, String taskName) {
50-
TimerTask task = new TimerTask() {
51-
@Override
52-
public void run() {
53-
runnable.run();
54-
}
55-
};
50+
TimerTask task = new BasicTimerTask(runnable);
5651

5752
TimerTaskContextImpl taskContext = new TimerTaskContextImpl(runnable, task, intervalMillis);
5853
TimerTaskContextImpl existingTask = factory.putTask(taskName, taskContext);
@@ -87,4 +82,20 @@ public void close() {
8782
// do nothing
8883
}
8984

85+
/**
86+
* Using a private static class avoids keeping a reference to {@link BasicTimerProvider} which then fails to be garbage collected,
87+
* including its reference to {@link KeycloakSession}.
88+
*/
89+
private static class BasicTimerTask extends TimerTask {
90+
private final Runnable runnable;
91+
92+
public BasicTimerTask(Runnable runnable) {
93+
this.runnable = runnable;
94+
}
95+
96+
@Override
97+
public void run() {
98+
runnable.run();
99+
}
100+
}
90101
}

0 commit comments

Comments
 (0)