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 <[email protected]>
  • Loading branch information
clalancette committed Nov 18, 2024
1 parent 8766c33 commit 7cb8b24
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 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,15 @@ 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.
if os.name == 'nt':
allow_reuse_address = False
else:
allow_reuse_address = True

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

0 comments on commit 7cb8b24

Please sign in to comment.