Hi,
We had the same problem. As you noted, this is due to a race where the initial INVITE is processed by a different worker thread than the re-INVITE.
We solved this by introducing a (very!) small asynchronous delay in handling all reinvites, around 30 ms or so. That was lazy and probably could be improved by using 'table' or 'dialog' variables to track whether the reinvite received is the first reinvite for a given dialog, and only delay it then.
Regardless, a litany of features is available to make this easy:
https://kamailio.org/docs/modules/5.2.x/modules/async.html#async.f.async_sle... https://kamailio.org/docs/modules/5.2.x/modules/rtimer.html https://kamailio.org/docs/modules/5.2.x/modules/mqueue.html
Though I take no pleasure in saying it, this is probably the most fruitful solution.
-- Alex