From 37d5e3e0df62d321f847f241a213f42db2b2d8c8 Mon Sep 17 00:00:00 2001 From: Nico Weber Date: Mon, 10 Aug 2020 14:18:26 -0400 Subject: [PATCH] Shell: Fix fd leak with pipes Fixes the problem reported in #3073. While trying to write a test for this, I thought I'd use Shell -c 'for i in $(seq 100) { echo $i }' | head -n 1 but that makes the cpu spin at 100% and doesn't terminate even with this fix here. But at least piping disasm into head now works. --- Shell/Shell.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Shell/Shell.cpp b/Shell/Shell.cpp index ef865cf21a..99ba9c4327 100644 --- a/Shell/Shell.cpp +++ b/Shell/Shell.cpp @@ -535,6 +535,10 @@ RefPtr Shell::run_command(const AST::Command& command) perror("dup2(run)"); return nullptr; } + // dest_fd is closed via the `fds` collector, but rewiring.other_pipe_end->dest_fd + // isn't yet in that collector when the first child spawns. + if (rewiring.other_pipe_end && close(rewiring.other_pipe_end->dest_fd) < 0) + perror("close other pipe end"); } fds.collect();