Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 28 additions & 3 deletions include/gz/sim/components/Model.hh
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@
#ifndef GZ_SIM_COMPONENTS_MODEL_HH_
#define GZ_SIM_COMPONENTS_MODEL_HH_

#include <memory>
#include <mutex>
#include <string>

#include <sdf/Model.hh>
#include <sdf/Root.hh>
#include <sdf/parser.hh>

#include <gz/sim/components/Factory.hh>
#include <gz/sim/components/Component.hh>
Expand Down Expand Up @@ -103,12 +106,34 @@ namespace serializers
return _in;
}

// Its super expensive to create an SDFElement for some reason
sdf::Root root;
sdf::Errors errors = root.LoadSdfString(sdf);
{
// It's super expensive to create an sdf::SDFPtr object.
// Workaround this by making it a static object so we only initialize it
// once.
// https://github.com/gazebosim/sdformat/issues/1478
sdf::Errors errors;
static std::mutex mutex;
static sdf::SDFPtr sdfParsed;
Copy link
Contributor

@arjo129 arjo129 Oct 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Simply adding static to a mutex may or maynot work. I can see failures on mac OS where the mutex is throwing an exception in our tests. Apparently according to this issue on another project, this may have to do with destruction. I dont think itll impact UX but it'll make our tests more flaky.

In hind-sight perhaps #3070 may be a better option for future releases of gazebo ( GZ K and up)

Copy link
Contributor Author

@iche033 iche033 Oct 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I saw the test failure and haven't been able to resolve it yet. I wasn't able to reproduce the crash locally on my mac. I'm also testing different workarounds and rerunning homebrew builds and will see if I have any luck.

Yeah, we can retarget #3070 to main as it causes a behavior change.

std::lock_guard<std::mutex> lock(mutex);
if (!sdfParsed)
{
sdfParsed = std::make_shared<sdf::SDF>();
sdf::init(sdfParsed);
}
auto config = sdf::ParserConfig::GlobalConfig();
// Read an SDF string, and store the result in sdfParsed.
if (!sdf::readString(sdf, config, sdfParsed, errors))
{
gzwarn << "Unable to read SDF while deserializing sdf::Model "
<< sdf << std::endl;
return _in;
}
root.Load(sdfParsed, config);
}
if (!root.Model())
{
gzwarn << "Unable to deserialize sdf::Model " << sdf<< std::endl;
gzwarn << "Unable to deserialize sdf::Model " << sdf << std::endl;
return _in;
}

Expand Down
Loading