--- kind: CONCEPT slug: messaging-security-c03 title: 한 경합에서 서로 다른 두 결함이 나왔다 topic: state-machines-and-ownership project: clean-architecture-backend-template status: 게시 전 sourceRevision: 21234e38cdb9a926cbc92bb97a2aee2e4a7d2916 rootTreeNode: concept:messaging-security-c03 evidenceCapturedOn: 2026-09-01 assets: - key: messaging-security-c03 file: ../../../final/evidence/rendered/messaging-security-c03.svg - key: messaging-security-c03-diagram file: ../../../final/assets/diagrams/messaging-security-c03.svg evidence: - ../../../final/evidence/raw/messaging-security-c03.txt source: - 원본 분석 절은 analysis/messaging/messaging-security.md#L141 이다. module: messaging-security --- # 한 경합에서 서로 다른 두 결함이 나왔다 `CredentialRuntimeRegistry.resolve`의 key별 single-flight가 이 leaf에서 가장 조밀한 동시성 코드이고, 이전 결함이 주석에 통째로 남아 있다. ## 관계 - **배선된 게이트는 자기 leaf 레인에서 검증한다** 같은 분석 리프에서 끌어낸 규칙이다. - **같은 술어가 두 타입에 있으면 하나가 다른 하나를 부른다** 같은 분석 리프에서 끌어낸 규칙이다. - **가변 필드로 상태 전이를 표현하면 가시성을 함께 정한다** 같은 분석 리프에서 끌어낸 규칙이다. - **맵 갱신 함수 안에서 I/O를 하면 그 지연이 락 범위가 된다** 같은 분석 리프에서 끌어낸 규칙이다. ## 본문 이 leaf에서 가장 조밀한 동시성 코드이고, 이전 결함이 주석에 통째로 남아 있다. > "get → fetch → put → clear had no synchronization at all. Two callers rotating the same credential both read the same old runtime and both fetched a replacement: one replacement was lost from the map without ever being cleared — a secret left in memory that nothing owns — and the caller that lost the race could clear material the winner was still using." **두 개의 서로 다른 결함이 한 경합에서 나왔다** — 소유자 없는 비밀이 힙에 남는 것과, 사용 중인 자격증명이 지워지는 것. ## 교체와 소거의 순서 :::evidence key="messaging-security-c03-diagram" alt="compute 진입과 provider resolve 와 replacement 설치와 이전 세대 소거가 왼쪽에서 오른쪽으로 이어지는 구조" caption="교체와 소거의 순서" zoom="false" ::: `ConcurrentHashMap.compute`가 해당 bin의 락을 잡으므로 fetch가 정확히 한 번 일어난다. 그리고 **`clear()`가 `replacement` 생성 후에 온다** — 주석이 그 순서의 이유를 적는다: "no reader sees a window with no usable credential — and only by the thread that replaced it, so the material a concurrent reader holds is never wiped underneath it." ## ConcurrentHashMap 참조 위치 :::evidence key="messaging-security-c03" alt="코드베이스에서 ConcurrentHashMap 를 검색한 출력 2줄. 이 기록이 세는 참조가 그 출력에 그대로 보인다." caption="ConcurrentHashMap 코드베이스 검색 — 2줄 · exit 0" zoom="true" ::: ## 외부 I/O가 bin 락을 잡은 채로 일어난다 `compute`의 람다 안에서 `provider.resolve(...)`가 호출된다. 같은 credential id를 요청하는 다른 스레드는 그 동안 막히고, `ConcurrentHashMap` 문서는 compute 람다 안에서 같은 맵을 갱신하지 말라고 요구한다(여기서는 지켜진다). 다른 키는 다른 bin이면 막히지 않지만 해시 충돌 시 같은 bin이면 막힌다. §17. ## 별도 플래그 없이 같은 필드로 상태를 표현한다 소거 판정이 `material.length == 0`이다. 생성자가 빈 배열을 거절하므로(`"credential material must not be empty"`) 길이 0은 소거된 상태를 뜻한다. ## volatile이 아닌 필드가 남긴 위험 **`material` 필드가 `volatile`이 아니다.** `clear()`가 다른 스레드에서 호출되면 `material()`이 옛 참조를 볼 수 있다. 실제 경로에서는 `compute` 안에서만 `clear()`가 불리고 그 전에 `replacement`가 맵에 들어가므로 위험이 낮지만, `clearAll()`은 락 없이 순회한다. §17.