Skip to content

Commit

Permalink
feat: handle SIGABRT
Browse files Browse the repository at this point in the history
  • Loading branch information
homuler committed Nov 3, 2020
1 parent 32cab19 commit ee0c4c3
Show file tree
Hide file tree
Showing 15 changed files with 173 additions and 89 deletions.
7 changes: 7 additions & 0 deletions Assets/MediaPipe/SDK/Scripts/Core/MediaPipePluginException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using System;

namespace Mediapipe {
public class MediaPipePluginException : ApplicationException {
public MediaPipePluginException(string message) : base(message) {}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion Assets/MediaPipe/SDK/Scripts/Framework/BoolPacket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,16 @@ public class BoolPacket : Packet<bool> {
this.ptr = ptr;
}

public BoolPacket(bool value, Timestamp timestamp) : base() {
UnsafeNativeMethods.mp__MakeBoolPacket_At__b_Rtimestamp(value, timestamp.mpPtr, out var ptr).Assert();
this.ptr = ptr;
}

public override bool Get() {
return SafeNativeMethods.mp_Packet__GetBool(ptr);
UnsafeNativeMethods.mp_Packet__GetBool(mpPtr, out var value).Assert();

GC.KeepAlive(this);
return value;
}

public override bool Consume() {
Expand Down
20 changes: 15 additions & 5 deletions Assets/MediaPipe/SDK/Scripts/PInvoke/MpReturnCode.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
namespace Mediapipe {
public enum MpReturnCode : int {
Success = 0,
/// <summary>A standard exception is thrown</summary>
StandardError = 1,
UnknownError = 2,
/// <summary>Something other than standard exception is thrown</summary>
UnknownError = 70,
/// <summary>SDK failed to set status code (bug)</summary>
Unset = 128, //
/// <summary>Received SIGABRT</summary>
Aborted = 134,
}

public static class MpReturnCodeExtension {
public static void Assert(this MpReturnCode code) {
if (code == MpReturnCode.Success) {
return;
switch (code) {
case MpReturnCode.Success: return;
case MpReturnCode.Aborted: {
throw new MediaPipeException("MediaPipe Aborted, refer glog files for more details");
}
default: {
throw new MediaPipePluginException($"Failed to call a native function (code={code}");
}
}

throw new MediaPipeException($"Failed to call a native method (code={code})");
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,11 @@ internal static partial class UnsafeNativeMethods {

[DllImport (MediaPipeLibrary, ExactSpelling = true)]
public static extern MpReturnCode mp__MakeBoolPacket__b([MarshalAs(UnmanagedType.I1)] bool value, out IntPtr packet);

[DllImport (MediaPipeLibrary, ExactSpelling = true)]
public static extern MpReturnCode mp__MakeBoolPacket_At__b_Rtimestamp([MarshalAs(UnmanagedType.I1)] bool value, IntPtr timestamp, out IntPtr packet);

[DllImport (MediaPipeLibrary, ExactSpelling = true)]
public static extern MpReturnCode mp_Packet__GetBool(IntPtr packet, [MarshalAs(UnmanagedType.I1)]out bool value);
}
}
2 changes: 2 additions & 0 deletions C/mediapipe_api/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,13 @@ cc_library(

cc_library(
name = "common",
srcs = ["common.cc"],
hdrs = ["common.h"],
deps = [
"@com_google_mediapipe//mediapipe/framework/port:logging",
],
visibility = ["//visibility:public"],
alwayslink = True,
)

pkg_model(
Expand Down
29 changes: 29 additions & 0 deletions C/mediapipe_api/common.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include "mediapipe_api/common.h"

namespace {
thread_local struct sigaction orig_act;
thread_local sigjmp_buf abrt_jbuf;

void 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);
}

43 changes: 32 additions & 11 deletions C/mediapipe_api/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#define MP_CAPI_EXPORT
#endif

#include <csetjmp>
#include <csignal>
#include <string>
#include "mediapipe/framework/port/logging.h"

Expand All @@ -22,19 +24,38 @@ extern inline const char* strcpy_to_heap(const std::string& str) {
enum class MpReturnCode : int {
Success = 0,
StandardError = 1,
UnknownError = 2
UnknownError = 70,
Unset = 128,
Aborted = 134,
};

#define TRY try
#define CATCH_ALL catch (std::exception& e) {\
LOG(ERROR) << e.what();\
google::FlushLogFiles(google::ERROR);\
return MpReturnCode::StandardError;\
} catch (...) {\
LOG(ERROR) << "Unknown exception";\
google::FlushLogFiles(google::ERROR);\
return MpReturnCode::UnknownError;\
}
bool sigabrt_is_not_received();
void setup_sigaction();
void restore_sigaction();

// TODO(homuler): make code more readable
#define TRY auto _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";\
google::FlushLogFiles(google::ERROR);\
_mp_return_code = MpReturnCode::UnknownError;\
}\
return _mp_return_code;

#define TRY_ALL TRY {\
setup_sigaction();\
if (sigabrt_is_not_received())
#define CATCH_ALL else {\
_mp_return_code = MpReturnCode::Aborted;\
}\
restore_sigaction();\
} CATCH_EXCEPTION
#define RETURN_CODE(code) _mp_return_code = code

#define MP_CAPI(rettype) MP_CAPI_EXPORT extern rettype
#endif // C_MEDIAPIPE_API_COMMON_H_
4 changes: 2 additions & 2 deletions C/mediapipe_api/external/protobuf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ MpReturnCode google_protobuf__SetLogHandler__PF(LogHandler* handler) {
TRY {
logHandler = handler;
google::protobuf::SetLogHandler(&HandleProtobufLog);
return MpReturnCode::Success;
} CATCH_ALL
RETURN_CODE(MpReturnCode::Success);
} CATCH_EXCEPTION
}

void MpSerializedProtoDestroy(MpSerializedProto* proto) {
Expand Down
4 changes: 2 additions & 2 deletions C/mediapipe_api/framework/calculator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ MpReturnCode google_protobuf_TextFormat__ParseFromStringAsCalculatorGraphConfig_
auto result = google::protobuf::TextFormat::ParseFromString(input, config);

*config_out = result ? config : nullptr;
return MpReturnCode::Success;
} CATCH_ALL
RETURN_CODE(MpReturnCode::Success);
} CATCH_EXCEPTION
}

void mp_CalculatorGraphConfig__delete(mediapipe::CalculatorGraphConfig* config) {
Expand Down
61 changes: 36 additions & 25 deletions C/mediapipe_api/framework/packet.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
MpReturnCode mp_Packet__(mediapipe::Packet** packet_out) {
TRY {
*packet_out = new mediapipe::Packet();
return MpReturnCode::Success;
} CATCH_ALL
RETURN_CODE(MpReturnCode::Success);
} CATCH_EXCEPTION
}

void mp_Packet__delete(mediapipe::Packet* packet) {
Expand All @@ -15,62 +15,73 @@ void mp_Packet__delete(mediapipe::Packet* packet) {

MpReturnCode mp_Packet__At__Rtimestamp(mediapipe::Packet* packet, mediapipe::Timestamp* timestamp, mediapipe::Packet** packet_out) {
TRY {
// not move but copy
*packet_out = new mediapipe::Packet { packet->At(*timestamp) };
return MpReturnCode::Success;
} CATCH_ALL
RETURN_CODE(MpReturnCode::Success);
} CATCH_EXCEPTION
}

MpReturnCode mp_Packet__ValidateAsProtoMessageLite(mediapipe::Packet* packet, mediapipe::Status** status_out) {
TRY {
*status_out = new mediapipe::Status { packet->ValidateAsProtoMessageLite() };
return MpReturnCode::Success;
} CATCH_ALL
RETURN_CODE(MpReturnCode::Success);
} CATCH_EXCEPTION
}

MpReturnCode mp_Packet__Timestamp(mediapipe::Packet* packet, mediapipe::Timestamp** timestamp_out) {
TRY {
*timestamp_out = new mediapipe::Timestamp { packet->Timestamp() };
return MpReturnCode::Success;
} CATCH_ALL
RETURN_CODE(MpReturnCode::Success);
} CATCH_EXCEPTION
}

