From 416d88247100c7b9bdeee2a2f0933d54b29bee14 Mon Sep 17 00:00:00 2001 From: DongHyeonka Date: Fri, 6 Nov 2026 22:01:08 +0900 Subject: [PATCH] =?UTF-8?q?fix(httpclient):=20JDK=20=EC=8B=9C=EB=8F=84?= =?UTF-8?q?=EA=B0=80=20deadline=20=EC=A7=81=EC=A0=84=EC=97=90=20=EB=81=8A?= =?UTF-8?q?=EA=B2=A8=20=EC=9D=91=EB=8B=B5=20timeout=EC=9C=BC=EB=A1=9C=20?= =?UTF-8?q?=EB=B3=B4=EA=B3=A0=EB=90=98=EC=A7=80=20=EC=95=8A=EA=B2=8C=20?= =?UTF-8?q?=ED=95=9C=EB=8B=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 문제: JDK transport는 attempt deadline을 Spring JdkClientHttpRequest의 read timeout 하나로 끊는다. DeadlineAwareJdkRequestFactory는 그 값을 AttemptDeadline.bound(response-header)로 정하고, bound는 남은 시간을 나노초까지 그대로 돌려준다. Spring 7.0.9 TimeoutHandler는 이 값을 Duration.toMillis()로 잘라 timer를 건다. 그래서 timer가 deadline보다 최대 1ms 먼저 터진다. BlockingAttemptExecutor.finish와 circuit breaker 제외 조건 (deadline.setByCaller() && scope.expired())은 엔진이 포기한 뒤에 scope.expired()로 deadline이 원인인지 묻는다. 그 순간 deadline은 아직 지나지 않았다. 그래서 호출자 deadline으로 끊긴 시도가 HttpResponseTimeoutException(헤더 대기 중)이나 HttpResponseTruncatedException(본문 중)으로 보고되고, breaker는 그것을 상대 장애로 센다. 건강한 상대 서버에 circuit이 열릴 수 있다. CircuitBreakerStatusAccountingTest의 JDK 변형 2건이 이 경로로 실패했다. 고른 방법: bound가 남은 시간으로 줄인 값을 다음 밀리초로 올린다. 엔진 timer는 bound를 계산한 뒤에 걸리므로, 올린 값을 밀리초로 잘라 건 timer도 deadline보다 먼저 끝나지 않는다. 단계 timeout이 더 짧으면 그 값을 그대로 둔다. 버린 대안: - JDK 일반 요청도 scope abort로 끊기(Apache·Jetty 방식). Spring 요청의 응답 future에 닿을 수 없어 JdkClientHttpRequest를 본문 닫기까지 다시 구현해야 한다. 결함 크기에 비해 바뀌는 면이 크다. - finish에서 1ms 여유를 두고 deadline으로 판정. deadline 근처에서 터진 진짜 단계 timeout도 deadline으로 바꿔 부르는 추측이다. - factory에서만 올리기. bound를 쓰는 다음 엔진이 같은 함정에 빠진다. 위험·한계: - deadline으로 줄인 JDK timer는 deadline보다 1ms 미만 늦게 끝난다. JDK 일반 요청이 deadline을 넘기는 시간이 그만큼 늘어난다. - 같은 원인을 찾아본 곳: JDK 스트리밍 setup(JDK 요청 timeout은 나노초를 지키고 deadline은 scope abort가 끊는다), Apache·Jetty(scope abort가 fired를 먼저 세운다), messaging DefaultMessagePublisher.withDeadline (밀리초로 자르지만 원인을 TimeoutException 타입으로 판정한다), gRPC·Redis(나노초 단위). 같은 원인은 없었다. 검증(desktop, 공유 잠금 아래, 로그 ~/t2-cb-logs/): - RED: 기준 커밋 06f520e6에 새 테스트만 얹고 CircuitBreakerStatusAccountingTest·AttemptDeadlineTest를 --rerun으로 10회. 10/10 rc=1. 새 단위 테스트 10/10 실패, CircuitBreaker JDK 변형은 10회 중 9회 실패(JDK 케이스 20건 중 14건). - GREEN: 수정 트리에서 같은 명령 10회 두 번. 20/20 rc=0, 실패 0. - ./gradlew ci: rc=0(93초, 다른 leaf는 캐시 재사용, httpclient test 359건·계약 23건·보안 9건·Spring 6.2 스캔 7건 실제 실행). - :adapter:outbound:httpclient:testQualityCheck spring70CompatibilityTest (23건) rc=0, httpClientFailureInjectionTest(Toxiproxy, 4건) rc=0. Fixes: 34565130e021 ("fix!: 성숙도 리뷰의 High·P1 결함 19건을 고친다") --- .../jdk/DeadlineAwareJdkRequestFactory.java | 5 ++++ .../httpclient/transport/AttemptDeadline.java | 19 +++++++++++++- .../transport/AttemptDeadlineTest.java | 26 +++++++++++++++++++ 3 files changed, 49 insertions(+), 1 deletion(-) diff --git a/src/adapter/outbound/httpclient/src/main/java/dev/caskeleton/adapter/outbound/httpclient/jdk/DeadlineAwareJdkRequestFactory.java b/src/adapter/outbound/httpclient/src/main/java/dev/caskeleton/adapter/outbound/httpclient/jdk/DeadlineAwareJdkRequestFactory.java index 6b28ad8f..943f3a5d 100644 --- a/src/adapter/outbound/httpclient/src/main/java/dev/caskeleton/adapter/outbound/httpclient/jdk/DeadlineAwareJdkRequestFactory.java +++ b/src/adapter/outbound/httpclient/src/main/java/dev/caskeleton/adapter/outbound/httpclient/jdk/DeadlineAwareJdkRequestFactory.java @@ -22,6 +22,11 @@ import org.springframework.http.client.JdkClientHttpRequestFactory; * timeout, remaining deadline)} from {@link AttemptDeadline}. The factory only holds references; * the client and the write executor are shared. * + *

