Skip to content

Commit 9e102bc

Browse files
edburnsCopilot
andcommitted
Fix memory leak from cancelled timeout tasks in CopilotSession
Replace Executors.newSingleThreadScheduledExecutor with an explicit ScheduledThreadPoolExecutor so we can enable removeOnCancelPolicy(true). Without this, each call to sendAndWait() that completes normally cancels its timeout task, but the cancelled task remains in the scheduler's work queue, leaking memory over the lifetime of the session. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Signed-off-by: Ed Burns <edburns@microsoft.com>
1 parent a36d145 commit 9e102bc

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

src/main/java/com/github/copilot/sdk/CopilotSession.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import java.util.concurrent.ConcurrentHashMap;
1616
import java.util.concurrent.Executors;
1717
import java.util.concurrent.ScheduledExecutorService;
18+
import java.util.concurrent.ScheduledThreadPoolExecutor;
1819
import java.util.concurrent.TimeUnit;
1920
import java.util.concurrent.TimeoutException;
2021
import java.util.concurrent.atomic.AtomicReference;
@@ -159,11 +160,13 @@ public final class CopilotSession implements AutoCloseable {
159160
this.sessionId = sessionId;
160161
this.rpc = rpc;
161162
this.workspacePath = workspacePath;
162-
this.timeoutScheduler = Executors.newSingleThreadScheduledExecutor(r -> {
163+
var executor = new ScheduledThreadPoolExecutor(1, r -> {
163164
var t = new Thread(r, "sendAndWait-timeout");
164165
t.setDaemon(true);
165166
return t;
166167
});
168+
executor.setRemoveOnCancelPolicy(true);
169+
this.timeoutScheduler = executor;
167170
}
168171

169172
/**

0 commit comments

Comments
 (0)