Skip to content

dialog: ignore replicated packets that hit another call's dialog id - #4398

Open
Lt-Flash wants to merge 4 commits into
OpenSIPS:masterfrom
Lt-Flash:fix/dialog-repl-id-collision
Open

Lt-Flash wants to merge 4 commits into
OpenSIPS:masterfrom
Lt-Flash:fix/dialog-repl-id-collision

Conversation

@Lt-Flash

Copy link
Copy Markdown
Contributor

Summary

Stop dialog replication from applying one call's update, value and delete packets to a different call that got the same dialog id on another node.

Details

This is a bug fix for any dialog replication cluster where more than one node creates dialogs, such as several active nodes behind an anycast address. Issue #4397 has the full analysis and a reproduction.

Since b3c31fb the replication handlers find their dialog by (h_entry, h_id) only, which assumes the id is unique in the cluster. It is not: a node allocates the id on the initial INVITE but replicates the dialog only once it is answered, and every node raises its next_id past the ids it receives. So while a call rings on one node, another node can give a new call the same id. From then on:

  • update packets overwrite the other call's contacts, SDPs, state, flags, timeout and variables (topology hiding then sends its in-dialog requests to the wrong contact);
  • value packets replace its dialog values;
  • a delete packet ends it when the other call hangs up;
  • the create packet is skipped silently.

Solution

A new helper, dlg_repl_id_collision(), compares the Call-ID, and the caller tag when the packet carries one, of the dialog found by id with the packet. On a mismatch it logs

dialog id collision [<h_entry>:<h_id>]: ignoring replicated <create|update|value|delete> for callid |...| ftag |...|, local dialog has callid |...| ftag |...|

(without the From tags for value packets, which do not carry one) and the handler drops the packet. It is called:

  • in dlg_replicated_update() before the "not found, build a new dialog" branch, so no second dialog with the same id is created either;
  • in dlg_replicated_value(), on the Call-ID only, as value packets carry no tags;
  • in dlg_replicated_delete(), under the entry lock, since lookup_dlg() returns with the lock released and the legs array can be reallocated; the reference taken by lookup_dlg() is released on a mismatch;
  • in the "already created" branch of dlg_replicated_create(), which keeps skipping the packet but now logs the collision.

The check applies only to lookups by id (DLG_BIN_V4 and later; value packets always); older packets are already matched by Call-ID and tags. It does not affect the looping case b3c31fb was written for: looped dialogs share the Call-ID and From tag, so they pass the check and the id still picks the right one.

Two nodes can still give the same id to two calls; on the other nodes one of the two calls then has no replica, and an in-dialog request for it that reaches such a node is not matched. This PR does not change id allocation.

How I tested it: two nodes on one host as in #4397, with collisions forced on every run, comparing master 6abfdcc with this branch:

master 6abfdcc this branch
B's callee hangs up (the steps in the issue) BYE sent to A's caller BYE sent to B's caller
A hangs up after B ended (three timings) no dialog: B's delete packet ended A routed normally
dialog contents while both calls are up B holds A's caller contact and dialog value, A holds B's value each dialog keeps its own
packets for the other call applied, nothing logged logged and dropped (create, update, value, delete)
looping call (two dialogs, same Call-ID and tags) replicated and ended on both nodes the same, no collision logged
in-dialog BYE sent to the node that did not create the call routed from its replica the same

Builds: the whole tree with gcc 14.2.0 and -Werror (0 warnings), and the dialog module with clang 19.1.7 and -Werror.

Compatibility

No configuration, script or replication format change. Packets that belong to the dialog found by id always carry its Call-ID and caller tag, so they are applied as before. The only visible change is the new error line on a collision. There is no platform-specific code.

Closing issues

closes #4397

@razvancrainea razvancrainea self-assigned this Oct 27, 2026
@razvancrainea

Copy link
Copy Markdown
Member

Hi, @Lt-Flash !

