Can someone elaborate on the safety comment in `nix::unistd::fork`

Hi all, I am trying to understand the safety comment here:
In a multithreaded program, only [async-signal-safe](https://man7.org/linux/man-pages/man7/signal-safety.7.html) functions like `pause` and `_exit` may be called by the child (the parent isn’t restricted). Note that memory allocation may **not** be async-signal-safe and thus must be prevented.

I thought async signal safe referred to a function which can be called safely within a signal handler. But the child process is not being invoked within a signal handler right? Also, assuming the child process is not being invoked within a signal handler (I am not sure if that is even a meaningful statement), is it not enough for the child process's signal handler itself to be async signal safe?

The level of uncertainty about the state of the child process is very similar to the level of uncertainty in a signal handler. For example, if one of the other threads in the parent was in the middle of a malloc call at the time of the fork, the allocator's state is probably in a state that prevents it from working properly in the child.

3 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.