Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions ros2cli/ros2cli/node/daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,10 @@ def is_daemon_running(args):

def _is_daemon_address_free():
# Mirror LocalXMLRPCServer.allow_reuse_address: SO_REUSEADDR on
# non-Windows so TIME_WAIT doesn't make us falsely report busy.
# all platforms so TIME_WAIT doesn't make us falsely report busy.
try:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
if os.name != 'nt':
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(daemon.get_address())
return True
except socket.error as e:
Expand Down Expand Up @@ -140,6 +139,9 @@ def spawn_daemon(args, timeout=None, debug=False):
`False` if it was already running.
:raises: if it fails to spawn the daemon.
"""
if is_daemon_running(args):
return False

# Acquire socket by instantiating XMLRPC server.
try:
server = daemon.make_xmlrpc_server()
Expand Down
8 changes: 2 additions & 6 deletions ros2cli/ros2cli/xmlrpc/local_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import socket
# Import SimpleXMLRPCRequestHandler to re-export it.
from xmlrpc.server import SimpleXMLRPCRequestHandler # noqa
Expand All @@ -33,11 +32,8 @@ def get_local_ipaddrs():
class LocalXMLRPCServer(SimpleXMLRPCServer):

# 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
# TCP TIME_WAIT).
allow_reuse_address = True

def verify_request(self, request, client_address):
if client_address[0] not in get_local_ipaddrs():
Expand Down
7 changes: 5 additions & 2 deletions ros2cli/test/test_ros2cli_daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,16 +116,19 @@ def noop_execute_callback(goal_handle):
action_name=TEST_ACTION_NAME,
execute_callback=noop_execute_callback
)
action_server # to avoid "assigned by never used" warning
action_client = rclpy.action.ActionClient(
node=node,
action_type=test_msgs.action.Fibonacci,
action_name=TEST_ACTION_NAME
)
action_client # to avoid "assigned by never used" warning

yield node

# Teardown: explicitly destroy the node to make sure that any
# lingering middleware resources are freed (specifically sockets)
action_client.destroy()
action_server.destroy()
node.destroy_node()

@pytest.fixture(scope='module')
def daemon_node():
Expand Down
10 changes: 8 additions & 2 deletions ros2cli/test/test_strategy.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,25 @@
from ros2cli.node.daemon import spawn_daemon
from ros2cli.node.strategy import NodeStrategy


@pytest.fixture
def enforce_no_daemon_is_running():
# Setup phase: enforce no daemon running
if is_daemon_running(args=[]):
assert shutdown_daemon(args=[], timeout=5.0)
yield

# Teardown phase: enforce no daemon left over
if is_daemon_running(args=[]):
assert shutdown_daemon(args=[], timeout=5.0)

@pytest.fixture
def enforce_daemon_is_running():
# Setup phase: enforce the daemon is running
if not is_daemon_running(args=[]):
assert spawn_daemon(args=[], timeout=5.0)
yield
# Teardown phase: enforce no daemon left over
if is_daemon_running(args=[]):
assert shutdown_daemon(args=[], timeout=5.0)


def test_with_daemon_running(enforce_daemon_is_running):
Expand Down
8 changes: 5 additions & 3 deletions ros2multicast/test/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,17 @@ def target():
t = threading.Thread(target=target)
t.start()
time.sleep(0.1)
send(sent_data, **tx_kwargs)
t.join()
try:
send(sent_data, **tx_kwargs)
finally:
t.join()
return received_data


def test_api():
sent_data = b'test_api'

rx_kwargs = {'timeout': 1.0}
rx_kwargs = {'timeout': 0.2}
tx_kwargs = {}

assert sent_data == _send_receive(sent_data, rx_kwargs, tx_kwargs)
Expand Down