Boost.Process wait_for_exit(child): crash?

Boost.Process wait_for_exit(child): crash?

本文关键字:crash child exit Process wait for Boost      更新时间:2023-10-16

我使用的是 Boost.Process 的 0.5 版。可在此处找到文档。我正在使用Mac OS X Yosemite。

的问题:我正在启动编译作为子进程。我想等待该过程完成。

当我的子进程正确编译时,一切都很好。

但是当我的子进程不编译时,我的代码在调用boost::process::wait_for_exit时似乎崩溃了。

我的用户代码如下所示:

编辑

:代码已被编辑以匹配最新,更正确的版本(仍然不起作用)。

s::error_code ec{};
bp::child child = bp::execute(bpi::set_args(compilationCommand),
              bpi::bind_stderr(outErrLog_),
              bpi::bind_stdout(outErrLog_),
              bpi::inherit_env(),
              bpi::set_on_error(ec));
bool compilationSuccessful = true;
if (!ec) {
    s::error_code ec2;
    bp::wait_for_exit(child, ec2);
  if (ec2)
    compilationSuccessful = false;
}

bp::wait_for_exit的内部实现:

template <class Process>
inline int wait_for_exit(const Process &p, boost::system::error_code &ec)
{ 
   pid_t ret;
   int status;
   do
   {
     ret = ::waitpid(p.pid, &status, 0); 
   } while ((ret == -1 && errno == EINTR) || (ret != -1 && !WIFEXITED(status)));
   if (ret == -1) {
       BOOST_PROCESS_RETURN_LAST_SYSTEM_ERROR("waitpid(2) failed");
   }
   else
       ec.clear();
   return status;
}

当我的编译命令失败时,永远不会到达::waitpid之后的代码。显示的错误是:"子项已退出;PID: xxxx;UID:yyy;退出值:1"。

问题:

  1. 这是一个错误还是我滥用了boost::process::wait_for_exit.
  2. 避免我得到的可移植崩溃的任何解决方法?

只是看你的代码,让我印象深刻的第一件事是,直到你调用 wait_for_exit() 之后,你才真正测试说 execute() 是否成功的 "ec" 变量。如果您使用无效的子进程调用 wait_for_exit(),那么它会崩溃是完全可以理解的。

在调用 wait_for_exit() 之前先检查"ec"。

所以问题是Boost.Test以某种方式修改了信号堆栈。

这种信号堆栈修改与Boost.Process有交互,至少在默认Boost.Test配置中,代码无法可靠地测试。

我用正常的主函数和一些函数重写了测试,它完成了这项工作。