Why preexec_fn in subprocess.Popen may lead to deadlock?

It is … complicated.

On Unix processes are spawned in two steps. First a program clones itself (fork, see man fork(2)), then it loads and runs an executable image (execv*, see man exec(3)). If the program calls any non-async-signal-safe function between fork and exec, it can lead to a deadlock. The prexec_fn is called between fork and exec (hence the name) and it can call unsafe function like malloc.

https://man7.org/linux/man-pages/man7/signal-safety.7.html

1 Like