I'd say, yes.
If the intent is to do exec
right after, some people argue that it doesn't matter (after all you can simply close all fds you didn't want inherited by the child process). See e.g. discussion in this thread https://github.com/klemens-morgenstern/boost-process/issues/65
I agree logically speaking, but then I had spurious deadlocks in asynchronous operations after forking/exec in a process. Sadly, I'm not at all sure that doing the notify_fork
always helps. At some point I had a safe_io_service
object that made sure all io_service
instances in the process were registered and would be notify_fork
-ed at appropriate times, while also synchronizing fork operations.¹
All in all, doing notify_fork(prepare)
and notify_fork(parent)
seems like it shouldn't hurt², it should allow any services that really hold precious services to do the right thing.
Even with those I could still get deadlocks occurring on asynchronous operations. The hottest lead I ever had to explaining the problem was this: epoll
is fundamentally broken.
TL;DR
In the end I had to ship a version with hand-rolled fork/exec³ and manual asynchronous IO loops instead of using Asio.
- We were still actively using Asio for RPC socket listeners though,
- the
pthread_atfork
method of ensuring synchronized fork()
calls with io_service::notify_fork()
calls was in place
and we didn't see problems using it that way.
¹ We used pthread_atfork
to actually trigger this
² although I would take precautions to make that synchronized (so no two threads would fork simultaneously). Just to be safe
³ Instead of using Boost Process which also uses Asio for the async IO parts. Leading to less, and more elegant, code of course