Add extra safeguard around calling CLR callbacks.

This commit is contained in:
Eric Harding committed 2018-02-23 19:40:23 -05:00
1 parent 78644f9f4d
commit 6dda712a61
1 file changed
+29 -11
+29 -11
View File
@@ -3,6 +3,7 @@
#include "lib/config/config.h"
#include "lib/logging/logging.h"
#include "lib/utils/bts_assert.h"
#include "lib/utils/Dispatcher.h"
#include "../public/ServiceContext.h"
#include "../public/Result.h"
@@ -114,14 +115,31 @@ CAPI void api_client_destroy(ApiClient* pclient) {
}
}
template <class C>
void safe_call_fn(const C& cb) {
#ifdef _MSC_VER
__try {
#endif
cb();
#ifdef _MSC_VER
}
__except(EXCEPTION_EXECUTE_HANDLER) {
LOG(Api, Error, "Error calling CLR");
}
#endif
}
void reply_error(ApiError error, raw_response_callback cb, void* extra) {
ResponseHeader response;
response.set_code(error);
auto str = response.SerializeAsString();
str = (char)(str.size()+1) + str;
cb(str.c_str(), str.size(), extra);
safe_call_fn([&]() {
cb(str.c_str(), str.size(), extra);
});
}
CAPI void api_client_send(ApiClient* pclient, const char* buffer, int buffer_length, raw_response_callback cb, void* extra) {
bts_assert(pclient);
std::string strbuf(buffer, buffer_length);
@@ -132,7 +150,9 @@ CAPI void api_client_send(ApiClient* pclient, const char* buffer, int buffer_len
LOG(Api,Diag,"rpc reply {}",((int64_t)buffer));
switch (e) {
case qbus::RPCError::RPC_OK:
cb(reply->value().c_str(), reply->value().size(), extra);
safe_call_fn([&]() {
cb(reply->value().c_str(), reply->value().size(), extra);
});
break;
case qbus::RPCError::NO_SERVER:
reply_error(ApiError::NO_SERVER, cb, extra);
@@ -152,16 +172,11 @@ CAPI bool api_client_is_on_qb_thread(ApiClient* pclient) {
void safe_call_subscription_callback(raw_subscription_callback cb, EventType type, const std::vector<StreamUpdate>& updates, void* extra) {
#ifdef _MSC_VER
__try {
#endif
safe_call_fn([&]() {
if (updates.size() > 0) {
cb(type, &updates[0], updates.size(), extra);
}
#ifdef _MSC_VER
}
__except(EXCEPTION_EXECUTE_HANDLER) { }
#endif
});
}
@@ -179,7 +194,7 @@ CAPI TableSubscription api_client_subscribe(ApiClient* pclient, const char* meth
static_assert((int)qbus::Change::Type::CREATE == StreamUpdate::CREATE, "");
auto topic = stream_topic(pclient->service_name, method_and_qualifier);
LOG(Api,Info,"api subscribe: {}" ,topic.value());
LOG(Api,Diag,"api subscribe: {}" ,topic.value());
auto result = new qbus::TableSubscriber(pclient->qbus, equal_topic(topic), [cb, extra](const qbus::TableEvent& ev) {
if (ev.event_type == qbus::TableEvent::EventType::TABLE_CREATED ||
@@ -216,7 +231,10 @@ CAPI void api_client_unsubscribe(ApiClient* pclient, TableSubscription pvtable)
CAPI void api_client_log_error(const char* message) {
// managed api dll does not include logging but native api does `\_(o,o)_/`
LOG(Api, Error, "{}", std::string(message));
std::string msg(message);
utils::Dispatcher::background().invoke([msg]() {
LOG(Api, Error, "{}", msg);
});
}
}}// namespace