增强上下文:异常传播问题

时间:2019-05-17 00:47:25

标签: exception boost executioncontext boost-context

我正在使用boost::context::execution_context(第2版)编写C ++ 11库,并且希望将异常从execution_context传播到调用执行。

我想在客户端提供给我的库函数的lambda内部处理异常;但是,我遇到了一个奇怪的问题,在某些情况下boost :: context无法正确处理异常。

这可以按预期工作,并且与boost的testsexamples类似:

TEST(execution_context, works) {
  // Client callable
  auto &&f = [](boost::context::execution_context<void> &&ctx) {
    throw std::runtime_error("help!");
    return std::move(ctx);
  };

  // Library code
  std::exception_ptr exc{};
  boost::context::execution_context<void> source(
      [&exc, &f](boost::context::execution_context<void> &&ctx) {
        try {
          ctx = f(std::move(ctx));
        } catch (boost::context::detail::forced_unwind const &) {
          throw;
        } catch (...) {
          exc = std::current_exception();
        }
        return std::move(ctx);
      });

  try {
    source = source();
    if (exc) {
      std::rethrow_exception(exc);
    }
  } catch (std::runtime_error const &) {
    std::cout << "Runtime Error Caught" << std::endl;
  }
}

输出:

[ RUN      ] execution_context.works
Runtime Error Caught
[       OK ] execution_context.works (0 ms)

但是以下更改无效:

我们添加了一个包装execution_context的类:

class Core {
  boost::context::execution_context<void> ctx_;

public:
  explicit Core(boost::context::execution_context<void> &&ctx)
      : ctx_{std::move(ctx)} {}

  auto &&done() { return std::move(ctx_); }
};

现在我们进行与之前相同的测试,但使用定义的类:

TEST(execution_context, fails) {
  // Client callable
  auto &&f = [](Core c) {
    throw std::runtime_error("help!");
    return c.done();
  };

  // Library code
  std::exception_ptr exc{};
  boost::context::execution_context<void> source(
      [&exc, &f](boost::context::execution_context<void> &&ctx) {
        try {
          ctx = f(Core(std::move(ctx)));
        } catch (boost::context::detail::forced_unwind const &) {
          throw;
        } catch (...) {
          exc = std::current_exception();
        }
        return std::move(ctx);
      });

  try {
    source = source();
    if (exc) {
      std::rethrow_exception(exc);
    }
  } catch (std::runtime_error const &) {
    std::cout << "Runtime Error Caught" << std::endl;
  }
}

输出:

[ RUN      ] execution_context.fails
unknown file: Failure
Unknown C++ exception thrown in the test body.
generators.t.tsk: /home/plewis/dpkg/refroot/amd64/opt/include/boost/context/detail/exception.hpp:37: boost::context::detail::forced_unwind::~forced_unwind(): Assertion `caught' failed.
zsh: abort (core dumped)  ./test.t.tsk

我注意到的唯一区别是execution_context包含在一个类中,这导致对异常的处理不正确。那没有道理。

环境

我正在使用GTest。

编译器

> g++ --version
g++ (GCC) 5.3.1 20160406 (Red Hat 5.3.1-6)
...

系统

> uname -a
Linux myhostname 2.6.32-642.6.2.el6.x86_64 #1 SMP Mon Oct 24 10:22:33 EDT 2016 x86_64 x86_64 x86_64 GNU/Linux

提升

boost version 1.69.0 compiled for amd64

1 个答案:

答案 0 :(得分:1)

问题在于移动ctx实例。假设您有两个execution_context的实例:

execution_context src, dest;
// src and dest have some states
dest = std::move(src); // [1]

在[1]中,我们调用了移动分配运算符,该运算符“窃取” src的资源并将其放入dest。如果execution_context有一个指针,则移动后的指针在src实例中将为0,因此该对象无用,不应使用。对它的任何操作都可能会导致某些不良行为。

简而言之,您的代码如下:

void foo ()
{
  auto &&f = [](Core c) {};

  boost::context::execution_context<void> source(
      [&exc, &f](boost::context::execution_context<void> &&ctx) {
        // many lines        
        return std::move(ctx);
      });

    source = source();
}

我们有两个上下文foo和lambda主体传递到源构造函数中。调用source()时,将切换上下文并恢复foo并执行lambda。在此foo的lambda上下文中,因为它只是被移到Core实例中而被销毁。那么,您如何恢复foo的执行?你不能。

核心问题:

class Core {
  boost::context::execution_context<void> ctx_;

public:
  explicit Core(boost::context::execution_context<void> &&ctx)
      : ctx_{std::move(ctx)} {} // [2]

  auto &&done() { return std::move(ctx_); }
};

ctx_是非引用数据成员,因此在[2]中调用move构造函数,该构造函数会窃取ctx的资源。

如果没有引发异常,下一个问题将是done方法: 看这个:

  auto &&f = [](Core c)
  {
    // throw std::runtime_error("help!"); COMMENTED
    return c.done();
  };

c在lambda内部。 done返回对Core数据成员的引用,该数据成员在lambda结束时被破坏。因此,您可以随意参考。

修复:您可以将对上下文的引用存储在Core中。这样foo的原始上下文将很安全。