That timer is the only thing that ends a JDK attempt at its deadline, and it keeps whole + * milliseconds. {@link AttemptDeadline#bound(Duration)} rounds the remaining time up for that + * reason: truncated, the timer fired just before the deadline and the attempt was reported as a + * response timeout rather than the deadline. + * *

A streaming download cannot use that request. Its body is handed to the caller and may run for * minutes, but Spring's timer only stops when the body stream is closed, so every JDK download * longer than the response-header timeout was cut mid-body (review 2026-11-03 RR-04-03). Inside a diff --git a/src/adapter/outbound/httpclient/src/main/java/dev/caskeleton/adapter/outbound/httpclient/transport/AttemptDeadline.java b/src/adapter/outbound/httpclient/src/main/java/dev/caskeleton/adapter/outbound/httpclient/transport/AttemptDeadline.java index 36d072bb..b2ac3dfa 100644 --- a/src/adapter/outbound/httpclient/src/main/java/dev/caskeleton/adapter/outbound/httpclient/transport/AttemptDeadline.java +++ b/src/adapter/outbound/httpclient/src/main/java/dev/caskeleton/adapter/outbound/httpclient/transport/AttemptDeadline.java @@ -1,6 +1,7 @@ package dev.caskeleton.adapter.outbound.httpclient.transport; import java.time.Duration; +import java.time.temporal.ChronoUnit; import java.util.ArrayList; import java.util.List; import java.util.Objects; @@ -83,14 +84,30 @@ public final class AttemptDeadline { return Optional.ofNullable(CURRENT.get()).map(Scope::remaining); } - /** A stage timeout no longer than the current attempt's remaining time. */ + /** + * A stage timeout cut down to the current attempt's remaining time when that is shorter. + * + *

A cut-down value is rounded up to the next whole millisecond, so an engine timer armed from + * it never ends before the deadline. Engines keep their timeouts in milliseconds — Spring's JDK + * request arms its timer with {@code Duration.toMillis()} — and the truncated remainder made that + * timer fire just before the deadline. The executor names the deadline by asking the scope after + * the engine gave up, so such an attempt was reported as a response timeout on a healthy upstream + * and the circuit breaker counted the caller's own deadline against it. Ending under a + * millisecond late costs nothing: by then the scope reports itself expired. + */ public static Duration bound(Duration stageTimeout) { Objects.requireNonNull(stageTimeout, "stage timeout"); return remaining() .filter(remaining -> remaining.compareTo(stageTimeout) < 0) + .map(AttemptDeadline::toWholeMillisecondsUp) .orElse(stageTimeout); } + private static Duration toWholeMillisecondsUp(Duration duration) { + Duration truncated = duration.truncatedTo(ChronoUnit.MILLIS); + return truncated.equals(duration) ? duration : truncated.plusMillis(1); + } + /** * Registers how to abort the exchange the current attempt is about to start. * diff --git a/src/adapter/outbound/httpclient/src/test/java/dev/caskeleton/adapter/outbound/httpclient/transport/AttemptDeadlineTest.java b/src/adapter/outbound/httpclient/src/test/java/dev/caskeleton/adapter/outbound/httpclient/transport/AttemptDeadlineTest.java index 388bb2c6..fa2cfc77 100644 --- a/src/adapter/outbound/httpclient/src/test/java/dev/caskeleton/adapter/outbound/httpclient/transport/AttemptDeadlineTest.java +++ b/src/adapter/outbound/httpclient/src/test/java/dev/caskeleton/adapter/outbound/httpclient/transport/AttemptDeadlineTest.java @@ -6,6 +6,7 @@ import java.time.Duration; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.locks.LockSupport; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -41,6 +42,31 @@ class AttemptDeadlineTest { } } + @Test + @DisplayName("bound 값을 밀리초로 잘라 건 엔진 timer도 attempt deadline보다 먼저 끝나지 않는다") + void anEngineTimerArmedInWholeMillisecondsDoesNotEndBeforeTheDeadline() { + // Spring's JDK request arms its timer with Duration.toMillis(). A remaining time with a + // sub-millisecond part lost it there, the engine gave up just before the deadline, and the + // executor, asking the scope a moment later, reported a response timeout instead. + try (AttemptDeadline.Scope scope = + AttemptDeadline.open(Duration.ofMillis(20).plusNanos(900_000))) { + // given + Duration bounded = AttemptDeadline.bound(Duration.ofSeconds(2)); + long armedAt = System.nanoTime(); + long firesAt = armedAt + TimeUnit.MILLISECONDS.toNanos(bounded.toMillis()); + + // when + while (System.nanoTime() - firesAt < 0) { + LockSupport.parkNanos(firesAt - System.nanoTime()); + } + + // then + assertThat(scope.expired()) + .as("deadline passed when a whole-millisecond timer from bound() = %s fired", bounded) + .isTrue(); + } + } + @Test @DisplayName("deadline이 지나면 등록된 중단 동작을 실행하고 scope는 만료로 보고한다") void theRegisteredAbortRunsWhenTheDeadlinePasses() throws Exception {