Skip to content

Commit e975d6c

Browse files
Copilotedburns
andcommitted
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 e624857 commit e975d6c

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
@@ -730,10 +730,15 @@ private void executeToolAndRespondAsync(String requestId, String toolName, Strin
730730
}
731731
}
732732
};
733-
if (executor != null) {
734-
CompletableFuture.runAsync(task, executor);
735-
} else {
736-
CompletableFuture.runAsync(task);
733+
try {
734+
if (executor != null) {
735+
CompletableFuture.runAsync(task, executor);
736+
} else {
737+
CompletableFuture.runAsync(task);
738+
}
739+
} catch (RejectedExecutionException e) {
740+
LOG.log(Level.WARNING, "Executor rejected tool task for requestId=" + requestId + "; running inline", e);
741+
task.run();
737742
}
738743
}
739744

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

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)