Allocation was pushed before forking in order to avoid potential
deadlocking when forking while multiple threads are running. This
deadlocking is possible when a thread (other than the one forking) has
acquired a lock and didn't get around to releasing it before the fork.
This leaves the lock in a locked state in the resulting process with no
hope of it ever being released.
Signed-off-by: Brandon Williams <redacted>
---
run-command.c | 8 ++++++++
1 file changed, 8 insertions(+)
diff --git a/run-command.c b/run-command.c
index 84c63b209..2b3249de4 100644
--- a/run-command.c
+++ b/run-command.c
@@ -458,6 +458,14 @@ int start_command(struct child_process *cmd)
argv_array_pushv(&argv, cmd->argv);
}
+ /*
+ * NOTE: In order to prevent deadlocking when using threads special
+ * care should be taken with the function calls made in between the
+ * fork() and exec() calls. No calls should be made to functions which
+ * require acquiring a lock (e.g. malloc) as the lock could have been
+ * held by another thread at the time of forking, causing the lock to
+ * never be released in the child process.
+ */
cmd->pid = fork();
failed_errno = errno;
if (!cmd->pid) {--
2.12.2.715.g7642488e1d-goog