MpReturnCode mp_Packet__DebugString(mediapipe::Packet* packet, const char** str_out) {
TRY {
*str_out = strcpy_to_heap(packet->DebugString());
return MpReturnCode::Success;
} CATCH_ALL
RETURN_CODE(MpReturnCode::Success);
} CATCH_EXCEPTION
}

MpReturnCode mp_Packet__RegisteredTypeName(mediapipe::Packet* packet, const char** str_out) {
TRY {
*str_out = strcpy_to_heap(packet->RegisteredTypeName());
return MpReturnCode::Success;
} CATCH_ALL
RETURN_CODE(MpReturnCode::Success);
} CATCH_EXCEPTION
}

MpReturnCode mp_Packet__DebugTypeName(mediapipe::Packet* packet, const char** str_out) {
TRY {
*str_out = strcpy_to_heap(packet->DebugTypeName());
return MpReturnCode::Success;
} CATCH_ALL
RETURN_CODE(MpReturnCode::Success);
} CATCH_EXCEPTION
}

MpReturnCode mp__MakeBoolPacket__b(bool value, mediapipe::Packet** packet_out) {
TRY {
*packet_out = new mediapipe::Packet(mediapipe::MakePacket<bool>(value));
return MpReturnCode::Success;
} CATCH_ALL
*packet_out = new mediapipe::Packet { mediapipe::MakePacket<bool>(value) };
RETURN_CODE(MpReturnCode::Success);
} CATCH_EXCEPTION
}

