diff --git a/src/adapter/outbound/httpclient/src/main/java/dev/caskeleton/adapter/outbound/httpclient/profile/ClientRuntimeRegistry.java b/src/adapter/outbound/httpclient/src/main/java/dev/caskeleton/adapter/outbound/httpclient/profile/ClientRuntimeRegistry.java index 8078fc62..6b566576 100644 --- a/src/adapter/outbound/httpclient/src/main/java/dev/caskeleton/adapter/outbound/httpclient/profile/ClientRuntimeRegistry.java +++ b/src/adapter/outbound/httpclient/src/main/java/dev/caskeleton/adapter/outbound/httpclient/profile/ClientRuntimeRegistry.java @@ -169,6 +169,11 @@ public final class ClientRuntimeRegistry implements AutoCloseable { * the executor reports termination from inside the worker before that thread exits, so under load * the thread could still be listed when close returned. Joining the thread is what makes "no * thread left" true when this returns. + * + *
An interrupt does not cut the wait short; the flag is restored once the wait is over. A
+ * closing thread that was already interrupted used to give up at the first wait and return
+ * normally with the drain thread still running — the one failure path that said nothing (review
+ * 2026-11-06 RR7-02-01).
*/
private void stopDrainThread() {
ScheduledExecutorService scheduler = drainScheduler.getAndSet(null);
@@ -177,23 +182,38 @@ public final class ClientRuntimeRegistry implements AutoCloseable {
}
scheduler.shutdownNow();
long deadline = System.nanoTime() + TimeUnit.MILLISECONDS.toNanos(SHUTDOWN_AWAIT_MILLIS);
+ boolean interrupted = false;
try {
- if (!scheduler.awaitTermination(SHUTDOWN_AWAIT_MILLIS, TimeUnit.MILLISECONDS)) {
- throw new IllegalStateException("http client drain scheduler did not terminate");
- }
- for (Thread thread : drainThreads) {
- long remaining = TimeUnit.NANOSECONDS.toMillis(deadline - System.nanoTime());
- thread.join(Math.max(1L, remaining));
- if (thread.isAlive()) {
- throw new IllegalStateException("http client drain thread did not exit");
+ while (true) {
+ try {
+ awaitDrainThreadExit(scheduler, deadline);
+ return;
+ } catch (InterruptedException interruptedWait) {
+ interrupted = true;
}
}
- drainThreads.clear();
- } catch (InterruptedException interrupted) {
- Thread.currentThread().interrupt();
+ } finally {
+ if (interrupted) {
+ Thread.currentThread().interrupt();
+ }
}
}
+ private void awaitDrainThreadExit(ScheduledExecutorService scheduler, long deadline)
+ throws InterruptedException {
+ if (!scheduler.awaitTermination(deadline - System.nanoTime(), TimeUnit.NANOSECONDS)) {
+ throw new IllegalStateException("http client drain scheduler did not terminate");
+ }
+ for (Thread thread : drainThreads) {
+ long remaining = TimeUnit.NANOSECONDS.toMillis(deadline - System.nanoTime());
+ thread.join(Math.max(1L, remaining));
+ if (thread.isAlive()) {
+ throw new IllegalStateException("http client drain thread did not exit");
+ }
+ }
+ drainThreads.clear();
+ }
+
private AtomicReference