Skip to content

SHM: unlinking the port's named mutex breaks mutual exclusion in open_port_internal; listener ends up permanently deaf on an unlinked segment (is_port_ok still true) #6569

Open

Description

@zhubeilife

Is there an already existing issue for this?

  • I have searched the existing issues

(Closest ones I found: #5469 and #5737 describe similar symptoms — SHM data stops and only a restart//dev/shm cleanup helps — but have no root-cause analysis. #6485 is a different bug, see "Additional context".)

Expected behavior

When a SHM listener's port is reset by a writer (e.g. because the listener was stalled for longer than healthy_check_timeout_ms), the listener should notice that its segment is gone/not ok, call regenerate_port(), and receive data again. Concurrent open_port_internal() calls for the same port id should be serialized by the port's named mutex.

Current behavior

With several writers and a listener that is stalled for 1–2 s from time to time (CPU starvation, SIGSTOP, a debugger…), the listener sometimes ends up permanently deaf although its process is healthy:

  • the listener keeps listening on a segment that has already been unlinked (/proc/<pid>/maps shows the old inode, (deleted)), and in that old segment is_port_ok == 1, num_listeners == 1, is_processing == 0 — so from the listener's point of view nothing is wrong and wait_pop() never calls regenerate_port();
  • the segment that is currently on disk has num_listeners == 0;
  • the writers see no listener, so every send() → cleanup_output_ports() drops the port and the next send re-creates it. Several writers keep creating and deleting each other's freshly created segment (hundreds of Zombie. Reset the port / NOT Healthy / Couldn't find port_node lines per minute), publish() blocks for 5–19 s at a time in find_port() waiting for the named mutex, and the state does not decay after the stall is gone (it lasted the whole 15 min I let it run). Restarting only the deaf listener fixes it.

Root cause (as far as I can tell). open_port_internal() serializes creation/removal of a port segment with the boost named mutex fastrtps_port<N>_mutex. But that mutex is itself unlinked:

  • Port::~Port() does SharedMemSegment::remove(segment_name) and SharedMemSegment::named_mutex::remove(segment_name + "_mutex") when the last reference goes away (SharedMemGlobal.hpp, in ~Port);
  • open_or_create_and_lock_named_mutex() also unlinks and re-creates it when timed_lock times out.

After the name has been unlinked, a process that opens the name again gets a different POSIX semaphore from the one still held by another process that opened it earlier, so the two are no longer mutually exclusive. I polled the inode of /dev/shm/sem.fastrtps_port<N>_mutex: it changes in lock-step with every segment re-creation (up to ~200 times in 10 s during the storm).