I am curious whether this is this something that you actually hit in production/testing, or it is just a theoretical fix. Because I can totally agree it may happen, but it is very unlikely :). Did you hit it?
If you actually hit it, I don't think this is the correct fix - ignoring the dialogs may lead to inconsistent states, therefore defies the purpose of the replication altogether. That's why I'd rather propose a different fix/enhancement: each module can have a new dialog module parameter called instance/id, or something like that, that may be used to build the dialog id: that would change a bit the format of the dialog id, because a new label will be introduced, before the hash_id and hash_entry - but my idea is that each replication (but also sequential handling) should support this new parameter's parser - this way we can differentiate between the server that created the dialog from the one that got replicated, and we can say for sure whether it is a colision or now. Of course, provided that each instance uses a different instance id. So essentiall, the new dialog id would look something like: did.instance.hash_id.hash_entry. If instance is not specified, we can skip it.
At this point, there should be no colition, unless something is misconfigured (same isntance ids, or lack of it), but that's a design issue and should be sorted out separately - at this point we can even ignore the collided dialogs, as you suggested.

What do you think? do you want to give it a try using this approach? If not, I can work out something myself to get this fixed.

Best regards,
Răzvan

@Lt-Flash

Lt-Flash commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor Author

Hi @razvancrainea,

Thanks a lot for looking into this so quickly!

Yes, I'm afraid it's real, not just theory. Customers noticed it and reported it to support: dead air, calls left up on their PBX, hold and transfer failing mid-call. When I traced those calls, I found the same did used by different calls on different SBCs. I run 8 active nodes on one anycast address, with ECMP in front of them. In-dialog requests then followed the other call's dialog. One BYE meant for the Asterisk PBX ended up on an unrelated customer's phone. Other calls lost their dialog when the other call ended, and their next requests got a 404.

It's less rare than it looks. A node takes the id on the INVITE, but replicates the dialog only once it's answered. So the window is the whole ringing time. #4397 reproduces it on every run.

I needed a quick fix to help the customers, so I opened this PR. I built OpenSIPS with it and put it into production, and it helped.

Thank you for catching the mistake in my patch, much appreciated. The create branch returns before the FROM_DB/SYNCED handling. So a colliding local dialog could be dropped at the end of a sync. I didn't hit it because I keep no dialogs in a database and use no sharing tags.

I really like your instance id idea. It's a much better fix, and I'd be happy to implement it. A few questions before I start:

  • Should it default to the clusterer node id when dialog_replication_cluster is set?
  • Format instance.h_entry.h_id, keeping today's h_entry.h_id when no instance is set?
  • For db_mode, would you add a new column to the dialog table?
  • Same PR or a new one? I'd like to keep this check as a guard, if you agree.

Thanks again!

@razvancrainea

Copy link
Copy Markdown
Member

Hi, @Lt-Flash !

  • yes, it is a good idea to default it to @dialog_replication_cluster@, if provisioned. But I do see this helpful even if dialog replication is not used, especially when chaining opensips instances one after another - it would be a mechansim to make sure instance 1 does not recognize instance 2's dialog as its own; currently a different rr_param is suggested, but adding an instance id will do it as well.
  • check the next paragraph :).
  • this is actually interesting :) because we uniquely identify the dialog based on h_entry.h_id. Therefore, even though we can use the instance to determine whether it is our dialog or not, we can not differentiate between two dialogs with same coordinates on two different servers. This means that somehow this instance should take part of the current h_entry.h_id format, perhaps "reserve" a byte in the most signigicant h_entry, and leave the rest 3 bytes for the actualy hash table? that would limit our instance ids to 255, but will make this work. Other ideas are welcome :)
  • you can ammend this PR

Thanks a lot for your report and taking care of this!

Cheers,
Răzvan

@Lt-Flash

Lt-Flash commented Nov 9, 2026

Copy link
Copy Markdown
Contributor Author

Hi @razvancrainea,

Thanks a lot for your reply! I am glad to help with resolving this issue.

One other idea: take the byte from h_id instead of h_entry. So h_id = the instance in the top byte plus a 24-bit counter per bucket.

  • h_entry is the bucket index, and the receiver of a replicated packet computes it from the Call-ID. The instance there would need a new field in every replication packet.
  • h_id is allocated in one place and already travels in every replication packet, in the did and in the DB dlg_id. So the did format, the DB schema and the BIN packets stay as they are.

The code change is small: the allocation, and the three "next_id follows the max" places, which would follow only ids of my own instance. The counter per bucket becomes 24 bits instead of 32, so it wraps after 2^24 dialogs per bucket.

