Skip to content

[macOS] SHM transport wakes readers on a ~5.7 ms polling tick: Boost.Interprocess falls back to spin_semaphore on Apple #6639

Open

Description

@idesign0

Is there an already existing issue for this?

  • I have searched the existing issues

Expected behavior

With the SHM transport, a DataReader in another process on the same host wakes as soon as a sample is written. On macOS that should take tens of microseconds, the same as with UDPv4 on the same Mac (50–110 µs, below).

Current behavior

On macOS, a DataReader in another process only notices new SHM data on a fixed tick of about 5.7 ms, whatever the publish rate. As a result:

  • Latency: median ~2.9 ms, p99 ~5.7 ms, from 50 Hz to 5 kHz.
  • Bursts: at 2 kHz, samples arrive in groups of ~11 every ~5.7 ms.
  • Loss with KEEP_LAST 1: all but the newest sample of each burst are replaced before the reader runs: 2000 Hz published, 354–450 Hz received.
  • Idle CPU: an idle reader process uses ~5 % of a core polling. Cyclone DDS on the same machine uses ~0.
  • Collapse under overload: from 100 kHz upward (Float64, reader KEEP_LAST 100), the reader receives 0.0–1.5 % of samples with 0.1–2.9 s latency. That held in every run, including SHM-only (FASTDDS_BUILTIN_TRANSPORTS=SHM). Over UDPv4 alone the same rates deliver 37–51 %.

With SHM removed (FASTDDS_BUILTIN_TRANSPORTS=UDPv4), the same test gives 78–87 µs median and no loss.

Root cause

The stack. sample on the reader process shows the SHM listener thread (dds.shm.<port>) here for ~95 % of samples:

eprosima::fastdds::rtps::SharedMemGlobal::Port::wait_pop
 RobustInterprocessCondition::timed_wait / do_timed_wait
  boost::interprocess::ipcdetail::try_based_timed_lock<lock_to_wait<spin_semaphore>>
   boost::interprocess::spin_wait::yield()
    nanosleep

How the bundled Boost (master) gets there:

  1. thirdparty/boost/include/boost/interprocess/detail/workaround.hpp
    • On __APPLE__ it defines BOOST_INTERPROCESS_BUGGY_POSIX_PROCESS_SHARED, because Darwin's pthread_cond stores a mutex pointer inside the condition, so BOOST_INTERPROCESS_POSIX_PROCESS_SHARED is not set.
    • It also leaves BOOST_INTERPROCESS_POSIX_UNNAMED_SEMAPHORES unset ("MacOsX declares _POSIX_SEMAPHORES but sem_init returns ENOSYS").
  2. sync/interprocess_semaphore.hpp uses POSIX semaphores only when both macros are set. Otherwise it falls back to spin/semaphore.hpp, and interprocess_mutex likewise falls back to spin_mutex.
  3. RobustInterprocessCondition blocks each waiter on a bi::interprocess_semaphore (SemaphoreNode::sem). On macOS that is the spin semaphore: try_based_timed_lock → spin_wait::yield().
  4. Once the yield-only budget of one system tick is used up, spin_wait alternates thread_yield() and thread_sleep_tick(). On POSIX, thread_sleep_tick() is nanosleep(get_system_tick_ns() / 2) (detail/os_thread_functions.hpp).
  5. On macOS, sysconf(_SC_CLK_TCK) is 100. That gives a 10 ms tick and a 5 ms sleep request, and nanosleep(5 ms) measured 6.04–6.11 ms mean / 7.1–9.3 ms max on this machine. That sleep is the observed ~5.7 ms tick, and its average half-period is the ~2.9 ms median latency.

Causation check. I injected a nanosleep interposer (DYLD_INSERT_LIBRARIES) that shortens only the 5 ms request. That alone moves latency and loss, with no other change to Fast DDS:

Sleep per poll Received of 2000 Hz (KEEP_LAST 1) Latency p50 / p99 Idle reader CPU
5 ms (stock) 377 Hz 0.93 / 5.56 ms 5.6 %
1 ms 919 Hz 0.28 / 1.09 ms 6.3 %
200 µs 1992 Hz 0.13 / 0.31 ms 10.2 %
50 µs 1864 Hz 0.06 / 1.16 ms 19.0 %