Without mutual exclusion the following interleaving is possible. The log lines below and the inode numbers (the deaf listener's segment and its _el file were created back-to-back) are consistent with it, but I have not captured it in a debugger:

  1. listener L (re-generating its port) does create_only of the segment and is still before init_port() constructs the PortNode;
  2. writer W, which is not blocked by the (different) mutex, does open_only on the same name, find<PortNode> returns null → Couldn't find port_node → SharedMemSegment::remove();
  3. L continues happily on the now-unlinked segment (it was fully initialized by L, is_port_ok = true, listener registered, _el lock taken) and never learns that it was removed.

The same missing exclusion explains the other lines in the log: Failed to create port segment …: File exists, Failed to remove …_el / …_sl, fastrtps_port<N>No such file or directory -> ~Port.

Steps to reproduce

ROS 2 Humble, Python, one machine. ROS_DOMAIN_ID=77, and this profile via FASTRTPS_DEFAULT_PROFILES_FILE (SHM + UDP, 1 s health check, which is also the default):

<profiles xmlns="http://www.eprosima.com/XMLSchemas/fastRTPS_Profiles">
  <transport_descriptors>
    <transport_descriptor><transport_id>shm</transport_id><type>SHM</type>
      <segment_size>134217728</segment_size><port_queue_capacity>512</port_queue_capacity>
      <healthy_check_timeout_ms>1000</healthy_check_timeout_ms></transport_descriptor>
    <transport_descriptor><transport_id>udp</transport_id><type>UDPv4</type></transport_descriptor>
  </transport_descriptors>
  <participant profile_name="p" is_default_profile="true"><rtps>
    <useBuiltinTransports>false</useBuiltinTransports>
    <userTransports><transport_id>shm</transport_id><transport_id>udp</transport_id></userTransports>
  </rtps></participant>
</profiles>

pub.py (best-effort, 256 KB messages at 500 Hz; run 4 copies):

import rclpy, time
from rclpy.qos import *
from std_msgs.msg import String
rclpy.init(); n = rclpy.create_node("pub")
q = QoSProfile(history=HistoryPolicy.KEEP_LAST, depth=10, reliability=ReliabilityPolicy.BEST_EFFORT)
p = n.create_publisher(String, "/chat", q); m = String(); m.data = "x" * 262144
t = time.monotonic()
while rclpy.ok():
    p.publish(m); t += 1 / 500; time.sleep(max(0, t - time.monotonic()))

sub.py (run 3 copies; writes how many messages it has received):

import rclpy, os, time
from rclpy.qos import *
from std_msgs.msg import String
rclpy.init(); n = rclpy.create_node("sub%d" % os.getpid()); c = [0]
def cb(_):
    c[0] += 1
    if c[0] % 100 == 0: open("/tmp/sub_%d" % os.getpid(), "w").write("%d %f\n" % (c[0], time.time()))
q = QoSProfile(history=HistoryPolicy.KEEP_LAST, depth=10, reliability=ReliabilityPolicy.BEST_EFFORT)
n.create_subscription(String, "/chat", cb, q); rclpy.spin(n)

Driver: start the 3 subscribers, then the 4 publishers. For ~2 minutes loop: kill -STOP a random non-empty subset of the subscribers, sleep 1.2–2 s, kill -CONT them, sleep 1.5–3 s. Then stop disturbing, wait 60 s, and check whether every /tmp/sub_<pid> keeps growing.

Results (Fast DDS 2.6.10 + the #6485 fix, so that bug is out of the picture; 3 subscribers × 4 publishers; "deaf" = no new message in 20 s after 60 s of quiet):

rounds with a deaf subscriber Couldn't find port_node lines File exists lines
unpatched 2 / 3 263 / 507 / 321 386 / 905 / 604
named_mutex::remove() in ~Port removed 0 / 3 0 / 0 / 0 0 / 0 / 0

With 1 subscriber × 3 publishers (120 s of stalls + 90 s quiet), 4 of 4 unpatched rounds ended in the permanent storm, and 0 of 4 with the change below. A single-line change to the "deaf" subscriber's old segment (is_port_ok = false before removing in the "Zombie" branch) did not help (1 round), consistent with the segment being removed through the Couldn't find port_node branch, which never gets a chance to flag it.

Suggested change (what I tested): do not unlink the named mutex in ~Port:

                             SharedMemSegment::remove(segment_name.c_str());
-                            SharedMemSegment::named_mutex::remove((segment_name + "_mutex").c_str());

Side effects I checked: with the change a clean exit leaves one small sem.fastrtps_port<N>_mutex file per port id (bounded; fastdds shm clean / a reboot removes it); kill -9 while holding the mutex still recovers through the existing timeout branch (~2 s); repeatedly kill -9 of listeners or writers recovers as before; the SHM unit tests I ran one by one pass. I did not examine whether the timeout branch of open_or_create_and_lock_named_mutex() has the same problem, it probably does in the rare case where it fires while another process is merely slow.

Fast DDS version/commit

2.6.10 and 2.6.12 (ROS 2 Humble ros-humble-fastrtps). I read the code of 2.14.x, 3.2.x and master: the named_mutex::remove() in Port::~Port() and the timeout re-creation are identical there, but I did not run the reproducer on those versions.

Platform/Architecture

Ubuntu 22.04 aarch64 (reproduced in a VM). Similar symptoms (listeners stuck on a stale segment, ports re-created every 2–3 s for over an hour) were also seen on an NVIDIA Jetson Orin, 12 cores, load average 27–36, where I have not confirmed that this is the cause.

Transport layer

Default configuration, UDPv4 & SHM

Additional context

XML configuration file

(see above)

Relevant log output

Three publisher processes and three subscribers, RTPS_TRANSPORT_SHM warnings enabled, one second of the storm (uuids shortened):

00:48:52.694 [RTPS_TRANSPORT_SHM Warning] Failed to remove /dev/shm/fastrtps_port26663_sl -> Function test_lock
00:48:52.703 [RTPS_TRANSPORT_SHM Warning] fastrtps_port26663No such file or directory -> Function ~Port
00:48:52.703 [RTPS_TRANSPORT_SHM Warning] Existing Port 26663 (<uuid>) NOT Healthy. -> Function open_port_internal
00:48:52.703 [RTPS_TRANSPORT_SHM Warning] Port 26663 (<uuid>) Removed. -> Function open_port_internal
00:48:52.703 [RTPS_TRANSPORT_SHM Warning] Port 26663 Zombie. Reset the port -> Function open_port_internal
00:48:52.703 [RTPS_TRANSPORT_SHM Warning] fastrtps_port26663No such file or directory -> Function ~Port
00:48:52.703 [RTPS_TRANSPORT_SHM Warning] Port 26663 Couldn't find port_node  -> Function open_port_internal
00:48:52.703 [RTPS_TRANSPORT_SHM Warning] Port 26663 Removed. -> Function open_port_internal
00:48:52.703 [RTPS_TRANSPORT_SHM Warning] Failed to create port segment fastrtps_port26663: File exists -> Function open_port_internal
00:48:52.703 [RTPS_TRANSPORT_SHM Warning] SHM Port 26663 failure: the port is marked as not ok! -> Function try_push
00:48:52.710 [RTPS_TRANSPORT_SHM Warning] fastrtps_port26663No such file or directory -> Function ~Port
00:48:52.711 [RTPS_TRANSPORT_SHM Warning] Existing Port 26663 (<uuid>) NOT Healthy. -> Function open_port_internal
00:48:52.716 [RTPS_TRANSPORT_SHM Warning] Failed to create port segment fastrtps_port26663: File exists -> Function open_port_internal

State of a deaf subscriber (read from /proc/<pid>/mem, the old segment is (deleted) in /proc/<pid>/maps): old segment is_port_ok=1 num_listeners=1 is_processing=0; current segment on disk num_listeners=0; in some runs the subscriber still holds a valid _el lock, in others it holds none.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions