Skip to content

Commit

Permalink
Prevent crashes for opencv exception and std::exception (apache#14433)
Browse files Browse the repository at this point in the history
* Relax constexpr restriction

* Change imagenet_gen_qsym_mkldnn

* Revert constexpr change

* Add dummy image for test

* Fix unreverted change

* Remove url

* Fix imagenet change

* Catch only std::exception

* Fix const, remove dmlc::Error catch

* Retrigger CI
  • Loading branch information
anirudh2290 authored and Rohit Kumar Srivastava committed May 14, 2019
1 parent 5aa9a6f commit 3bee946
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
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() \
} \
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 @@ -381,7 +381,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

0 comments on commit 3bee946

Please sign in to comment.