Shorter sleeps only trade latency for CPU, so a real blocking primitive is needed (see possible fixes below).

Steps to reproduce

Two processes on one Mac with default transports, ROS 2 rclcpp, std_msgs/msg/Float64, BEST_EFFORT. Data-sharing is not involved: there are no DataSharing frames in the profile.

repro.cpp (run repro sub 1 and repro pub 1; the argument is the KEEP_LAST depth)
#include <algorithm>
#include <chrono>
#include <cstdio>
#include <cstring>
#include <memory>
#include <vector>
#include <rclcpp/rclcpp.hpp>
#include <std_msgs/msg/float64.hpp>
using namespace std::chrono;
static double now_us() {return duration<double, std::micro>(steady_clock::now().time_since_epoch()).count();}
int main(int argc, char ** argv)
{
  rclcpp::init(argc, argv);
  const bool pub = argc > 1 && !strcmp(argv[1], "pub");
  const int depth = argc > 2 ? atoi(argv[2]) : 1;
  auto node = std::make_shared<rclcpp::Node>(pub ? "repro_pub" : "repro_sub");
  auto qos = rclcpp::QoS(depth).best_effort();
  rclcpp::Publisher<std_msgs::msg::Float64>::SharedPtr p;
  rclcpp::Subscription<std_msgs::msg::Float64>::SharedPtr s;
  rclcpp::TimerBase::SharedPtr t;
  std::vector<double> lat;
  if (pub) {  // 2 kHz, payload = send time
    p = node->create_publisher<std_msgs::msg::Float64>("repro", qos);
    t = node->create_wall_timer(500us, [&] {std_msgs::msg::Float64 m; m.data = now_us(); p->publish(m);});
  } else {    // print rate and latency every 2 s
    s = node->create_subscription<std_msgs::msg::Float64>("repro", qos, [&](std_msgs::msg::Float64::ConstSharedPtr m) {
      lat.push_back(now_us() - m->data);});
    t = node->create_wall_timer(2s, [&] {
      if (lat.empty()) {return;}
      std::sort(lat.begin(), lat.end());
      printf("received %.0f Hz  latency p50 %.0f us  p99 %.0f us\n", lat.size() / 2.0, lat[lat.size() / 2], lat[lat.size() * 99 / 100]);
      fflush(stdout); lat.clear();});
  }
  rclcpp::spin(node);
  rclcpp::shutdown();
}

Full benchmark, scripts and the root-cause tools (nanosleep shim, os_sync_wait_on_address demo): https://github.com/idesign0/ros2-macos-rmw-bench

All numbers below are from Release (-O3) builds, on a quiet machine, with ranges over repeated runs.

Reproducer output. ROS 2 Kilted (Fast DDS 3.2.5), 2 kHz publisher:

RMW / transport KEEP_LAST Received Latency p50 / p99
Fast DDS, default (SHM + UDPv4) 1 354–402 Hz 0.54–0.96 / 5.2–9.8 ms
Fast DDS, FASTDDS_BUILTIN_TRANSPORTS=UDPv4 1 1995 Hz 82–87 / 172–204 µs
Cyclone DDS 1 1998–2000 Hz 85–97 / 151–163 µs

Same behaviour on both versions. Separate benchmark, std_msgs Float64 at 2 kHz, KEEP_LAST 1 unless noted:

Fast DDS Transport Received Latency p50 / p99 Arrival gap p50 / p99
2.14.7 (Jazzy) default 450 Hz 2.04 / 5.54 ms 0.07 / 5.80 ms
2.14.7 (Jazzy) default, KEEP_LAST 100 7996 / 8000 2.86 / 5.65 ms 2 µs / 5.74 ms (bursts)
2.14.7 (Jazzy) SHM only, KEEP_LAST 100 8000 / 8000 2.86 / 5.68 ms 2 µs / 5.75 ms (bursts)
2.14.7 (Jazzy) UDPv4 only 1999 Hz 78 / 284 µs 500 / 651 µs
3.2.5 (Kilted) default 428 Hz 1.50 / 5.53 ms 0.07 / 5.80 ms
3.2.5 (Kilted) default, KEEP_LAST 100 7980–7999 / 8000 2.86–2.89 / 5.64–6.27 ms 2 µs / 5.74 ms (bursts)
3.2.5 (Kilted) SHM only, KEEP_LAST 100 7998 / 8000 2.85 / 5.63 ms 2 µs / 5.73 ms (bursts)
3.2.5 (Kilted) UDPv4 only 2000 Hz 79 / 122 µs 500 / 560 µs

