Skip to content

[BUG] dialog replication: packets of one call are applied to another call that got the same dialog id on another node #4397

Open

Description

@Lt-Flash

OpenSIPS version you are running

version: opensips 4.1.0-dev (x86_64/linux)
flags: STATS: On, DISABLE_NAGLE, USE_MCAST, SHM_MMAP, PKG_MALLOC, Q_MALLOC, F_MALLOC, HP_MALLOC, F_PARALLEL_MALLOC, DBG_MALLOC, FAST_LOCK-ADAPTIVE_WAIT
ADAPTIVE_WAIT_LOOPS=1024, MAX_RECV_BUFFER_SIZE 262144, MAX_LISTEN 16, MAX_URI_SIZE 1024, BUF_SIZE 65535
poll method support: poll, epoll, sigio_rt, select.
git revision: 6abfdccca
main.c compiled on 23:31:02 Oct 24 2026 with cc 14

Describe the bug

In a dialog replication cluster where more than one node creates dialogs (for example several active nodes behind an anycast address), two nodes can give the same dialog id (h_entry, h_id) to two different calls. The replication handlers then apply one call's packets to the other call, because for current packets they find the target dialog by that id only:

  • dlg_replicated_update() (lookup_dlg_unsafe(h_entry, h_id), dlg_replication.c:487) overwrites the local dialog's state, CSeqs, caller and callee contacts, SDPs, flags, timeout, variables and profiles. The route set is skipped (line 538), so with topology hiding an in-dialog request keeps its own route set but gets the other call's contact as R-URI (set_ruri(req,&leg->contact), dlg_handlers.c:2939).
  • dlg_replicated_value() (line 827) stores the other call's dialog values.
  • dlg_replicated_delete() (lookup_dlg(h_entry, h_id, 1), line 676) ends the local call as if a BYE had been received when the other call hangs up.
  • dlg_replicated_create() (line 217) finds the local dialog and returns at DBG level ("is already created"), so the collision is never logged.

Only dlg_replicated_cseq_updated() checks the result with match_dialog() (line 781) and logs Failed to match dialog leg.

How two nodes reach the same id: every hash entry's next_id starts at rand() (dlg_hash.c:155). A node allocates an id when it creates the dialog on the initial INVITE (dlg->h_id = d_entry->next_id++, dlg_hash.h:418), but the create packet is sent only when the 2xx is relayed (dlg_handlers.c:879). Each node raises its own next_id past every id it receives (line 257, before the socket check, so even a dialog the node then drops moves its counter). So every answered call brings the other nodes' counters for that entry up to the same value, and while a call is still ringing on one node, another node creating a dialog in the same entry can hand out the same id. The window is the ringing time, not the network delay.

The handlers have matched by id since b3c31fb ("dialog: match replicated dialogs by did", 2021, for looping calls with identical SIP coordinates; also in 3.1.6+ and 3.2.3+), which assumes ids are unique in the cluster. That holds when one node at a time creates dialogs, not when several do.

To Reproduce

