fix(httpclient): interrupt된 스레드에서 닫아도 drain 스레드 종료를 기다린다
문제: ClientRuntimeRegistry.close()는 "돌아오면 drain 스레드가 없다"를 약속하고, 기다리다 시간이 지나면 IllegalStateException을 던진다. 그런데 닫는 스레드가 이미 interrupt 상태면 첫 awaitTermination이 바로 InterruptedException을 내고, catch가 flag만 되살린 뒤 정상으로 돌아온다. drain 스레드가 살아 있어도 호출자는 모른다(리뷰 RR7-02-01). 고른 방법: interrupt를 받아도 같은 마감(5초)까지 다시 기다리고, 기다림이 끝나면 flag를 되살린다. 마감 안에 끝나지 않으면 다른 경로처럼 IllegalStateException을 던진다. 기다리는 부분을 awaitDrainThreadExit로 떼어 내 interrupt마다 남은 시간으로 다시 부른다. 버린 대안: interrupt 경로에서 바로 IllegalStateException을 던진다 — 더 단순하지만, 곧 끝날 스레드를 두고 정상 종료를 실패로 보고한다. 위험·한계: interrupt된 스레드의 close()가 이제 최대 5초까지 막힐 수 있다(전에는 바로 돌아왔다). interrupt된 채 마감을 넘기는 경로는 테스트하지 않았다(5초를 기다려야 한다). 같은 모양을 찾아본 다른 종료 코드(gRPC 서버, notification worker, S3 worker, outbox relay)는 "스레드가 없다"를 약속하지 않고 shutdownNow로 끝내는 최선 노력이라 고치지 않았다. 검증(desktop, 로그 ~/r8-f5-logs/): - RED: 기준 73d39212에 테스트만 얹음. closingFromAnInterruptedThreadStillWaitsForTheDrainThread 실패 — "Expecting empty but was: [httpclient-runtime-drain]" (red-rr7-02-01.log, .xml) - GREEN: ClientRuntimeRegistryTest rc=0 (green-rr7-02-01.log) - :adapter:outbound:httpclient:check, :testQualityCheck, httpClientStableContractTest(apache, jdk), spring70CompatibilityTest, :ci rc=0 (summary.txt) Refs: docs/reviews/2026-11-03-rereview/round7/02-idempotency-outbound.md RR7-02-01
This commit is contained in:
+31
-11
@@ -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.
|
||||
*
|
||||
* <p>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<ClientRuntime> holder(ClientProfileName name) {
|
||||
AtomicReference<ClientRuntime> holder = runtimes.get(name);
|
||||
if (holder == null) {
|
||||
|
||||
+53
@@ -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<String> 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<String> liveDrainThreads() {
|
||||
return Thread.getAllStackTraces().keySet().stream()
|
||||
.filter(Thread::isAlive)
|
||||
|
||||
Reference in New Issue
Block a user