Skip to content

Commit

Permalink
fix: fails to longjmp sometimes
Browse files Browse the repository at this point in the history
  • Loading branch information
homuler committed Nov 14, 2020
1 parent 2e03f73 commit f884865
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 33 deletions.
29 changes: 4 additions & 25 deletions C/mediapipe_api/common.cc
Original file line number Diff line number Diff line change
@@ -1,29 +1,8 @@
#include "mediapipe_api/common.h"

namespace {
thread_local struct sigaction orig_act;
thread_local sigjmp_buf abrt_jbuf;
thread_local struct sigaction mp_api::orig_act;
thread_local sigjmp_buf mp_api::abrt_jbuf;

void sigabrt_handler(int sig) {
siglongjmp(abrt_jbuf, 1);
}
void mp_api::sigabrt_handler(int sig) {
siglongjmp(abrt_jbuf, 1);
}

bool sigabrt_is_not_received() {
return sigsetjmp(abrt_jbuf, 1) == 0;
}

void setup_sigaction() {
struct sigaction act;

sigemptyset(&act.sa_mask);
act.sa_flags = 0;
act.sa_handler = sigabrt_handler;

sigaction(SIGABRT, &act, &orig_act);
}

void restore_sigaction() {
sigaction(SIGABRT, &orig_act, nullptr);
}

25 changes: 17 additions & 8 deletions C/mediapipe_api/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,31 +29,40 @@ enum class MpReturnCode : int {
Aborted = 134,
};

bool sigabrt_is_not_received();
void setup_sigaction();
void restore_sigaction();
namespace mp_api {
extern thread_local struct sigaction orig_act;
extern thread_local sigjmp_buf abrt_jbuf;

extern void sigabrt_handler(int sig);
}

// TODO(homuler): make code more readable
#define TRY auto _mp_return_code = MpReturnCode::Unset;\
#define TRY auto volatile _mp_return_code = MpReturnCode::Unset;\
try
#define CATCH_EXCEPTION catch (std::exception& e) {\
LOG(ERROR) << e.what();\
google::FlushLogFiles(google::ERROR);\
_mp_return_code = MpReturnCode::StandardError;\
} catch (...) {\
LOG(ERROR) << "Unknown exception";\
LOG(ERROR) << "Unknown exception occured";\
google::FlushLogFiles(google::ERROR);\
_mp_return_code = MpReturnCode::UnknownError;\
}\
return _mp_return_code;

#define TRY_ALL TRY {\
setup_sigaction();\
if (sigabrt_is_not_received())
struct sigaction act;\
sigemptyset(&act.sa_mask);\
act.sa_flags = 0;\
act.sa_handler = mp_api::sigabrt_handler;\
sigaction(SIGABRT, &act, &mp_api::orig_act);\
if (sigsetjmp(mp_api::abrt_jbuf, 1) == 0)
#define CATCH_ALL else {\
LOG(ERROR) << "Aborted";\
google::FlushLogFiles(google::ERROR);\
_mp_return_code = MpReturnCode::Aborted;\
}\
restore_sigaction();\
sigaction(SIGABRT, &mp_api::orig_act, nullptr);\
} CATCH_EXCEPTION
#define RETURN_CODE(code) _mp_return_code = code

Expand Down

0 comments on commit f884865

Please sign in to comment.