Gazebo | Ignition | Community
Ask Your Question

Cannot use fork() with anything that links Gazebo common

asked 2019-08-08 15:51:47 -0500

Jenny B gravatar image


I'm trying to use fork to start Gazebo and another process together in a test. However, linking against gazebo_common causes the forked child process to hang. Here is a minimal example:

#include "gazebo/common/Time.hh"

int main(int argc, char** argv) {
    int pid = fork();
    if (pid == 0) {
        printf("in child process %d\n", getpid());
    } else {
        printf("in parent process %d\n", getpid());
        gazebo::common::Time time;

If I run this I find that the child process never dies. If I do not include the instantiation of gazebo::common::Time then the child process dies as I would expect.

I'm using a from-source install of gazebo10 on gazebo10_10.0.0 but also saw this problem with the source install of gazebo9.

This behavior is what I would expect if there were threading in the program but I'm not including any threading in the above. Does simply linking against gazebo_common pull in something that spawns threads?

Has anyone else noticed this or can reproduce?


edit retag flag offensive close merge delete


I'd also be interested in any solution for launching a process from within a Gazebo TestFixture that doesn't use fork :)

Jenny B gravatar imageJenny B ( 2019-08-09 06:09:52 -0500 )edit

This appears to have to do with the ModelDatabase singleton.

Jenny B gravatar imageJenny B ( 2019-08-23 11:12:02 -0500 )edit

2 Answers

Sort by ยป oldest newest most voted

answered 2019-08-09 10:49:03 -0500

chapulina gravatar image

On gazebo_ros_pkgs we're using fork() in tests. Maybe the example helps:

edit flag offensive delete link more


None of those files actually pull in any Gazebo dependencies in their headers.

Note that the gazebo_main does use fork, but I suspect that's why the process always has to escalate to sigkill for the client.

Jenny B gravatar imageJenny B ( 2019-08-12 08:03:53 -0500 )edit

answered 2019-08-09 06:29:02 -0500

Jenny B gravatar image

This is not exactly an answer to the original question, but for anyone else encountering the same problem, I found I could use popen instead of fork and things worked (interesting since AFAIK popen uses fork underneath).

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower


Asked: 2019-08-08 15:51:47 -0500

Seen: 70 times

Last updated: Aug 09 '19