未验证 提交 a197737c 编写于 作者: Y Yu Yang 提交者: GitHub

Merge pull request #12690 from reyoung/feature/better_exception_holder

Polish API of exception holder
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
#pragma once #pragma once
#include "glog/logging.h"
#include "paddle/fluid/platform/enforce.h" #include "paddle/fluid/platform/enforce.h"
namespace paddle { namespace paddle {
...@@ -22,27 +23,24 @@ namespace details { ...@@ -22,27 +23,24 @@ namespace details {
class ExceptionHolder { class ExceptionHolder {
public: public:
void Catch(const platform::EnforceNotMet& exp) { void Catch(std::exception_ptr eptr) {
std::lock_guard<std::mutex> lock(mu_); try {
exception_.reset(new platform::EnforceNotMet(exp)); std::rethrow_exception(eptr);
type_ = kEnforceNotMet; } catch (platform::EOFException exp) {
} Catch(exp);
} catch (platform::EnforceNotMet exp) {
void Catch(const platform::EOFException& exp) { Catch(exp);
std::lock_guard<std::mutex> lock(mu_); } catch (...) {
// EOFException will not cover up existing EnforceNotMet. LOG(FATAL) << "Unknown exception caught";
if (exception_.get() == nullptr) {
exception_.reset(new platform::EOFException(exp));
type_ = kEOF;
} }
} }
bool ExceptionCatched() const { bool IsCaught() const {
std::lock_guard<std::mutex> lock(mu_); std::lock_guard<std::mutex> lock(mu_);
return exception_.get() != nullptr; return exception_.get() != nullptr;
} }
void Throw() { void ReThrow() {
std::lock_guard<std::mutex> lock(mu_); std::lock_guard<std::mutex> lock(mu_);
switch (type_) { switch (type_) {
case kNone: case kNone:
...@@ -50,27 +48,41 @@ class ExceptionHolder { ...@@ -50,27 +48,41 @@ class ExceptionHolder {
case kEnforceNotMet: { case kEnforceNotMet: {
auto e = *static_cast<platform::EnforceNotMet*>(exception_.get()); auto e = *static_cast<platform::EnforceNotMet*>(exception_.get());
throw e; throw e;
break;
} }
case kEOF: { case kEOF: {
auto e = *static_cast<platform::EOFException*>(exception_.get()); auto e = *static_cast<platform::EOFException*>(exception_.get());
throw e; throw e;
break;
} }
default:
LOG(FATAL) << "Unknown exception.";
} }
exception_.reset(); ClearImpl();
type_ = kNone;
} }
void Clear() { void Clear() {
std::lock_guard<std::mutex> lock(mu_); std::lock_guard<std::mutex> lock(mu_);
ClearImpl();
}
private:
void ClearImpl() {
exception_.reset(); exception_.reset();
type_ = kNone; type_ = kNone;
} }
private: void Catch(const platform::EnforceNotMet& exp) {
std::lock_guard<std::mutex> lock(mu_);
exception_.reset(new platform::EnforceNotMet(exp));
type_ = kEnforceNotMet;
}
void Catch(const platform::EOFException& exp) {
std::lock_guard<std::mutex> lock(mu_);
// EOFException will not cover up existing EnforceNotMet.
if (exception_.get() == nullptr) {
exception_.reset(new platform::EOFException(exp));
type_ = kEOF;
}
}
enum ExceptionType { kNone, kEnforceNotMet, kEOF }; enum ExceptionType { kNone, kEnforceNotMet, kEOF };
ExceptionType type_{kNone}; ExceptionType type_{kNone};
......
...@@ -107,11 +107,11 @@ FeedFetchList ThreadedSSAGraphExecutor::Run( ...@@ -107,11 +107,11 @@ FeedFetchList ThreadedSSAGraphExecutor::Run(
auto cur_ready_vars = ready_vars.PopAll(1, &timeout); auto cur_ready_vars = ready_vars.PopAll(1, &timeout);
if (timeout) { if (timeout) {
if (exception_holder_.ExceptionCatched()) { if (exception_holder_.IsCaught()) {
for (auto &run_op_future : run_op_futures_) { for (auto &run_op_future : run_op_futures_) {
run_op_future.wait(); run_op_future.wait();
} }
exception_holder_.Throw(); exception_holder_.ReThrow();
} else { } else {
continue; continue;
} }
...@@ -220,12 +220,8 @@ void ThreadedSSAGraphExecutor::RunOp( ...@@ -220,12 +220,8 @@ void ThreadedSSAGraphExecutor::RunOp(
running_ops_--; running_ops_--;
ready_var_q->Extend(op->Outputs()); ready_var_q->Extend(op->Outputs());
VLOG(10) << op << " " << op->Name() << "Signal posted"; VLOG(10) << op << " " << op->Name() << "Signal posted";
} catch (platform::EOFException ex) {
exception_holder_.Catch(ex);
} catch (platform::EnforceNotMet ex) {
exception_holder_.Catch(ex);
} catch (...) { } catch (...) {
LOG(FATAL) << "Unknown exception catched"; exception_holder_.Catch(std::current_exception());
} }
}; };
if (pool_) { if (pool_) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册