Mercury109 created an issue (kamailio/kamailio#4169)
### Description
I loop through the active dialogs in a timer functions and based on some conditions, a dialog may be ended by setting a short timeout or using the dlg_bye function. **Sometimes** when a dialog times out or the dlg_bye("all") function is used, the BYE is not being sent in both directions, only towards the caller.
There's a warning:
send_bye(): dlg [25641:71143] with callid XXXXXXXXX already in deleted state, BYE not sent.
I noticed this check was added in the commit below:
https://github.com/kamailio/kamailio/commit/d411c6ea4358e6d9dd367349e877ed5f...
Is there a race condition between marking the dialog as terminated and sending the BYE?
Not sending a BYE towards the callee when it should is much worse than sending an unnecessary BYE as it causes billing issues. If this can't be resolved to be 100% reliable, it's safer to remove this check all together.
### Additional Information
* **Kamailio Version** - output of `kamailio -v`
``` version: kamailio 5.7.6 (x86_64/linux)) ```
* **Operating System**:
Debian GNU/Linux 12 (bookworm)
henningw left a comment (kamailio/kamailio#4169)
Thanks for the report. From the linked commit it looks that the dialog is already deleted for some reasons. As you stated, if the BYE is only send in one direction, maybe the dialog is already deleted during the first BYE send out, and depending on the exact timing, its then not send out to the other party. This would be a race condition, the dialog should be only deleted after the second BYE was send out (just guessing right now, have not checked the code).
miconda left a comment (kamailio/kamailio#4169)
Looking at the code, it might have been the case when the response of the first BYE (towards the caller) is received before the second one (towards the callee) is sent out. I pushed a commit on devel version (referenced above), can you try it?
Mercury109 left a comment (kamailio/kamailio#4169)
Thanks for the quick replies, I tested with master and looks good.
You're right, the BYE towards the callee is going out 7ms after the loopback BYE sometimes and instantly other times. The two calls compared were identical scenarios so I'm not sure why that happens. 
If we can add this to 5.7, that would be great. Thanks again!
miconda left a comment (kamailio/kamailio#4169)
I will check and backport to 5.7 if not breaking other things.
Closing as fixed.
Closed #4169 as completed.