2020import java .util .Objects ;
2121import java .util .concurrent .CompletableFuture ;
2222import java .util .concurrent .CompletionException ;
23+ import java .util .concurrent .ExecutorService ;
24+ import java .util .concurrent .Executors ;
2325
2426import tools .jackson .databind .DeserializationFeature ;
2527import tools .jackson .databind .ObjectMapper ;
@@ -72,6 +74,11 @@ public class CodexAppServerClient implements AutoCloseable {
7274 JsonMapper .builder ().disable (DeserializationFeature .FAIL_ON_UNKNOWN_PROPERTIES ).build ();
7375 private final ThreadMappingCache threadBySession ;
7476 private final Object httpClientLock = new Object ();
77+ private final ExecutorService clientExecutor = Executors .newCachedThreadPool (r -> {
78+ Thread thread = new Thread (r , "codex-app-server-client" );
79+ thread .setDaemon (true );
80+ return thread ;
81+ });
7582
7683 private volatile HttpClient httpClient ;
7784 private volatile boolean closed ;
@@ -145,16 +152,15 @@ public CodexAppServerConfig getConfig() {
145152 }
146153
147154 /**
148- * Closes the client. New turns are rejected afterwards; the shared
149- * {@link HttpClient} is shut down once any in-flight turn completes.
155+ * Closes the client. New turns are rejected afterwards; the executor
156+ * backing the shared {@link HttpClient} is shut down gracefully. The
157+ * executor is owned by this client so closing works on every JDK line
158+ * ({@code HttpClient.close()} only exists since JDK 21).
150159 */
151160 @ Override
152161 public void close () {
153162 closed = true ;
154- HttpClient client = httpClient ;
155- if (Objects .nonNull (client )) {
156- client .close ();
157- }
163+ clientExecutor .shutdown ();
158164 }
159165
160166 private HttpClient httpClient () {
@@ -164,6 +170,7 @@ private HttpClient httpClient() {
164170 if (Objects .isNull (httpClient )) {
165171 httpClient = HttpClient .newBuilder ()
166172 .connectTimeout (Duration .ofMillis (config .getConnectTimeoutMillis ()))
173+ .executor (clientExecutor )
167174 .build ();
168175 }
169176 client = httpClient ;
0 commit comments