Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

Prevent crashes for opencv exception and std::exception #14433

Merged
merged 10 commits into from
May 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions include/mxnet/c_api_error.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,26 @@
* and finishes with API_END() or API_END_HANDLE_ERROR()
* The finally clause contains procedure to cleanup states when an error happens.
*/
#define MX_API_BEGIN() try { on_enter_api(__FUNCTION__);
#define MX_API_END() } catch(dmlc::Error &_except_) { on_exit_api(); return MXAPIHandleException(_except_); } on_exit_api(); return 0; // NOLINT(*)
#define MX_API_END_HANDLE_ERROR(Finalize) } catch(dmlc::Error &_except_) { Finalize; on_exit_api(); return MXAPIHandleException(_except_); } on_exit_api(); return 0; // NOLINT(*)
#define MX_API_BEGIN() \
try { \
on_enter_api(__FUNCTION__);
#define MX_API_END() \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it possible to have the defines inside the scopes a bit more ordered?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you please elaborate on what you mean by that

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Never mind, I think it's a separate discussion from this PR.

} \
catch (const std::exception &_except_) { \
on_exit_api(); \
return MXAPIHandleException(_except_); \
} \
on_exit_api(); \
return 0; // NOLINT(*)
#define MX_API_END_HANDLE_ERROR(Finalize) \
} \
catch (const std::exception &_except_) { \
Finalize; \
on_exit_api(); \
return MXAPIHandleException(_except_); \
} \
on_exit_api(); \
return 0; // NOLINT(*)
/*!
* \brief Set the last error message needed by C API
* \param msg The error message to set.
Expand All @@ -44,7 +61,7 @@ void MXAPISetLastError(const char* msg);
* \param e the exception
* \return the return value of API after exception is handled
*/
inline int MXAPIHandleException(const dmlc::Error &e) {
inline int MXAPIHandleException(const std::exception &e) {
MXAPISetLastError(e.what());
return -1;
}
Expand Down
2 changes: 1 addition & 1 deletion src/engine/threaded_engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ class ThreadedEngine : public Engine {
} else {
callback();
}
} catch (dmlc::Error& e) {
} catch (const std::exception& e) {
threaded_opr->opr_exception =
std::make_shared<std::exception_ptr>(std::current_exception());
callback();
Expand Down
13 changes: 10 additions & 3 deletions tests/python/unittest/test_exc_handling.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,8 @@ def test_multiple_waitalls():
mx.nd.waitall()

@with_seed()
def test_exc_profiler():
def run_training_iteration(data):
output = net(data)
def run_training_iteration(data):
output = net(data)

net = gluon.nn.HybridSequential()
with net.name_scope():
Expand All @@ -182,6 +181,14 @@ def run_training_iteration(data):
mx.nd.waitall()
mx.profiler.set_state("stop")

@with_seed()
def test_opencv_exception():
def check_resize():
img = mx.nd.ones((1200, 1600, 3))
img = mx.image.imresize(img, 320, 320, interp=-1)
img.asnumpy()
assert_raises(MXNetError, check_resize)


if __name__ == '__main__':
import nose
Expand Down