Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 21 Apr 2005 13:39:37 -0700 (PDT)
From:      Luke Dean <LukeD@pobox.com>
To:        Philip Hallstrom <freebsd@philip.pjkh.com>
Cc:        freebsd-questions@freebsd.org
Subject:   Re: Reusing a port after a crash
Message-ID:  <20050421133314.F6889@border.crystalsphere.multiverse>
In-Reply-To: <20050421130012.V86918@wolf.pjkh.com>
References:  <20050421123644.U6491@border.crystalsphere.multiverse> <20050421130012.V86918@wolf.pjkh.com>

next in thread | previous in thread | raw e-mail | index | archive | help
>> From time to time, my torrent filesharing application will crash or need 
>> to be killed.  The application is configured to listen on a specific port. 
>> If I try to restart the application after improper termination, I receive a 
>> fatal error message stating that the port is already in use.
>> 
>> Before restarting the application, I verify that the application did indeed 
>> shut down (via 'ps -aux') and 'sockstat -l' shows that no application is 
>> using the port in question, so I don't understand how the port could still 
>> be 'in use'.
>> 
>> The only way I've found to restart the application after improper 
>> termination is to reboot the whole system.  Is there something else I could 
>> try?  Does this kind of thing tend to happen to network applications when 
>> they crash or could this be specific to my application?
>
> What little tinkering I've done in the socket world reminds me of this:
>
> --------------------------------------------------------------
> % man setsockopt
>
> ...
>
> SO_REUSEADDR       enables local address reuse
> SO_REUSEPORT       enables duplicate address and port bindings
>
> ...
>
> SO_REUSEADDR indicates that the rules used in validating addresses sup-
> plied in a bind(2) call should allow reuse of local addresses.
> SO_REUSEPORT allows completely duplicate bindings by multiple processes
> if they all set SO_REUSEPORT before binding the port.  This option per-
> mits multiple instances of a program to each receive UDP/IP multicast or
> broadcast datagrams destined for the bound port.
> --------------------------------------------------------------
>
>
> Basically, what I remember about this is that if you supply those options 
> when creating the socket and your app crashes, you can start it back up using 
> the same host:port otherwise you get the message you're getting...
>
> I'm sure a good networking would be able to explain it better...

Thanks!  That's what I needed to know.  I didn't know such options 
existed.

I poked around in the code for the app, and even though I know next to 
nothing about python I found that there's a "reuse" parameter on the call 
that binds the listening port, and it's defaulting to "false".  I bet if 
I explicitly set it to "true" that I'll get the behavior I want (or break 
the whole thing...)
Open Source is fun.  :)

Luke Dean



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20050421133314.F6889>