Segfault at process exit after dlclose() of a library using rclcpp: g_rmw_lib destructor unmaps librmw_implementation
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
調査の方向性
Start with rmw_implementation/src/functions.cpp and trace how the RMW library handle is stored, loaded, and reset in get_library() and unload_library(). Reproduce the exit-time crash with the supplied host and ROS library example, then check whether the proposed lifetime change prevents it. Done means the reproducer exits successfully while unload_library() retains its test and benchmark behavior.
索引モデルが issue の本文から書いたものです。
説明
Generated by Generative AI
Claude Opus 5.5 (Anthropic) via Claude Code: investigation (gdb, dynamic loader tracing), reproducer, proposed change, and this text.
Operating System
Linux 7.0.0-28-generic #28-Ubuntu SMP PREEMPT_DYNAMIC x86_64 GNU/Linux
Ubuntu 26.04.1 LTS, glibc 2.43-2ubuntu2.4
Also reproduced in a clean ros:lyrical Docker image after `apt-get upgrade`.
ROS version or commit hash
lyrical: rmw_implementation 3.1.6, rcl_logging_implementation 3.4.1, rclcpp 32.0.3, rmw_fastrtps_cpp 9.4.10
RMW implementation (if applicable)
rmw_fastrtps_cpp (default)
Client library (if applicable)
rclcpp
Steps to reproduce issue
An executable dlopen()s a library that uses rclcpp, dlclose()s it, and returns from main().
host.cpp:
// Loads a library that uses ROS 2, runs it, unloads it, and exits normally.
#include <dlfcn.h>
#include <cstdio>
int main()
{
void * lib = dlopen("./libros_user.so", RTLD_NOW | RTLD_LOCAL);
if (!lib) {
std::fprintf(stderr, "dlopen failed: %s\n", dlerror());
return 2;
}
auto run = reinterpret_cast<int (*)()>(dlsym(lib, "run"));
int result = run();
dlclose(lib);
std::fprintf(stderr, "main returns %d\n", result);
return result;
}
ros_user.cpp:
// Initializes ROS 2, creates a node with a publisher, spins once, and shuts down.
#include <memory>
#include <rclcpp/rclcpp.hpp>
#include <std_msgs/msg/string.hpp>
extern "C" int run()
{
rclcpp::init(0, nullptr);
{
auto node = std::make_shared<rclcpp::Node>("dlclose_repro");
auto pub = node->create_publisher<std_msgs::msg::String>("chatter", 10);
rclcpp::executors::SingleThreadedExecutor executor;
executor.add_node(node);
executor.spin_some();
}
rclcpp::shutdown();
return 0;
}
CMakeLists.txt:
cmake_minimum_required(VERSION 3.20)
project(rmw_dlclose_repro CXX)
find_package(rclcpp REQUIRED)
find_package(std_msgs REQUIRED)
add_library(ros_user SHARED ros_user.cpp)
target_link_libraries(ros_user ${std_msgs_TARGETS} rclcpp::rclcpp)
add_executable(host host.cpp)
docker run --rm -it ros:lyrical bash
apt-get update && apt-get install -y clang && apt-get -y upgrade
# create the three files above in the current directory
source /opt/ros/lyrical/setup.bash
cmake -S . -B build -DCMAKE_CXX_COMPILER=clang++
cmake --build build
cd build && ./host; echo "exit code: $?"
Expected behavior
main returns 0 is printed and the exit code is 0.
Actual behavior
main returns 0 is printed, then the process crashes during exit: Segmentation fault (core dumped), exit code 139. It happens on every run.
Additional information
What happens (gdb, LD_DEBUG=files,bindings, and glibc's elf/dl-close.c):
- When the host calls
dlclose()on its library, glibc cannot unload the ROS 2 libraries yet:librclcpp.sohas a pendingthread_localdestructor registered on the main thread (l_tls_dtor_count > 0), and an object in that state is kept. So the library, rclcpp, and their dependencies, includinglibrmw_implementation.so, stay loaded. - During
exit(), the main thread'sthread_localdestructors run first. After that, the only references keeping these libraries loaded are the plugin libraries opened at runtime:librmw_fastrtps_cpp.so(held byg_rmw_libinrmw_implementation) andlibrcl_logging_spdlog.so(held byrcl_logging_implementation). - The exit handlers run the static destructor of
g_rmw_lib, whichdlclose()slibrmw_fastrtps_cpp.so. Thisdlclose()is not nested in anotherdlclose(), so glibc unloads all objects that are no longer used: the RMW library and the whole group the application closed, includinglibrmw_implementation.so. Whendlclose()returns, execution continues in the unmappedlibrmw_implementation.soand crashes:
Thread 1 "host" received signal SIGSEGV, Segmentation fault.
#0 0x00007ffff7a34b01 in ?? () <- in librmw_implementation.so, already unmapped
...
#4 __run_exit_handlers (...) at ./stdlib/exit.c:118
(0x...4b01 is the return address after rcpputils::SharedLibrary::~SharedLibrary() in the release of g_rmw_lib.)
rcl_logging_implementation has the same problem in its atexit(unload_logging_library) handler, which dlclose()s librcl_logging_spdlog.so and can unmap librcl_logging_implementation.so the same way. I have not reported it in ros2/rcl_logging yet; the fix there is analogous (keep the handle in storage that is never destroyed and drop the atexit() registration), and I can open an issue there as well. In this reproducer that handler runs before the g_rmw_lib destructor:
- With both libraries unpatched, the process crashes when returning into
unload_logging_library(). Theg_rmw_libdestructor runs inside thatdlclose(), as part of unloading the group. - With only
rcl_logging_implementationfixed, the process crashes in theg_rmw_libdestructor as shown above. - With only
rmw_implementationfixed, it still crashes inunload_logging_library().
Both changes are needed.
Observations:
- Not reproducible on Jazzy (Ubuntu 24.04, rmw_implementation 2.15.6) with the same reproducer.
- Not reproducible with the May 2026 Lyrical packages in the
ros:lyricalimage (rclcpp 32.0.0, rmw_implementation 3.1.5); reproducible afterapt-get upgrade. The change that exposes it in this reproducer is ros2/rclcpp#3219 (b723878c, first released in rclcpp 32.0.3): itsstatic thread_local std::unordered_set<const Context *> g_contexts_in_shutdownincontext.cppregisters athread_localdestructor on the thread that callsrclcpp::shutdown(). That registration is the only one in the process and is what keepslibrclcpp.soloaded after the host'sdlclose()(step 1 above). When that registration is suppressed (LD_PRELOADinterposer for__cxa_thread_atexit_impl), the reproducer exits with 0. #3219 itself is not wrong; it only delays the unload to process exit, where the exit-time unload of the plugin libraries crashes. Other applications reach the same state without #3219, whenever something else keeps the ROS 2 libraries loaded after the application'sdlclose(). - Not reproducible when the library is built with GCC. The GCC-built library contains
STB_GNU_UNIQUEsymbols, which make glibc mark it as non-unloadable, so its dependencies are never unloaded either. - With
target_link_libraries(ros_user rclcpp::rclcpp ${std_msgs_TARGETS})(rclcpp first), it does not reproduce: message typesupport libraries that are still open at exit then resolve some C++ typeinfo symbols tolibrclcpp.so, which keeps rclcpp andlibrmw_implementation.soloaded. elf/dl-close.cis functionally the same in glibc 2.39 and 2.43.- We first hit this in a real application (unit tests that load ROS 2 modules at runtime, built with clang).
Proposed solution
Do not unload the RMW library during process exit: keep the handle in a function-local static that is never destroyed. unload_library() (used by tests and benchmarks) still resets it.
--- a/rmw_implementation/src/functions.cpp
+++ b/rmw_implementation/src/functions.cpp
@@ -45,7 +45,17 @@
#define STRINGIFY_(s) #s
#define STRINGIFY(s) STRINGIFY_(s)
-static std::shared_ptr<rcpputils::SharedLibrary> g_rmw_lib = nullptr;
+// The RMW library handle is intentionally never destroyed, so the RMW library is not unloaded
+// by a static destructor at process exit. During exit, the RMW library can be the last object
+// keeping this library loaded (e.g. after the application dlclose()d the library that uses
+// ROS 2), and unloading it from a static destructor of this library would then unmap this
+// library while the destructor is still running.
+static std::shared_ptr<rcpputils::SharedLibrary> &
+rmw_lib()
+{
+ static auto * lib = new std::shared_ptr<rcpputils::SharedLibrary>();
+ return *lib;
+}
static std::shared_ptr<rcpputils::SharedLibrary>
attempt_to_load_one_rmw(const std::string & library)
@@ -132,10 +142,11 @@ load_library()
std::shared_ptr<rcpputils::SharedLibrary>
get_library()
{
- if (!g_rmw_lib) {
- g_rmw_lib = load_library();
+ auto & lib = rmw_lib();
+ if (!lib) {
+ lib = load_library();
}
- return g_rmw_lib;
+ return lib;
}
void *
@@ -1036,5 +1047,5 @@ unload_library()
symbol_rmw_take_dynamic_message = nullptr;
symbol_rmw_take_dynamic_message_with_info = nullptr;
symbol_rmw_serialization_support_init = nullptr;
- g_rmw_lib.reset();
+ rmw_lib().reset();
}
Tested on Ubuntu 26.04 with the lyrical branch of rmw_implementation and rcl_logging:
| reproducer | exit codes (3 runs) |
|---|---|
| unpatched | 139 139 139 |
| only rmw_implementation patched | 139 139 139 (crash in unload_logging_library()) |
| only rcl_logging_implementation patched | 139 139 139 (crash after g_rmw_lib release) |
| both patched | 0 0 0 |
rmw_implementation tests and linters pass (38 tests, 0 failures). In test_rmw_implementation, test_subscription__rmw_fastrtps_cpp take_with_info_publisher_gid_for_uint8_sequence_type fails with and without this change.
Trade-offs and alternatives:
- If an application unloads
librmw_implementationitself (for example, a host that unloads and reloads a module using ROS 2), the RMW library stays loaded after it. Each such cycle keeps one reference to the RMW library. unload_library()keeps its current behavior; it is only used by tests and benchmarks.- Alternatives: load the RMW library with
RTLD_NODELETE, or skip the reset only during process teardown. Both need changes to howrcpputils::SharedLibraryis used.
Happy to open a PR if the approach is acceptable.
- 主要言語
- C++
- スター
- 25
- フォーク
- 66
- 平均マージ
- 5日 17時間
- マージ済み PR(30日)
- 8
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートなし
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
ros2/rmw_implementation のほかの issue
-
:farmer: ignored warn_unused_result return values in test_event_message_lost_deadlock.cpp対応中かも @clalancette が 2 日前に担当しました。 オープンbug
ros2/rmw_implementation#320 · 担当者 1 名 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 48/100
ros2/rmw_implementation#207 · コメント 2 件 ·
メンテナーはふだん 1 日以内に返信
-
`rmw_implementation`, it `<depend>`s on `rmw_implementation_cmake`再び着手できるかも @ahcorde が 2310 日前に担当しましたが、オープン中のプルリクエストはありません。 オープン
ros2/rmw_implementation#100 · コメント 1 件 · 担当者 1 名 ·
メンテナーはふだん 1 日以内に返信
ros2/rmw_implementation の issue をすべて見る
似ている issue
-
arch-based bug niri
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
noctalia-dev/noctalia#5552 ·
メンテナーはふだん 1 日以内に返信
-
bug
難易度 1/5 1〜3時間 初心者へのやさしさ 82/100
EchoTools/nevr-runtime#2912 ·
メンテナーはふだん 1 日以内に返信
-
area/docdb kind/failing-test priority/medium status/awaiting-triage
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
yugabyte/yugabyte-db#39462 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
meshtastic/firmware#12519 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 79/100
メンテナーはふだん 1 日以内に返信