swaglog: delay creating zmq socket to first use - fix modeld crash in sim (#24271)

* Initialize zmq socket later with initialize method

* empty lines

* Add lock and move initialize function

* Check for initialized
pull/24273/head
Gijs Koning 3 years ago committed by GitHub
parent e95a250bca
commit 6ab7b2f325
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 3
      selfdrive/common/statlog.cc
  2. 4
      selfdrive/common/swaglog.cc
  3. 18
      selfdrive/common/util.h

@ -17,6 +17,9 @@ class StatlogState : public LogState {
static StatlogState s = {}; static StatlogState s = {};
static void log(const char* metric_type, const char* metric, const char* fmt, ...) { static void log(const char* metric_type, const char* metric, const char* fmt, ...) {
std::lock_guard lk(s.lock);
if (!s.initialized) s.initialize();
char* value_buf = nullptr; char* value_buf = nullptr;
va_list args; va_list args;
va_start(args, fmt); va_start(args, fmt);

@ -21,7 +21,6 @@ class SwaglogState : public LogState {
public: public:
SwaglogState() : LogState("ipc:///tmp/logmessage") {} SwaglogState() : LogState("ipc:///tmp/logmessage") {}
bool initialized = false;
json11::Json::object ctx_j; json11::Json::object ctx_j;
inline void initialize() { inline void initialize() {
@ -56,8 +55,7 @@ class SwaglogState : public LogState {
} else { } else {
ctx_j["device"] = "pc"; ctx_j["device"] = "pc";
} }
LogState::initialize();
initialized = true;
} }
}; };

@ -168,12 +168,18 @@ void update_max_atomic(std::atomic<T>& max, T const& value) {
class LogState { class LogState {
public: public:
bool initialized = false;
std::mutex lock; std::mutex lock;
void *zctx; void *zctx = nullptr;
void *sock; void *sock = nullptr;
int print_level; int print_level;
const char* endpoint;
LogState(const char* endpoint) { LogState(const char* _endpoint) {
endpoint = _endpoint;
}
inline void initialize() {
zctx = zmq_ctx_new(); zctx = zmq_ctx_new();
sock = zmq_socket(zctx, ZMQ_PUSH); sock = zmq_socket(zctx, ZMQ_PUSH);
@ -182,9 +188,13 @@ class LogState {
zmq_setsockopt(sock, ZMQ_LINGER, &timeout, sizeof(timeout)); zmq_setsockopt(sock, ZMQ_LINGER, &timeout, sizeof(timeout));
zmq_connect(sock, endpoint); zmq_connect(sock, endpoint);
}; initialized = true;
}
~LogState() { ~LogState() {
if (initialized) {
zmq_close(sock); zmq_close(sock);
zmq_ctx_destroy(zctx); zmq_ctx_destroy(zctx);
} }
}
}; };

Loading…
Cancel
Save