Skip to content

Commit 93be781

Browse files
Copilotedburns
andauthored
Handle RejectedExecutionException in all async submission sites
Agent-Logs-Url: https://github.com/github/copilot-sdk-java/sessions/63b9b09f-f1f4-44d3-8e34-ad01e355cc6a Co-authored-by: edburns <75821+edburns@users.noreply.github.com>
1 parent c708fd6 commit 93be781

File tree

3 files changed

+48
-17
lines changed

3 files changed

+48
-17
lines changed

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

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import java.util.concurrent.CompletionException;
1515
import java.util.concurrent.ConcurrentHashMap;
1616
import java.util.concurrent.Executor;
17+
import java.util.concurrent.RejectedExecutionException;
1718
import java.util.concurrent.TimeUnit;
1819
import java.util.logging.Level;
1920
import java.util.logging.Logger;
@@ -152,9 +153,13 @@ private CompletableFuture<Connection> startCore() {
152153
LOG.fine("Starting Copilot client");
153154

154155
Executor exec = options.getExecutor();
155-
return exec != null
156-
? CompletableFuture.supplyAsync(this::startCoreBody, exec)
157-
: CompletableFuture.supplyAsync(this::startCoreBody);
156+
try {
157+
return exec != null
158+
? CompletableFuture.supplyAsync(this::startCoreBody, exec)
159+
: CompletableFuture.supplyAsync(this::startCoreBody);
160+
} catch (RejectedExecutionException e) {
161+
return CompletableFuture.failedFuture(e);
162+
}
158163
}
159164

160165
private Connection startCoreBody() {
@@ -244,8 +249,17 @@ public CompletableFuture<Void> stop() {
244249
LOG.log(Level.WARNING, "Error closing session " + session.getSessionId(), e);
245250
}
246251
};
247-
closeFutures.add(
248-
exec != null ? CompletableFuture.runAsync(closeTask, exec) : CompletableFuture.runAsync(closeTask));
252+
CompletableFuture<Void> future;
253+
try {
254+
future = exec != null
255+
? CompletableFuture.runAsync(closeTask, exec)
256+
: CompletableFuture.runAsync(closeTask);
257+
} catch (RejectedExecutionException e) {
258+
LOG.log(Level.WARNING, "Executor rejected session close task; closing inline", e);
259+
closeTask.run();
260+
future = CompletableFuture.completedFuture(null);
261+
}
262+
closeFutures.add(future);
249263
}
250264
sessions.clear();
251265

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

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -729,10 +729,15 @@ private void executeToolAndRespondAsync(String requestId, String toolName, Strin
729729
}
730730
}
731731
};
732-
if (executor != null) {
733-
CompletableFuture.runAsync(task, executor);
734-
} else {
735-
CompletableFuture.runAsync(task);
732+
try {
733+
if (executor != null) {
734+
CompletableFuture.runAsync(task, executor);
735+
} else {
736+
CompletableFuture.runAsync(task);
737+
}
738+
} catch (RejectedExecutionException e) {
739+
LOG.log(Level.WARNING, "Executor rejected tool task for requestId=" + requestId + "; running inline", e);
740+
task.run();
736741
}
737742
}
738743

@@ -782,10 +787,16 @@ private void executePermissionAndRespondAsync(String requestId, PermissionReques
782787
}
783788
}
784789
};
785-
if (executor != null) {
786-
CompletableFuture.runAsync(task, executor);
787-
} else {
788-
CompletableFuture.runAsync(task);
790+
try {
791+
if (executor != null) {
792+
CompletableFuture.runAsync(task, executor);
793+
} else {
794+
CompletableFuture.runAsync(task);
795+
}
796+
} catch (RejectedExecutionException e) {
797+
LOG.log(Level.WARNING, "Executor rejected permission task for requestId=" + requestId + "; running inline",
798+
e);
799+
task.run();
789800
}
790801
}
791802

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.util.Map;
1111
import java.util.concurrent.CompletableFuture;
1212
import java.util.concurrent.Executor;
13+
import java.util.concurrent.RejectedExecutionException;
1314
import java.util.logging.Level;
1415
import java.util.logging.Logger;
1516

@@ -367,10 +368,15 @@ private void handleSystemMessageTransform(JsonRpcClient rpc, String requestId, J
367368
}
368369

369370
private void runAsync(Runnable task) {
370-
if (executor != null) {
371-
CompletableFuture.runAsync(task, executor);
372-
} else {
373-
CompletableFuture.runAsync(task);
371+
try {
372+
if (executor != null) {
373+
CompletableFuture.runAsync(task, executor);
374+
} else {
375+
CompletableFuture.runAsync(task);
376+
}
377+
} catch (RejectedExecutionException e) {
378+
LOG.log(Level.WARNING, "Executor rejected handler task; running inline", e);
379+
task.run();
374380
}
375381
}
376382
}

0 commit comments

Comments
 (0)