Skip to content

Keep the logging library loaded until the process exits - #152

Closed
jp-pino wants to merge 1 commit into
ros2:rollingfrom
jp-pino:keep-logging-library-loaded
Closed

jp-pino wants to merge 1 commit into
ros2:rollingfrom
jp-pino:keep-logging-library-loaded

Conversation

@jp-pino

@jp-pino jp-pino commented Nov 20, 2026

Copy link
Copy Markdown

Since the logging implementation is loaded at runtime, a process that uses spdlog itself and logs through rcl_logging_spdlog dies with SIGSEGV (exit code 139) after main() has returned. That happens with or without rclcpp::shutdown(), so it hits every node of a program that also uses spdlog for its own logging.

Cause

  • rcl_logging_implementation unloads the logging library from an atexit() handler, which it registers when logging is first initialized.
  • rcl_logging_spdlog starts spdlog's periodic flush thread with spdlog::flush_every() and registers its logger with spdlog. That code lives in the plugin: flush_every() and the periodic_worker constructor are templates, and the plugin creates the sink.
  • In a process that uses spdlog itself, for example one that logs before rclcpp::init(), spdlog's registry is created before the handler is registered, so its static destructors run after the handler. They stop the flush thread and destroy the logger after the library is gone.
  • When rcl linked the logging implementation directly, the implementation was never unloaded.

Fix

  • The library now stays loaded until the process exits: g_logging_lib is never destroyed, and there's no atexit() handler any more.
  • Unloading at exit freed nothing that the OS doesn't free anyway. rcl_logging_external_shutdown() already keeps the library loaded "to avoid race conditions".
  • The tests still unload it with force_unload_logging_library() to switch implementations.

Test

test_exit_with_spdlog uses death tests. A child process that uses spdlog logs through rcl_logging_spdlog and exits, once after rcl_logging_external_shutdown() and once without it.

It's its own executable because the other tests unload the library between tests. That isn't safe once spdlog outlives the library: the flush thread from one test would crash the next.

Tested against Lyrical's packages (Ubuntu 26.04, arm64, libspdlog 1.15.3):

10 runs each without the fix with the fix
TestExitWithSpdlog.exit_after_shutdown 0/10, killed by signal 11 10/10
TestExitWithSpdlog.exit_without_shutdown 0/10, killed by signal 11 10/10

With the fix, all of the package's tests and linters pass (41 tests, 5 skipped).

The same change on Lyrical's 3.4.1, with the stock rcl_logging_spdlog, makes our programs exit cleanly too: exit code 0 in 20 of 20 runs, with and without shutdown, against 139 before. So we'd like to ask for a backport to lyrical.

🤖 Generated with Claude Code

rcl_logging_implementation unloads the logging library from an atexit()
handler, but objects that the library creates can outlive that handler
and run its code later in exit(). rcl_logging_spdlog starts spdlog's
periodic flush thread and registers its logger with spdlog. In a process
that uses spdlog itself, spdlog's registry is created before the handler
is registered, so its static destructors run after it: they stop the
flush thread and destroy the logger after the library is gone, and the
process dies with SIGSEGV after main() has returned, with or without
rcl_logging_external_shutdown(). When rcl linked the logging
implementation directly, it was never unloaded.

Keep the library loaded until the process exits. The tests still unload
it with force_unload_logging_library() to switch implementations.

Add test_exit_with_spdlog: a process that uses spdlog logs through
rcl_logging_spdlog and exits, after a shutdown and without one.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Juan Pablo Pino Bravo <[email protected]>
@jp-pino

jp-pino commented Nov 20, 2026

Copy link
Copy Markdown
Author

Closing in favour of #150, which makes the same change and came first. The crash this PR was about, and its test, are described there: #150 (comment)

@jp-pino jp-pino closed this Nov 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant