Skip to content

Commit 7773db4

Browse files
lunasawclaude
andcommitted
fix(dialog): DialogRegistry 身份感知移除 — 修复自环 UAS 腿终结误删 UAC 项
自环(同 JVM 同 SipStack 既发 INVITE 又收 INVITE)下,出站 UAC dialog 与 入站 UAS dialog 共享同一 callId,但仅 UAC dialog 注册在 DialogRegistry。 processDialogTerminated 旧实现按 callId 盲删,UAS 腿先终结时误删仍存活、 等待发 BYE 的 UAC 项,导致关流 BYE 抛 DialogNotFoundException。 - DialogRegistry 新增 remove(callId, dialog):computeIfPresent 比对 entry.dialog == dialog,仅终结的正是注册项本身才移除。 - AbstractSipListener.processDialogTerminated 改调身份版,自环对端腿 终结不再误删本侧注册项。 仅 lab 自环触发;真实部署本 JVM 只有 UAC 腿,无交叉删除。 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent e5dbd11 commit 7773db4

2 files changed

Lines changed: 33 additions & 1 deletion

File tree

‎sip-common/src/main/java/io/github/lunasaw/sip/common/transmit/AbstractSipListener.java‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -501,9 +501,13 @@ public void processDialogTerminated(DialogTerminatedEvent dialogTerminatedEvent)
501501
CallIdHeader callIdHeader = dialog.getCallId();
502502
String callId = callIdHeader != null ? callIdHeader.getCallId() : null;
503503
if (callId != null) {
504-
Dialog removed = DialogRegistry.remove(callId);
504+
// 身份校验移除:自环(同栈既发又收 INVITE)下 UAC/UAS 共享 callId,
505+
// 仅当终结的正是注册项本身才移除,避免 UAS 腿终结误删等待 BYE 的 UAC 项
506+
Dialog removed = DialogRegistry.remove(callId, dialog);
505507
if (removed != null) {
506508
log.debug("DialogTerminatedEvent 清理 DialogRegistry: callId={}", callId);
509+
} else {
510+
log.debug("DialogTerminatedEvent 未匹配注册项(自环对端腿或已清理),保留 registry: callId={}", callId);
507511
}
508512
}
509513
}

‎sip-common/src/main/java/io/github/lunasaw/sip/common/transmit/DialogRegistry.java‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,34 @@ public static Dialog remove(String callId) {
119119
return removed == null ? null : removed.dialog;
120120
}
121121

122+
/**
123+
* 按 callId + dialog 身份移除 —— 仅当注册项持有的 dialog 与传入 dialog 为<b>同一对象</b>时才移除。
124+
*
125+
* <p>自环场景(同一 JVM、同一 {@code SipStack} 既发 INVITE 又收 INVITE)下,出站 UAC dialog 与
126+
* 入站 UAS dialog <b>共享同一 callId</b>,但只有 UAC dialog 注册在本表。若按 callId 盲删
127+
* ({@link #remove(String)}),UAS 腿先终结时会触发 {@code DialogTerminatedEvent} 误删仍存活、
128+
* 等待发 BYE 的 UAC 项,导致后续 BYE 抛 {@link DialogNotFoundException}。本方法比对 dialog 对象
129+
* 身份,仅在终结的正是注册项本身时移除,避免交叉误删。
130+
*
131+
* @param callId Call-ID
132+
* @param dialog 真正终结的 JAIN-SIP Dialog
133+
* @return 实际被移除的 dialog;未命中(callId 无项 / dialog 不匹配)返回 null
134+
*/
135+
public static Dialog remove(String callId, Dialog dialog) {
136+
if (callId == null || dialog == null) {
137+
return null;
138+
}
139+
Dialog[] holder = new Dialog[1];
140+
BY_CALL_ID.computeIfPresent(callId, (k, entry) -> {
141+
if (entry.dialog == dialog) {
142+
holder[0] = entry.dialog;
143+
return null; // 身份匹配 → 移除
144+
}
145+
return entry; // 非注册项(如自环 UAS 腿)→ 保留
146+
});
147+
return holder[0];
148+
}
149+
122150
public static int size() {
123151
return BY_CALL_ID.size();
124152
}

0 commit comments

Comments
 (0)