Skip to content

Commit

Permalink
Allow reuse address when firing up the ros2cli daemon.
Browse files Browse the repository at this point in the history
Especially in tests where the daemon is being repeatedly
created and destroyed, it can take some time for the kernel
to actually allow the address to be rebinded (even after the
old process has exited).  This can lead to some situations
where we fail to spawn the daemon.

To fix this, set "allow_reuse_address" inside the LocalXMLRPCServer,
which will set SO_REUSADDR on the socket.

Signed-off-by: Chris Lalancette <clalancette@gmail.com>
  • Loading branch information
clalancette committed Nov 18, 2024
1 parent 8766c33 commit e8f06ed
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion ros2cli/ros2cli/xmlrpc/local_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import socket
import struct
# Import SimpleXMLRPCRequestHandler to re-export it.
Expand All @@ -32,7 +33,12 @@ def get_local_ipaddrs():

class LocalXMLRPCServer(SimpleXMLRPCServer):

allow_reuse_address = False
# Allow re-binding even if another server instance was recently bound (i.e. we are still in
# TCP TIME_WAIT). This is already the default behavior on Windows, and further SO_REUSEADDR can
# lead to undefined behavior on Windows; see
# https://learn.microsoft.com/en-us/windows/win32/winsock/using-so-reuseaddr-and-so-exclusiveaddruse. # noqa
# So we don't set the option for Windows.
allow_reuse_address = False if os.name == 'nt' else True

def server_bind(self):
# Prevent listening socket from lingering in TIME_WAIT state after close()
Expand Down

0 comments on commit e8f06ed

Please sign in to comment.