We have a C++ shared library that uses ZeroC\'s Ice library for RPC and unless we shut down Ice\'s runtime, we\'ve observed child processes hanging on random mutexes. The Ice r
I consider this a bug in the programs calling fork(). In a multi-threaded process, the child process should call only async-signal-safe functions. If a program wants to fork without exec, it should do so before creating threads.
There isn't really a good solution for threaded fork()/pthread_atfork(). Some chunks of it appear to work, but this is not portable and liable to break across OS versions.
Congratulations, you found a defect in the standard. pthread_atfork
is fundamentally unable to solve the problem it was created to solve with mutexes, because the handler in the child is not permitted to perform any operations on them:
One potential workaround is to use POSIX semaphores in place of mutexes here. A semaphore does not have an owner, so if the parent process locks it (sem_wait
), both the parent and child processes can unlock (sem_post
) their respective copies without invoking any undefined behavior.
As a nice aside, sem_post
is async-signal-safe and thus definitely legal for the child to use.