bool mp_Packet__GetBool(mediapipe::Packet* packet) {
return packet->Get<bool>();
MpReturnCode mp__MakeBoolPacket_At__b_Rtimestamp(bool value, mediapipe::Timestamp* timestamp, mediapipe::Packet** packet_out) {
TRY {
*packet_out = new mediapipe::Packet { mediapipe::MakePacket<bool>(value).At(*timestamp) };
RETURN_CODE(MpReturnCode::Success);
} CATCH_EXCEPTION
}

MpReturnCode mp_Packet__GetBool(mediapipe::Packet* packet, bool* value_out) {
TRY_ALL {
*value_out = packet->Get<bool>();
RETURN_CODE(MpReturnCode::Success);
} CATCH_ALL
}

MpReturnCode mp_Packet__ValidateAsBool(mediapipe::Packet* packet, mediapipe::Status** status_out) {
TRY {
*status_out = new mediapipe::Status { packet->ValidateAsType<bool>() };
return MpReturnCode::Success;
} CATCH_ALL
RETURN_CODE(MpReturnCode::Success);
} CATCH_EXCEPTION
}

MpPacket* MpMakeFloatPacket(float value) {
Expand Down Expand Up @@ -99,8 +110,8 @@ const char* MpPacketGetString(MpPacket* packet) {
MpReturnCode mp_SidePacket__(SidePacket** side_packet_out) {
TRY {
*side_packet_out = new SidePacket();
return MpReturnCode::Success;
} CATCH_ALL
RETURN_CODE(MpReturnCode::Success);
} CATCH_EXCEPTION
}

void mp_SidePacket__delete(SidePacket* side_packet) {
Expand All @@ -110,6 +121,6 @@ void mp_SidePacket__delete(SidePacket* side_packet) {
MpReturnCode mp_SidePacket__emplace(SidePacket* side_packet, const char* key, mediapipe::Packet* packet) {
TRY {
side_packet->emplace(std::string(key), *packet);
return MpReturnCode::Success;
} CATCH_ALL
RETURN_CODE(MpReturnCode::Success);
} CATCH_EXCEPTION
}
3 changes: 2 additions & 1 deletion C/mediapipe_api/framework/packet.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ MP_CAPI(MpReturnCode) mp_Packet__DebugTypeName(mediapipe::Packet* packet, const

// Boolean
MP_CAPI(MpReturnCode) mp__MakeBoolPacket__b(bool value, mediapipe::Packet** packet_out);
MP_CAPI(bool) mp_Packet__GetBool(mediapipe::Packet* packet);
MP_CAPI(MpReturnCode) mp__MakeBoolPacket_At__b_Rtimestamp(bool value, mediapipe::Timestamp* timestamp, mediapipe::Packet** packet_out);
MP_CAPI(MpReturnCode) mp_Packet__GetBool(mediapipe::Packet* packet, bool* value_out);
MP_CAPI(MpReturnCode) mp_Packet__ValidateAsBool(mediapipe::Packet* packet, mediapipe::Status** status_out);

// Float
Expand Down
8 changes: 4 additions & 4 deletions C/mediapipe_api/framework/port/status.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ MpReturnCode mp_Status__i_PKc(int code, const char* message, mediapipe::Status**
TRY {
auto status_code = static_cast<mediapipe::StatusCode>(code);
*status_out = new mediapipe::Status { status_code, message };
return MpReturnCode::Success;
} CATCH_ALL
RETURN_CODE(MpReturnCode::Success);
} CATCH_EXCEPTION
}

void mp_Status__delete(mediapipe::Status* status) {
Expand All @@ -16,8 +16,8 @@ void mp_Status__delete(mediapipe::Status* status) {
MpReturnCode mp_Status__ToString(mediapipe::Status* status, const char** str_out) {
TRY {
*str_out = strcpy_to_heap(status->ToString());
return MpReturnCode::Success;
} CATCH_ALL
RETURN_CODE(MpReturnCode::Success);
} CATCH_EXCEPTION
}

bool mp_Status__ok(mediapipe::Status* status) {
Expand Down
Loading

0 comments on commit ee0c4c3

Please sign in to comment.