Skip to content

Fix program hang by closing pipe in the parent process#552

Closed
CHN-beta wants to merge 1 commit intoboostorg:developfrom
CHN-beta:fix-pipe-close
Closed

Fix program hang by closing pipe in the parent process#552
CHN-beta wants to merge 1 commit intoboostorg:developfrom
CHN-beta:fix-pipe-close

Conversation

@CHN-beta
Copy link
Copy Markdown
Contributor

@CHN-beta CHN-beta commented Apr 25, 2026

Before this patch, pipe file descriptors were not properly closed after fork was called. This causes the program to hang infinitely under specific conditions.

The following code could be used to reproduce the issue:

# include <boost/process.hpp>
# include <boost/asio.hpp>
# include <iostream>
# include <format>

int main()
{
  namespace bp = boost::process;
  boost::asio::io_context context;
  boost::filesystem::path actual_program = bp::environment::find_executable("echo");
  auto env = bp::environment::current();
  auto stdout_pipe = std::make_unique<boost::asio::readable_pipe>(context);
  std::string stdout_string;
  bp::process_stdio stdio{ .in = {}, .out = *stdout_pipe, .err = {} };
  auto proc = bp::process
    (context, actual_program, { "hhh" }, std::move(stdio), std::move(env));
  proc.wait();
  boost::system::error_code ec;
  boost::asio::read(*stdout_pipe, boost::asio::dynamic_buffer(stdout_string), ec);
  std::cout << std::format("{} {}\n", proc.exit_code(), stdout_string);
}

Compile the code with pidfd disabled (either by setting the BOOST_PROCESS_V2_DISABLE_PIDFD_OPEN macro or by using an old kernel header like 3.10), the program will hang after starting.

strace gives:

wait4(1763335, 0x7ffe3342ebc0, WNOHANG, NULL) = 0
wait4(1763335, [{WIFEXITED(s) && WEXITSTATUS(s) == 0}], 0, NULL) = 1763335
--- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=1763335, si_uid=1000, si_status=0, si_utime=0, si_stime=0} ---
write(9, "\21\0\0\0", 4)                = 4
rt_sigreturn({mask=[]})                 = 1763335
read(6, "hhh\n", 512)                   = 4
read(6

As shown in the strace output, the child process successfully exits, but the parent process blocks indefinitely on read(6). The root cause is that the writing-end of the pipe (fd 7) is still open in the parent process, even it has been closed in the child process (since the child process have died) and should never be used in the parent process. This make the read operation on reading-end of the pipe (fd 6) remains blocked waiting for EOF.

@klemens-morgenstern
Copy link
Copy Markdown
Collaborator

This might be a bug with process_stdio not moving correctly. Does it work when you change the process_stdio to this:

  auto proc = bp::process
    (context, actual_program, { "hhh" },  bp::process_stdio{ .in = {}, .out = *stdout_pipe, .err = {} }, std::move(env));

Otherwise, this change is just wrong, because you're just closing everything. You can open an issue if the above doesn't work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants