Skip to content

Commit a197737

Browse files
authored
Merge pull request #12690 from reyoung/feature/better_exception_holder
Polish API of exception holder
2 parents e765dea + aac80ef commit a197737

File tree

2 files changed

+36
-28
lines changed

2 files changed

+36
-28
lines changed

paddle/fluid/framework/details/exception_holder.h

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
#pragma once
1616

17+
#include "glog/logging.h"
1718
#include "paddle/fluid/platform/enforce.h"
1819

1920
namespace paddle {
@@ -22,55 +23,66 @@ namespace details {
2223

2324
class ExceptionHolder {
2425
public:
25-
void Catch(const platform::EnforceNotMet& exp) {
26-
std::lock_guard<std::mutex> lock(mu_);
27-
exception_.reset(new platform::EnforceNotMet(exp));
28-
type_ = kEnforceNotMet;
29-
}
30-
31-
void Catch(const platform::EOFException& exp) {
32-
std::lock_guard<std::mutex> lock(mu_);
33-
// EOFException will not cover up existing EnforceNotMet.
34-
if (exception_.get() == nullptr) {
35-
exception_.reset(new platform::EOFException(exp));
36-
type_ = kEOF;
26+
void Catch(std::exception_ptr eptr) {
27+
try {
28+
std::rethrow_exception(eptr);
29+
} catch (platform::EOFException exp) {
30+
Catch(exp);
31+
} catch (platform::EnforceNotMet exp) {
32+
Catch(exp);
33+
} catch (...) {
34+
LOG(FATAL) << "Unknown exception caught";
3735
}
3836
}
3937

40-
bool ExceptionCatched() const {
38+
bool IsCaught() const {
4139
std::lock_guard<std::mutex> lock(mu_);
4240
return exception_.get() != nullptr;
4341
}
4442

45-
void Throw() {
43+
void ReThrow() {
4644
std::lock_guard<std::mutex> lock(mu_);
4745
switch (type_) {
4846
case kNone:
4947
break;
5048
case kEnforceNotMet: {
5149
auto e = *static_cast<platform::EnforceNotMet*>(exception_.get());
5250
throw e;
53-
break;
5451
}
5552
case kEOF: {
5653
auto e = *static_cast<platform::EOFException*>(exception_.get());
5754
throw e;
58-
break;
5955
}
60-
default:
61-
LOG(FATAL) << "Unknown exception.";
6256
}
63-
exception_.reset();
64-
type_ = kNone;
57+
ClearImpl();
6558
}
6659

6760
void Clear() {
6861
std::lock_guard<std::mutex> lock(mu_);
62+
ClearImpl();
63+
}
64+
65+
private:
66+
void ClearImpl() {
6967
exception_.reset();
7068
type_ = kNone;
7169
}
7270

73-
private:
71+
void Catch(const platform::EnforceNotMet& exp) {
72+
std::lock_guard<std::mutex> lock(mu_);
73+
exception_.reset(new platform::EnforceNotMet(exp));
74+
type_ = kEnforceNotMet;
75+
}
76+
77+
void Catch(const platform::EOFException& exp) {
78+
std::lock_guard<std::mutex> lock(mu_);
79+
// EOFException will not cover up existing EnforceNotMet.
80+
if (exception_.get() == nullptr) {
81+
exception_.reset(new platform::EOFException(exp));
82+
type_ = kEOF;
83+
}
84+
}
85+
7486
enum ExceptionType { kNone, kEnforceNotMet, kEOF };
7587
ExceptionType type_{kNone};
7688

paddle/fluid/framework/details/threaded_ssa_graph_executor.cc

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,11 @@ FeedFetchList ThreadedSSAGraphExecutor::Run(
107107
auto cur_ready_vars = ready_vars.PopAll(1, &timeout);
108108

109109
if (timeout) {
110-
if (exception_holder_.ExceptionCatched()) {
110+
if (exception_holder_.IsCaught()) {
111111
for (auto &run_op_future : run_op_futures_) {
112112
run_op_future.wait();
113113
}
114-
exception_holder_.Throw();
114+
exception_holder_.ReThrow();
115115
} else {
116116
continue;
117117
}
@@ -220,12 +220,8 @@ void ThreadedSSAGraphExecutor::RunOp(
220220
running_ops_--;
221221
ready_var_q->Extend(op->Outputs());
222222
VLOG(10) << op << " " << op->Name() << "Signal posted";
223-
} catch (platform::EOFException ex) {
224-
exception_holder_.Catch(ex);
225-
} catch (platform::EnforceNotMet ex) {
226-
exception_holder_.Catch(ex);
227223
} catch (...) {
228-
LOG(FATAL) << "Unknown exception catched";
224+
exception_holder_.Catch(std::current_exception());
229225
}
230226
};
231227
if (pool_) {

0 commit comments

Comments
 (0)