Skip to content

Commit

Permalink
Merge pull request #290 from robotology/fix/prevent_tensorflow_segfault
Browse files Browse the repository at this point in the history
Force loading tensorflow shared libraries to prevent segfault
  • Loading branch information
diegoferigo authored Feb 9, 2021
2 parents fff3918 + cba57f6 commit 33cb234
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/cicd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ jobs:
run: |
apt-get update
apt-get install -y --no-install-recommends \
python3-numpy libxml2-dev coinor-libipopt-dev libeigen3-dev libassimp-dev swig
libxml2-dev coinor-libipopt-dev libeigen3-dev libassimp-dev swig
# =============
# Build project
Expand Down
37 changes: 37 additions & 0 deletions bindings/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,49 @@ def setup_gazebo_environment() -> None:
os.environ["IGN_GAZEBO_SYSTEM_PLUGIN_PATH"] = ign_gazebo_system_plugin_path


def preload_tensorflow_shared_libraries() -> None:

# Check if tensorflow is installed
import importlib.util
spec = importlib.util.find_spec("tensorflow")

if spec is None:
return

# Get the tensorflow __init__ location
import pathlib
init = pathlib.Path(spec.origin)

# Get the tensorflow top-level folder
tensorflow_dir = init.parent
assert tensorflow_dir.is_dir()

# Get the tensorflow/python folder
tensorflow_python_dir = tensorflow_dir / "python"
assert tensorflow_python_dir.is_dir()

# Load the main shared library
for lib in tensorflow_dir.glob("*tensorflow*.so*"):
import ctypes
ctypes.CDLL(str(lib))

# Load all the shared libraries inside tensorflow/python
for lib in tensorflow_python_dir.glob("_*.so"):
import ctypes
ctypes.CDLL(str(lib))


def import_gazebo() -> None:

# Check the the module was never loaded by someone else
if "scenario.bindings._gazebo" in sys.modules:
raise ImportError("Failed to load ScenarI/O Gazebo with custom dlopen flags")

# Preload the shared libraries of tensorflow if the package is installed.
# If tensorflow is imported after scenario.bindings.gazebo, the application segfaults.
if os.environ.get("SCENARIO_DISABLE_TENSORFLOW_PRELOAD") != "1":
preload_tensorflow_shared_libraries()

# Import SWIG bindings
# See https://github.com/robotology/gym-ignition/issues/7
# https://stackoverflow.com/a/45473441/12150968
Expand Down
3 changes: 1 addition & 2 deletions python/gym_ignition/rbd/idyntree/inverse_kinematics_nlp.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,7 @@ def set_current_robot_configuration(self,

q = rbd.idyntree.numpy.FromNumPy.to_idyntree_dyn_vector(array=joint_configuration)

if not self._ik.setCurrentRobotConfiguration(baseConfiguration=H,
jointConfiguration=q):
if not self._ik.setCurrentRobotConfiguration(H, q):
raise RuntimeError("Failed to set the current robot configuration")

if not self._floating_base:
Expand Down

0 comments on commit 33cb234

Please sign in to comment.