The parameter (say instance_id, 1 to 255) defaults to the node id when dialog_replication_cluster is set and the id fits in a byte. It also works without replication, for your chained case. When it is not set and there is no replication cluster, nothing changes: h_id stays the plain 32-bit counter it is today.

The two commits of this PR could stay as a guard, for partly upgraded clusters and for a restarted node until its sync completes. Whether to keep them is up to you: I am happy to implement both, or the instance alone.

Is the h_id byte fine with you? If you prefer h_entry, I will do that instead.

Thanks again!

@Lt-Flash

Lt-Flash commented Nov 9, 2026

Copy link
Copy Markdown
Contributor Author

One more point for the choice between the two bytes, about the dialog DB. I tested it.

The DB dlg_id is (h_entry << 32) | h_id, so the top byte of h_entry is bits 56 to 63 of it. The DB layer carries a BIGINT as a signed long long, and the MySQL column is BIGINT(10) UNSIGNED. So instances 128 to 255 in that byte give a negative value for an unsigned column.

Test: the tree of this PR plus a test-only patch that puts an instance byte in either place, MariaDB 11.8.6 with the stock dialog table, one answered call, a restart of OpenSIPS during the call, then the BYE.

instance byte row in dialog after the restart
top byte of h_entry, 127 stored dialog restored, BYE matched
top byte of h_entry, 128 or 255 not stored: error 1264, Out of range value for column 'dlg_id' dialog lost, BYE finds no dialog
top byte of h_id, 255 stored dialog restored with the same did, BYE matched

I ran 128 with db_mode 1, 2 and 3: the same result in all three. With a non-strict sql_mode the insert passes, but the row is stored with dlg_id 0 and the load at startup rejects it.

So the h_entry byte would need the instances limited to 127, or a change in how the DB id is built. With the h_id byte the DB id needs no change. I tested MariaDB only.

@razvancrainea

Copy link
Copy Markdown
Member

Hi, Yuri!

That seems to be a different bug - although the table is created as unsigned bigint, the module uses it as a signed - this is not correct (even now) and should be fixed. I'll try to double check who is using bigint signed, and if nobody does, amend the interface to always use bigint as unsigned. I will work on this meanwhile.
The reason why I do not want to put the byte in the h_id is that it lowers the randomness of the dialogs id - this should not be a problem though if different instances use different IDs, but nevertheless, lowers the window for the h_id overlappings. Whereas h_entry is fixed, and highly unlikely to be that big, like ever :).

Best regards,
Răzvan

@Lt-Flash

Copy link
Copy Markdown
Contributor Author

Hi @razvancrainea,

Thanks! I will put the instance in the top byte of h_entry then.

Two points before I write the code:

  1. The replication packets carry only h_id, and the receiver computes h_entry from the Call-ID. So I will add the instance as a new field to the dialog packets and raise the packet version to 6. Is that fine with you?
  2. Shall I limit the instance to 1-127 for now, or allow 1-255 and rely on your unsigned bigint change?

In the dialog itself I plan to keep h_entry as the plain bucket index and store the instance next to it. The two are combined only in the did and in the DB dlg_id.

Thanks again!

@razvancrainea

Copy link
Copy Markdown
Member

Hi, Yuri!
Yes, feel free to increase the version, ideally make it backwards compatible - if you receive a 5 version, don't try to fetch the id.
Also, for now let's limit to 127 the node id, but we should also check this in the mod_init/provisioning and reject broken settings.

Best regards,
Răzvan

@Lt-Flash

Copy link
Copy Markdown
Contributor Author

Hi @razvancrainea,

I have pushed the instance id to this PR, rebased on master.

  • instance_id (0-127) defaults to the node id in dialog_replication_cluster, and can also be set without replication, e.g. for chained instances. 0 keeps today's ids. A bad value, or a node id above 127 with no instance_id set, stops OpenSIPS in mod_init.
  • The instance is the top byte of h_entry. dlg->h_entry holds the whole value, so the did, the DB dlg_id and other modules carry it unchanged. Lookups match the full h_entry, so another instance's dialog is never taken as ours. This replaces my earlier plan of storing it next to h_entry.
  • Packet version 6 adds the instance after h_id. Version 5 packets are still accepted, as instance 0.
  • Upgrading a cluster: as with version 5, old nodes drop version 6 packets. While a cluster is half upgraded, old nodes get none of the dialogs created on upgraded nodes, and a dialog created on an old node stays there until its timeout if it ends through an upgraded node. So a cluster is best upgraded as a whole, or with each node drained first.
  • hash_size is now capped at 2^24, and the DB load skips rows whose hash entry does not fit the table. An older build would index past its table with such rows, so a downgrade must not load dialogs saved with an instance.

