[Freeswitch-users] [Solved] FS shutdown not closing TCP port causes TIME_WAIT problems
mario_fs at mgtech.com
Tue Apr 3 20:17:45 MSD 2012
I am posting this in case someone else runs into this, especially since I found it may not be Mac OSX specific (I was able to duplicate on SUSE although not as much).
Both of the following conditions must exist:
1. You have at least one TCP local extension. In this case, due to Bria, etc. on an iPad, using TCP reduced battery usage since no need to run in background.
2. FreeSwitch is recycled very quickly, in this case down/up within 30 seconds because it was automated at 2am each day. I needed it due to a long running sofia memory leak on OSX which may be resolved but I need to test.
Problem: The extension using TCP cause a TIME_WAIT that keeps the 5060 port active (but NOT open) on the TCP stack, in my case it was 30 seconds but I found the time could be different on other platforms. When FreeSwitch comes back up the "internal.xml" profile fails because port 5060 is in use. If FS is brought up AFTER the TIME_WAIT goes away there is no problem, but in my case it was very quick each night to minimize the outage.
Solution: I changed the OSX plist that auto restarts FS to run a script that starts FS, this makes no difference except a script is more flexible. Rather than use an arbitrary sleep time I check for port 5060 which is more reliable and faster. I tested this many times using different scenarios and all worked. Note I tried to use the "lsof -i TCP:5060" command but it shows only OPEN ports so it won't work.
while : ; do
a=$(/usr/sbin/netstat -p tcp)
if [[ "$a" != *sip* ]] ; then
echo "Port 5060 still in use - waiting for it to free up"
/usr/local/freeswitch/bin/freeswitch -nc -nonat
# a=$(/usr/sbin/lsof -i TCP:5060) <- only shows open ports
# if [ "$a" = "" ] ; then
On Apr 3, 2012, at 8:53 AM, Mario G wrote:
> For anyone who may run into this see "FS shutdown not closing TCP port causes TIME_WAIT problems" for follow up and solution.
> On Apr 2, 2012, at 1:33 PM, Mario G wrote:
>> That's what I was thinking, trying to figure out how now. Will post a solution when I get one.
>> On Apr 2, 2012, at 1:28 PM, Anthony Minessale wrote:
>>>> From what I see, sofia uses the REUSEADDR flag so I will bet its yet
>>> another MAC specific thing.
>>> I am not really sure what you can do since you tend to find issues
>>> deep inside libsofia-sip and its portability to OSX.
>>> Maybe make your restart script sleep a bit?
>>> su_setreuseaddr(socket, 1);
>>> On Mon, Apr 2, 2012 at 3:13 PM, Mario G <mario_fs at mgtech.com> wrote:
>>>> If FS is shutdown and started too quickly, the internal profile ip/port stays in use and FS will fail to start it. After searching on the wen I found many references to TIME_WAIT but all fixes were to the program source. So I am wondering if when FS/sofia shuts down they are not releasing the ip/port for internal. If it's true, I found some source changes to solve it. Google "TIME-WAIT" and you'll find lots of good stuff like http://serverfault.com/questions/329845/how-to-forcibly-close-a-socket-in-time-wait. Apparently there is no way to free up the port manually. Help/comments anyone?
>>>> Mario G
>>>> Professional FreeSWITCH Consulting Services:
>>>> consulting at freeswitch.org
>>>> Official FreeSWITCH Sites
>>>> FreeSWITCH-users mailing list
>>>> FreeSWITCH-users at lists.freeswitch.org
>>> Anthony Minessale II
-------------- next part --------------
An HTML attachment was scrubbed...
Join us at ClueCon 2011 Aug 9-11, 2011
More information about the FreeSWITCH-users