Sustained rate between two processes (Fast DDS 3.2.5, Float64, KEEP_LAST 100, sleep_until-paced publisher):

Publish rate Default (SHM + UDPv4): received, p50 / p99 UDPv4 only: received, p50 / p99
5 kHz 100 %, 2.9 / 5.7 ms 100 %, 55 / 116 µs
20 kHz 99.9 %, 3.1 / 5.7 ms 100 %, 48 / 103 µs
50 kHz 96.0 %, 3.5 / 5.8 ms 100 %, 82 µs / 20 ms
100 kHz 1.5 %, 0.25 / 0.61 s 51 %, 80 / 82 ms
flat out (~117–128 kHz) 0.0 %, 1.6 / 2.9 s 41 %, 79 / 81 ms

For reference on the same Mac:

  • Cyclone DDS (UDP): 100 % at 50 kHz (22 / 52 µs).
  • Cyclone DDS + iceoryx: 100 % at 200 kHz (10 / 27 µs).
  • Zenoh with batching off: 100 % up to its ~90 kHz publisher limit (53 / 97 µs).

The tick does not depend on rate. Fast DDS 3.2.5, default transports, Float64, KEEP_LAST 100:

Publish rate Latency p50 / p99
50 Hz 3.28 / 5.90 ms
200 Hz 2.83 / 5.66 ms
2000 Hz 2.86 / 5.64 ms
5000 Hz 2.92 / 5.69 ms

Fast DDS version/commit

  • Measured: 2.14.7 (ROS 2 Jazzy) and 3.2.5 (ROS 2 Kilted).
  • Code paths on master: RobustInterprocessCondition.hpp, the bundled Boost workaround.hpp, interprocess_semaphore.hpp and os_thread_functions.hpp are as described above.

Platform/Architecture

Other. Please specify in Additional context section.

Transport layer

Shared Memory Transport (SHM)

Additional context

Platform: macOS 27.0.1 (Darwin 27.0.0), Apple Silicon arm64 (MacBook Pro M2, Mac14,7), Apple clang. ROS 2 built from source via the Homebrew tap idesign0/homebrew-ros2.

Possible fixes (from least to most invasive):

  1. Futex-style semaphore on Apple (suggested). macOS 14.4+ has public, cross-process wait/wake on a word in shared memory: <os/os_sync_wait_on_address.h>, functions os_sync_wait_on_address, os_sync_wait_on_address_with_timeout and os_sync_wake_by_address_any/all, used with OS_SYNC_WAIT_ON_ADDRESS_SHARED / OS_SYNC_WAKE_BY_ADDRESS_SHARED.
    • What it replaces: only SemaphoreNode::sem in RobustInterprocessCondition (one counter per waiter, already in shared memory).
    • Shape of the change:
      • wait: a CAS-decrement loop, blocking with os_sync_wait_on_address_with_timeout(&count, 0, 4, SHARED, OS_CLOCK_MACH_ABSOLUTE_TIME, ns) while the count is 0;
      • post: fetch_add followed by os_sync_wake_by_address_any(&count, 4, SHARED);
      • older macOS: guard with __builtin_available(macOS 14.4, *) and keep the current fallback.
    • Measured here: a standalone two-process test (shared memory via shm_open/mmap, 2 kHz) woke the waiter in 5.9–9.7 µs p50 / 16–26 µs p99 over three runs. The waiter used 1–1.5 % CPU, and nothing while idle.
  2. Named POSIX semaphores (sem_open, which works on macOS) per waiter slot. They block in the kernel and work on older macOS, but the names need cleanup when a process crashes, which RobustInterprocessCondition would have to handle.
  3. A kernel "doorbell" per port, such as a FIFO or AF_UNIX datagram socket: the writer sends a byte after pushing to the ring buffer, and the reader blocks in read/kevent. Portable and robust, but one extra syscall per notification.
  4. Stop-gap only: have thread_sleep_tick() sleep much less than tick/2 on Apple (Fast DDS already patches its bundled Boost via BOOST_FASTDDS_PATCHES). The table above shows the CPU cost, so this only reduces the latency.