I kept the two guard commits: as you said, once the ids cannot collide, a collision means a misconfiguration and can be ignored.

Thanks!

@Lt-Flash
Lt-Flash force-pushed the fix/dialog-repl-id-collision branch from f7a0b72 to e964e8e Compare November 28, 2026 08:05
@Lt-Flash

Copy link
Copy Markdown
Contributor Author

I rebased the branch on the current master (9c442ad) to resolve the conflicts. The only conflict was in modules/dialog/test/test.c, where the new timer wheel tests and test_dlg_instance_id were added in the same places, so I kept both. The code itself is unchanged.

I also checked the dialog changes that landed since my last push (the lifetime and delete-delay timer wheels, dlg_contact_update). They only walk the hash table by index and never read dlg->h_entry, so nothing new needs the bucket mask.

Since the replication format matches dialogs by their id, the update,
value and delete handlers find their dialog by (h_entry, h_id) only, but
the id is unique only on the node that created the dialog. Every node
raises its next_id past the ids it receives, and a dialog gets its id at
the initial INVITE while it is replicated only once answered. So two nodes
creating dialogs in the same hash entry while the first call is still
ringing can give both calls the same id, and the packets of one call are
then applied to the other: contacts and SDPs overwritten (a
topology-hidden BYE was sent to the other call's contact), dialog values
replaced, and a live dialog deleted when the other call hung up.

Check the Call-ID (and the caller tag, where the packet carries it) of the
dialog found by id, and log and drop the packet on a mismatch, as the CSeq
handler already does through match_dialog(). The create handler also logs
such a collision instead of treating the dialog as already created.
When a replicated create hit a local dialog of another call with the same
id, dlg_replicated_create() returned before clearing DLG_FLAG_FROM_DB and
setting DLG_FLAG_SYNCED on it, unlike before the collision check was added.
During a cluster sync the local dialog then looked absent from the sync
data and was dropped at SYNC_DONE: dialogs loaded from the database, and
dialogs without a sharing tag, which always count as needing a sync.

Only log the collision there and keep the previous flag handling.
Nodes replicating dialogs to each other allocate ids on their own, so two
nodes can give the same (h_entry, h_id) to two different calls.

The new instance_id parameter (0-127) goes in the top byte of h_entry, so
ids made by different instances never match. It defaults to the node id in
dialog_replication_cluster; an invalid value stops the startup. h_entry
keeps the hash table index in its low 24 bits, and the did, the DB dlg_id
and the API carry the whole value.

Replication packets go to version 6, which carries the instance after
h_id. Version 5 packets are still accepted and read as instance 0, the
instance of a node without this change.

Upgrading a cluster: nodes without this change drop version 6 packets, as
they did with version 5. While a cluster is half upgraded, the old nodes
get no dialogs created on the upgraded nodes, and a dialog an old node
created stays there until its timeout when it ends through an upgraded
node.
@Lt-Flash
Lt-Flash force-pushed the fix/dialog-repl-id-collision branch from e964e8e to 4c4133f Compare December 1, 2026 09:51
The instance sits in the top byte of h_entry, so a hash table bigger
than 2^24 entries would mix index bits into the instance: refuse such a
hash_size whatever the instance is.

The database load paths used the stored hash entry as a table index
without checking it, so rows saved with a bigger hash_size were read and
written out of bounds. Skip such rows with an error.
@Lt-Flash
Lt-Flash force-pushed the fix/dialog-repl-id-collision branch from 4c4133f to 1e2bcd0 Compare December 1, 2026 18:07
@nathanleyton

Copy link
Copy Markdown

For the curious, we had also hit this in production at heavy peaks. We made our own temporary work around awaiting this solution. We made it so a shared update is ignored unless its Call-ID matches, not as well integrated as this, but it prevents the collisions.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

3 participants