Use RTLD_NODELETE when loading the render engine plugin#1280
Use RTLD_NODELETE when loading the render engine plugin#1280iche033 merged 2 commits intogazebosim:mainfrom
Conversation
Repeated unloadEngine/engine cycles dlopen and dlclose the engine plugin. Some transitive dependency (libgomp on Ubuntu Noble + rotary nightly packages) uses thread-local storage; without RTLD_NODELETE, each reload allocates from glibc's static-TLS surplus, which is not reliably reclaimed on dlclose. After ~10 cycles the surplus is exhausted and the next dlopen fails with "cannot allocate memory in static TLS block" Pass _noDelete=true to gz-plugin's Loader::LoadLib so dlclose keeps the library mapped. TLS is allocated once on first load and reused on every subsequent reload, eliminating the leak. Trade-off: the plugin and its transitive deps remain resident for the lifetime of the process. For a rendering engine this is the expected lifetime anyway. Fixes gazebosim#1265 Generated-by: Claude Opus 4.7 Signed-off-by: Taylor Howard <taylorhoward@me.com>
|
thanks for tracking this down. Verified that the changes fix the issue in local testing. |
|
would be good to get another pair of eyes on this to ensure this is safe, maybe @azeey |
azeey
left a comment
There was a problem hiding this comment.
Thanks for investigating this! The explanation makes sense, but I don't know why the test passes on Jenkins (e.g. https://build.osrfoundation.org/view/gz-rotary/job/gz_rendering-ci-main-noble-amd64/198/), but fails on Github Actions.
I think we can live with the trade-offs. Other than tests, I don't think we have a common use case where we want the rendering engine to be repeatedly loaded/unloaded. We do have some some variables with static storage. Checking with nm -SlC lib/libgz-rendering-ogre2.dylib | grep -F ' b ' | v, I see the following non-const variables that will not be reinitialized:
gz::rendering::v11::Ogre2RenderTarget::TargetFSAA(unsigned char)::ogre2FSAAWarn
gz::rendering::v11::Ogre2DynamicRenderable::CreateDynamicMesh()::dynamicRenderableId
gz::rendering::v11::Ogre2GaussianNoisePass::CreateRenderPass()::gaussianNodeCounter
gz::rendering::v11::Ogre2DepthGaussianNoisePass::CreateRenderPass()::gaussianDepthNodeCounter
and the following static initializers that affect singletons from
:global_Ogre2LensFlarePassFactory
global_Ogre2GaussianNoisePassFactory
@iche033 will these affect tests?
Overall, we've done this type of fix in other places (e.g. gazebosim/gz-sim#1649), so I think it's okay to merge even if there are still unanswered questions.
Good point. I don't think it'll affect the the tests and typical usage of gz-rendering. But I ticketed an issue to track this: #1285 |
|
@Mergifyio backport gz-rendering10 gz-rendering9 gz-rendering8 |
✅ Backports have been createdDetails
|
Repeated unloadEngine/engine cycles dlopen and dlclose the engine plugin. Some transitive dependency (libgomp on Ubuntu Noble + rotary nightly packages) uses thread-local storage; without RTLD_NODELETE, each reload allocates from glibc's static-TLS surplus, which is not reliably reclaimed on dlclose. After ~10 cycles the surplus is exhausted and the next dlopen fails with "cannot allocate memory in static TLS block" Pass _noDelete=true to gz-plugin's Loader::LoadLib so dlclose keeps the library mapped. TLS is allocated once on first load and reused on every subsequent reload, eliminating the leak. Trade-off: the plugin and its transitive deps remain resident for the lifetime of the process. For a rendering engine this is the expected lifetime anyway. Fixes #1265 Generated-by: Claude Opus 4.7 Signed-off-by: Taylor Howard <taylorhoward@me.com> (cherry picked from commit fb9dd4c)
Repeated unloadEngine/engine cycles dlopen and dlclose the engine plugin. Some transitive dependency (libgomp on Ubuntu Noble + rotary nightly packages) uses thread-local storage; without RTLD_NODELETE, each reload allocates from glibc's static-TLS surplus, which is not reliably reclaimed on dlclose. After ~10 cycles the surplus is exhausted and the next dlopen fails with "cannot allocate memory in static TLS block" Pass _noDelete=true to gz-plugin's Loader::LoadLib so dlclose keeps the library mapped. TLS is allocated once on first load and reused on every subsequent reload, eliminating the leak. Trade-off: the plugin and its transitive deps remain resident for the lifetime of the process. For a rendering engine this is the expected lifetime anyway. Fixes #1265 Generated-by: Claude Opus 4.7 Signed-off-by: Taylor Howard <taylorhoward@me.com> (cherry picked from commit fb9dd4c)
Repeated unloadEngine/engine cycles dlopen and dlclose the engine plugin. Some transitive dependency (libgomp on Ubuntu Noble + rotary nightly packages) uses thread-local storage; without RTLD_NODELETE, each reload allocates from glibc's static-TLS surplus, which is not reliably reclaimed on dlclose. After ~10 cycles the surplus is exhausted and the next dlopen fails with "cannot allocate memory in static TLS block" Pass _noDelete=true to gz-plugin's Loader::LoadLib so dlclose keeps the library mapped. TLS is allocated once on first load and reused on every subsequent reload, eliminating the leak. Trade-off: the plugin and its transitive deps remain resident for the lifetime of the process. For a rendering engine this is the expected lifetime anyway. Fixes #1265 Generated-by: Claude Opus 4.7 Signed-off-by: Taylor Howard <taylorhoward@me.com> (cherry picked from commit fb9dd4c)
🦟 Bug fix
Fixes #1265
Summary
Repeated
unloadEngine/enginecycles dlopen and dlclose the engine plugin. A transitive dependency in the plugin's chain (libgompon Ubuntu Noble + rotary nightly packages, vialibgz-common-graphics→libassimpand friends) uses thread-local storage. WithoutRTLD_NODELETE, each reload allocates from glibc's static-TLS surplus, which is not reliably reclaimed ondlclose. After ~10 cycles the surplus is exhausted and the nextdlopenfails with:This regression appeared on
mainafter #1246 switched CI to the rotary alias packages, which (via thegz-commonrebuild that swapped FreeImage for vendored STB ingz-common#803) no longer transitively pulllibfreeimage→libraw→libgompinto the test binary's startupDT_NEEDED. The static-TLS exhaustion was latent before that change —libfreeimage's dep chain was anchoringlibgompin the main binary's TLS region for free.The fix passes
_noDelete=truetogz::plugin::Loader::LoadLib, which gates theRTLD_NODELETEflag that's already supported by gz-plugin's loader. With this flag,dlclosekeeps the library mapped, finalizers don't run, andTLS slots aren't released. TLS is allocated once on first load and reused on every subsequent reload, eliminating the surplus leak regardless of which library in the plugin's chain is the TLS hog.
Reproduction
REGRESSION_reload_engine_ogre2_gl3plusreproduces the failure 100% of the time inside a freshubuntu:nobledocker container withgzdev repository enable --project=rotary(matching whatgazebo-tooling/action-gz-ci@nobledoes). Before this PR: 5 cases fail with the static-TLS error. After this PR: 8/8 cases pass.Trade-off
The engine plugin and its transitive dependencies remain mapped for the lifetime of the process. For a rendering engine this is effectively the process's lifetime anyway,
Ogre::Rootis created and destroyed byOgre2RenderEngineat the C++ level, separately from library load/unload, so callingunloadEnginefollowed byengine()still produces a freshOgre::Root. What changes:Alternatives considered
-Wl,--no-as-needed -lgompinto the test executable (initial attempt): works but only patches the test, doesn't fix the underlying reload bug for downstream consumers, GCC-specific, and only handles libgomp.GLIBC_TUNABLES=glibc.rtld.optional_static_tls=N: runtime-only, delays exhaustion rather than fixing it, and requires CI-environment plumbing.NEEDEDchain: the chain is largely load-bearing (the plugin uses ~80 symbols fromlibgz-common-graphics); not a productive direction.RTLD_NODELETEis the only option that fixes the bug for everygz::rendering::engine()consumer and not just this one test.Checklist
codecheckpassed (See contributing)ubuntu:nobledocker container withgzdevrotary packages, the previously-failingREGRESSION_reload_engine_ogre2_gl3plusnow passes (5.18 s, 8/8 cases).Generated-by: Claude Code
Note to maintainers: Remember to use Squash-Merge and edit the commit message to match the pull request summary while retaining
Signed-off-byandGenerated-bymessages.