Separately, possibly unrelated to SHM: with RMW_FASTRTPS_PUBLICATION_MODE=ASYNCHRONOUS and UDPv4 only, publish() failed with cannot publish data (rmw_publish.cpp:68) and aborted the publisher at 5 kHz (Float64, KEEP_LAST 1). Happy to open a separate issue if useful.

interprocess_mutex is also spin_mutex on Apple, so contended SHM mutexes can hit the same 5 ms sleep. That's not measured separately here.

Workarounds for users today:

  • Disable SHM: FASTDDS_BUILTIN_TRANSPORTS=UDPv4, or an XML profile with only UDPv4.
  • ROS 2 caveat: under ROS_AUTOMATIC_DISCOVERY_RANGE=LOCALHOST, rmw_fastrtps sets use_builtin_transports = false and adds SHM itself (rmw_fastrtps_shared_cpp/src/participant.cpp, Jazzy and Kilted). The variable is then ignored, and I measured the same 316–332 Hz / 5 ms behaviour in that mode.
  • Deeper KEEP_LAST queue: avoids the loss but not the latency.
  • One process (ROS 2 composition): load publisher and subscriber as components into the same component_container with use_intra_process_comms: true, so samples don't cross processes and SHM is not used. Fast DDS 2.14.7, 2 kHz, 64 B: 1989 Hz received, latency 14 / 40 µs p50 / p99. In one process without intra-process comms, Fast DDS delivery also avoids the tick: 2000 Hz, 25 / 48 µs. This only helps for nodes that can share a process.

XML configuration file

No XML. Default transports (ROS 2 rmw_fastrtps_cpp defaults), ROS_AUTOMATIC_DISCOVERY_RANGE=SUBNET.

Relevant log output

No response

Network traffic capture

No response

Activity

  1. MiguelCompany commented on Nov 18, 2026

    @MiguelCompany
    Member

    @idesign0 Could you check whether #6632 fixes this?

  2. idesign0 commented on Nov 18, 2026

    @idesign0
    Author

    @MiguelCompany

    Thanks! Yes, #6632 fixes it on my setup.

    I applied #6632 on top of v3.2.5 (the version in ROS 2 Kilted) and built it with the same configuration as the installed library (HAVE_SECURITY 0). I also built plain v3.2.5 the same way as a baseline, and swapped each in under the same ROS 2 Kilted install (rmw_fastrtps_cpp, default transports, two processes, std_msgs/Float64, M2, macOS 27.0.1):

    v3.2.5 v3.2.5 + #6632
    2 kHz, KEEP_LAST 1: received 1074–1333 Hz 1814–1984 Hz
    2 kHz, KEEP_LAST 1: latency p50 2.3–2.5 ms ~100 µs
    2 kHz, KEEP_LAST 100: latency p50 / p99 2.81 / 5.6 ms 35 / 109–462 µs
    2 kHz: arrival gap p50 / p99 2 µs / 5.7 ms (bursts) 500 / 557–691 µs
    20 kHz received / p99 96.7 % / 30 ms 100 % / 357 µs
    50 kHz received 94.0 % 99.6 %
    Idle subscriber CPU (20 s) 1.15 s 0.38 s

    Mixed versions behave as described in the PR: an old publisher with a new subscriber gives 593 µs p50, and a new publisher with an old subscriber stays at 2.8 ms.

    Two observations that may be separate:

    • Overload: from 100 kHz upward the reader still receives only 0.4–2.1 % (KEEP_LAST 100), with and without the patch, so the overload collapse from the issue description seems to have a different cause.
    • Depth-1 tail: with KEEP_LAST 1 and both sides patched, p99 latency is ~1.07 ms in every window, which matches the 1 ms cap. Some wake-ups might still be missed and only picked up by the timeout.

    Scripts: https://github.com/idesign0/ros2-macos-rmw-bench

  3. added 7 commits that reference this issue on Nov 20, 2026
    f5a0742
    dcc7c0a
    2f4ef8c
    c64374c
    093b0e0
    956924d
    106534b
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