I have an application that has two threads. One thread forks on an event and other thread is doing something which results into a lot of getaddrinfo() calls. I am facing an issue where in some cases forked child hangs on a mutex inside getaddrinfo() call.
Now I understand that this only happens when child is forked while second thread in the parent process is in getaddinfo() call. This results into a deadlock because the child inherits the parent's lock but never releases it.
My question is how can one go about this issue when a system call itself is taking a lock? Well, one way I can fix it is by synchronizing the fork system call and getaddrinfo call in both the threads. But is there any other way to go about it?
Thanks!
I ended up using pthread_atfork to register the pre/post-fork handlers that would lock/unlock respectively. I guess that is the only way forward in this case.
I just wish that man pages of these calls (like getaddrinfo) mention that they take locks internally so that programmers don't fall into this pitfall.