Skip to content

Commit c29b396

Browse files
peffgitster
authored andcommitted
run-command: forbid using run_command with piped output
Because run_command both spawns and wait()s for the command before returning control to the caller, any reads from the pipes we open must necessarily happen after wait() returns. This can lead to deadlock, as the child process may block on writing to us while we are blocked waiting for it to exit. Worse, it only happens when the child fills the pipe buffer, which means that the problem may come and go depending on the platform and the size of the output produced by the child. Let's detect and flag this dangerous construct so that we can catch potential bugs early in the test suite rather than having them happen in the field. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c5eadca commit c29b396

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

run-command.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,12 @@ int finish_command(struct child_process *cmd)
561561

562562
int run_command(struct child_process *cmd)
563563
{
564-
int code = start_command(cmd);
564+
int code;
565+
566+
if (cmd->out < 0 || cmd->err < 0)
567+
die("BUG: run_command with a pipe can cause deadlock");
568+
569+
code = start_command(cmd);
565570
if (code)
566571
return code;
567572
return finish_command(cmd);

0 commit comments

Comments
 (0)