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 holder(ClientProfileName name) { AtomicReference holder = runtimes.get(name); if (holder == null) { diff --git a/src/adapter/outbound/httpclient/src/test/java/dev/caskeleton/adapter/outbound/httpclient/profile/ClientRuntimeRegistryTest.java b/src/adapter/outbound/httpclient/src/test/java/dev/caskeleton/adapter/outbound/httpclient/profile/ClientRuntimeRegistryTest.java index 8b3a2501..6c5c29d6 100644 --- a/src/adapter/outbound/httpclient/src/test/java/dev/caskeleton/adapter/outbound/httpclient/profile/ClientRuntimeRegistryTest.java +++ b/src/adapter/outbound/httpclient/src/test/java/dev/caskeleton/adapter/outbound/httpclient/profile/ClientRuntimeRegistryTest.java @@ -7,7 +7,10 @@ import dev.caskeleton.adapter.outbound.httpclient.testkit.ClientProfiles; import java.time.Duration; import java.util.Map; import java.util.Set; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; +import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; class ClientRuntimeRegistryTest { @@ -83,6 +86,56 @@ class ClientRuntimeRegistryTest { assertThat(liveDrainThreads()).isEmpty(); } + @Test + @DisplayName("interrupt된 스레드에서 닫아도 drain 스레드가 끝난 뒤에 돌아오고 interrupt 상태는 남긴다") + void closingFromAnInterruptedThreadStillWaitsForTheDrainThread() throws InterruptedException { + // given: the drain thread is busy closing the old generation, and ignores the interrupt + // shutdownNow sends it, when the registry closes + CountDownLatch drainStarted = new CountDownLatch(1); + ClientRuntime busy = + new ClientRuntime( + ClientProfiles.builder("payment").build(), + new RuntimeGeneration(1), + () -> { + drainStarted.countDown(); + sleepIgnoringInterrupts(Duration.ofMillis(500)); + }); + ClientRuntimeRegistry registry = new ClientRuntimeRegistry(Map.of(busy.name(), busy)); + ClientRuntimeLease lease = registry.acquire(busy.name()); + registry.swap(busy.name(), running(2), Duration.ofMillis(1)); + assertThat(drainStarted.await(5, TimeUnit.SECONDS)).isTrue(); + + // when + Thread.currentThread().interrupt(); + boolean stillInterrupted; + try { + registry.close(); + } finally { + stillInterrupted = Thread.interrupted(); + } + + // then + Set left = liveDrainThreads(); + lease.close(); + assertThat(left).isEmpty(); + assertThat(stillInterrupted).isTrue(); + } + + private static void sleepIgnoringInterrupts(Duration duration) { + long end = System.nanoTime() + duration.toNanos(); + boolean interrupted = false; + for (long left = duration.toNanos(); left > 0; left = end - System.nanoTime()) { + try { + Thread.sleep(Duration.ofNanos(left)); + } catch (InterruptedException ignored) { + interrupted = true; + } + } + if (interrupted) { + Thread.currentThread().interrupt(); + } + } + private static Set liveDrainThreads() { return Thread.getAllStackTraces().keySet().stream() .filter(Thread::isAlive)