Skip to content

Commit 7da6885

Browse files
edburnsCopilot
andauthored
Avoid leaking session.
The retry on session creation uses `future.get(timeout)` but does not cancel the in-flight `createSession` future when a timeout occurs. If attempt 1 eventually completes after attempt 2 starts, it can leave an orphaned session registered in the client (and potentially race `getLastSessionId` persistence), reintroducing flakiness and leaking resources. Capture the future and cancel it on timeout before retrying. Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
1 parent a228c57 commit 7da6885

1 file changed

Lines changed: 8 additions & 5 deletions

File tree

java/src/test/java/com/github/copilot/CopilotSessionTest.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -758,19 +758,22 @@ void testShouldGetLastSessionId() throws Exception {
758758
try (CopilotClient client = ctx.createClient()) {
759759
CopilotSession session = null;
760760
for (int attempt = 1; attempt <= 2; attempt++) {
761+
CompletableFuture<CopilotSession> createFuture = client
762+
.createSession(new SessionConfig().setOnPermissionRequest(PermissionHandler.APPROVE_ALL));
761763
try {
762-
session = client
763-
.createSession(new SessionConfig().setOnPermissionRequest(PermissionHandler.APPROVE_ALL))
764-
.get(45, TimeUnit.SECONDS);
764+
session = createFuture.get(45, TimeUnit.SECONDS);
765765
break;
766766
} catch (java.util.concurrent.TimeoutException e) {
767+
createFuture.cancel(true);
767768
if (attempt == 2) {
768769
throw e;
769770
}
770771
} catch (java.util.concurrent.ExecutionException e) {
771-
if (!(e.getCause() instanceof java.util.concurrent.TimeoutException) || attempt == 2) {
772-
throw e;
772+
if (e.getCause() instanceof java.util.concurrent.TimeoutException && attempt < 2) {
773+
createFuture.cancel(true);
774+
continue;
773775
}
776+
throw e;
774777
}
775778
}
776779
assertNotNull(session, "Session should be created");

0 commit comments

Comments
 (0)