diff --git a/src/backend/CMakeLists.txt b/src/backend/CMakeLists.txt index d6ac83bd9..5057f786f 100644 --- a/src/backend/CMakeLists.txt +++ b/src/backend/CMakeLists.txt @@ -95,22 +95,14 @@ install(TARGETS automotive_simulator # ---------------------------------------- # Simulator-runner library # -# Right now this can't be made shared - segfaults. For more -# information, refer to: -# -# https://github.com/ToyotaResearchInstitute/delphyne/issues/341 -# include(${PROJECT_SOURCE_DIR}/cmake/Python.cmake) add_library(simulation_runner - STATIC interactive_simulation_stats.cc simulation_run_stats.cc simulation_runner.cc system.h ) -# Position independent code can disappear once we are able to drop STATIC -set_property(TARGET simulation_runner PROPERTY POSITION_INDEPENDENT_CODE ON) set_target_properties(simulation_runner PROPERTIES OUTPUT_NAME ${PROJECT_NAME}-simulation-runner) target_include_directories( simulation_runner diff --git a/src/backend/simulation_runner.cc b/src/backend/simulation_runner.cc index c7e3a7fef..0241c4e0a 100644 --- a/src/backend/simulation_runner.cc +++ b/src/backend/simulation_runner.cc @@ -81,10 +81,18 @@ SimulatorRunner::SimulatorRunner( ignerr << "Error advertising service [" << kSceneRequestServiceName << "]" << std::endl; } - // Initializes the python machinery so we can invoke a python callback - // function on each simulation step. - Py_Initialize(); - PyEval_InitThreads(); + + // The get_internals() function initializes the `internals.tstate` for + // subsequent `gil_scoped_acquire` calls. If the gil_scoped_acquire is + // attempted to be called before having initialized the internals.tstate, + // it would end up into a segmentation fault. This initialization should + // only occur if the SimulatorRunner class is instantiated from within a + // python script, which is checked in the if statement. + // In comment below, a similar approach to the one used here is suggested: + // https://github.com/pybind/pybind11/issues/1360#issuecomment-385988887 + if (Py_IsInitialized()) { + pybind11::detail::get_internals(); + } // Tell the simulator to run steps as fast as possible, as are handling the // sleep (if required) between steps.