Two nodes, clusterer with both nodes in one cluster, modparam("dialog", "dialog_replication_cluster", 1), SIP sockets with the same tags on both nodes (so each node accepts the other's dialogs), and topology_hiding() in dialog mode. With the default hash_size the collision happens by chance under load. To get it on every run I set hash_size to 2 and chose Call-IDs that hash to the same entry.

The relevant part of the script on each node (node 2 is the same with its own addresses):

socket = udp:127.0.0.11:5070 tag priv
socket = udp:127.0.0.11:5060 tag pub
socket = bin:127.0.0.11:3857
modparam("clusterer", "db_mode", 0)
modparam("clusterer", "my_node_id", 1)
modparam("clusterer", "my_node_info", "cluster_id=1, url=bin:127.0.0.11:3857, flags=seed")
modparam("clusterer", "neighbor_node_info", "cluster_id=1, node_id=2, url=bin:127.0.0.12:3857")
modparam("dialog", "dialog_replication_cluster", 1)
modparam("dialog", "hash_size", 2)

route {
	if (has_totag()) {
		if (topology_hiding_match()) {
			if (is_method("ACK"))
				$dlg_val(acked) = "node1 " + $ci;
			xlog("L_NOTICE", "RIG SEQ $rm ci=$ci did=$DLG_did ru=$ru\n");
			t_relay();
			exit;
		}
		xlog("L_NOTICE", "RIG NOMATCH $rm ci=$ci ru=$ru\n");
		send_reply(404, "Not here");
		exit;
	}
	if (t_check_trans())
		exit;
	create_dialog();
	$dlg_val(origin) = "node1 " + $ci;
	topology_hiding();
	force_send_socket("udp:127.0.0.11:5060");
	xlog("L_NOTICE", "RIG INV ci=$ci did=$DLG_did ru=$ru\n");
	t_relay();
}

Steps (sipp as callers and callees):

  1. One answered call through each node, so both nodes' next_id end up level.
  2. Call A to node 1, callee rings 1 s. 0.5 s later call B to node 2, callee rings 3 s. Both calls get the same did (visible in the Contact and in the xlog lines).
  3. A is answered. Node 2 skips A's create packet (the id is taken by B) and applies A's update and value packets to B, which is still ringing.
  4. B is answered, and 2 s later B's callee hangs up. Node 2 sends that BYE to A's caller instead of B's caller.
  5. B's dialog ends, node 1 applies B's delete packet to A, and A's next in-dialog request finds no dialog.

Expected behavior

A replicated packet should only be applied to the dialog it describes. If the dialog found by id has a different Call-ID (or From tag), the packet should be ignored and logged, as dlg_replicated_cseq_updated() already does.

Relevant System Logs

Unpatched master, the run in the steps above. Both calls got the dialog id 0.002ac9f1.

Node 1 (call A); A's BYE at the end finds no dialog, because B's delete packet ended it:

Oct 24 23:38:12 [2174688] NOTICE:RIG INV ci=rig-a-0@127.0.0.1 did=0.002ac9f1 ru=sip:a@127.0.0.1:7001 du=<null>
Oct 24 23:38:13 [2174688] NOTICE:RIG SEQ ACK ci=rig-a-0@127.0.0.1 did=0.002ac9f1 ru=sip:uas-a@127.0.0.1:7001
Oct 24 23:38:21 [2174688] NOTICE:RIG NOMATCH BYE ci=rig-a-0@127.0.0.1 ru=sip:127.0.0.11:5070;did=0.002ac9f1

Node 2 (call B); B's callee hangs up and node 2 sends the BYE to call A's caller (port 6001) instead of B's caller (port 6002):

Oct 24 23:38:12 [2174752] NOTICE:RIG INV ci=rig-b-2@127.0.0.1 did=0.002ac9f1 ru=sip:b@127.0.0.1:7002 du=<null>
Oct 24 23:38:15 [2174752] NOTICE:RIG SEQ ACK ci=rig-b-2@127.0.0.1 did=0.002ac9f1 ru=sip:uas-b@127.0.0.1:7002
Oct 24 23:38:17 [2174753] NOTICE:RIG SEQ BYE ci=rig-b-2@127.0.0.1 did=0.002ac9f1 ru=sip:caller-a@127.0.0.1:6001

What call A's caller received:

BYE sip:caller-a@127.0.0.1:6001 SIP/2.0
Via: SIP/2.0/UDP 127.0.0.12:5070;branch=z9hG4bK6f.6cbc20e3.0
From: <sip:b@127.0.0.1:7002>;tag=2174858SIPpTag011
To: <sip:caller-b@127.0.0.1:6002>;tag=2174870SIPpTag001
Call-ID: rig-b-2@127.0.0.1
CSeq: 1 BYE

dlg_list_ctx as B's callee hung up (both dialogs still confirmed), trimmed to the relevant fields. Call B on node 2 holds call A's caller contact and A's origin value; call A on node 1 holds B's acked value:

node 2: {"ID": "0.002ac9f1", "callid": "rig-b-2@127.0.0.1", "caller_contact": "sip:caller-a@127.0.0.1:6001", "callee_contact": "sip:uas-b@127.0.0.1:7002", "values": [{"acked": "node2 rig-b-2@127.0.0.1"}, {"origin": "node1 rig-a-0@127.0.0.1"}]}
node 1: {"ID": "0.002ac9f1", "callid": "rig-a-0@127.0.0.1", "caller_contact": "sip:caller-a@127.0.0.1:6001", "callee_contact": "sip:uas-a@127.0.0.1:7001", "values": [{"acked": "node2 rig-b-2@127.0.0.1"}, {"origin": "node1 rig-a-0@127.0.0.1"}]}

Neither node logged a dialog error or warning in this run.

OS/environment information

  • Operating System: Debian GNU/Linux 13 (trixie), kernel 6.12.111+deb13-amd64, gcc 14.2.0
  • OpenSIPS installation: git (source build, Makefile.conf from the template with only CC_O0 disabled)
  • other relevant information: in production I run 8 nodes, all active behind one anycast address, with dialog replication, socket tags and topology hiding in dialog mode.

Additional context

In production this showed up as a callee's BYE sent to an unrelated phone's contact with the correct route set, so the caller never learned the call had ended, and as live dialogs disappearing right after another node's call with the same did hung up (their next re-INVITE got no dialog). The only trace in the logs is the occasional Failed to match dialog leg from the CSeq handler.

I will open a PR that checks the Call-ID and From tag after the id lookup. It stops the corruption; two nodes can still hand out the same id, so on the other nodes one of the two calls then has no replica. Making ids unique per node would remove that too, which I leave to you.

Activity

  1. github-actions commented on Nov 11, 2026

    @github-actions

    Any updates here? No progress has been made in the last 15 days, marking as stale. Will close this issue if no further updates are made in the next 30 days.

  2. Lt-Flash commented on Nov 11, 2026

    @Lt-Flash
    ContributorAuthor

    Working on solution

  3. github-actions commented on Nov 27, 2026

    @github-actions

    Any updates here? No progress has been made in the last 15 days, marking as stale. Will close this issue if no further updates are made in the next 30 days.

  4. Lt-Flash commented on Nov 28, 2026

    @Lt-Flash
    ContributorAuthor

    Fix is in #4398 (instance id in the dialog id, as agreed with @razvancrainea), rebased on current master and waiting for review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions