Skip to content

Commit

Permalink
remove signal handler (#641)
Browse files Browse the repository at this point in the history
  • Loading branch information
sshane authored Jan 18, 2025
1 parent faf6ffd commit 102befe
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 39 deletions.
37 changes: 3 additions & 34 deletions msgq/impl_msgq.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,10 @@
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <csignal>
#include <cerrno>

#include "msgq/impl_msgq.h"


volatile sig_atomic_t msgq_do_exit = 0;

void sig_handler(int signal) {
assert(signal == SIGINT || signal == SIGTERM);
msgq_do_exit = 1;
}


MSGQContext::MSGQContext() {
}

Expand Down Expand Up @@ -72,23 +62,14 @@ int MSGQSubSocket::connect(Context *context, std::string endpoint, std::string a


Message * MSGQSubSocket::receive(bool non_blocking){
msgq_do_exit = 0;

void (*prev_handler_sigint)(int);
void (*prev_handler_sigterm)(int);
if (!non_blocking){
prev_handler_sigint = std::signal(SIGINT, sig_handler);
prev_handler_sigterm = std::signal(SIGTERM, sig_handler);
}

msgq_msg_t msg;

MSGQMessage *r = NULL;

int rc = msgq_msg_recv(&msg, q);

// Hack to implement blocking read with a poller. Don't use this
while (!non_blocking && rc == 0 && msgq_do_exit == 0){
while (!non_blocking && rc == 0){
msgq_pollitem_t items[1];
items[0].q = q;

Expand All @@ -107,21 +88,9 @@ Message * MSGQSubSocket::receive(bool non_blocking){
}
}


if (!non_blocking){
std::signal(SIGINT, prev_handler_sigint);
std::signal(SIGTERM, prev_handler_sigterm);
}

errno = msgq_do_exit ? EINTR : 0;

if (rc > 0){
if (msgq_do_exit){
msgq_msg_close(&msg); // Free unused message on exit
} else {
r = new MSGQMessage;
r->takeOwnership(msg.data, msg.size);
}
r = new MSGQMessage;
r->takeOwnership(msg.data, msg.size);
}

return (Message*)r;
Expand Down
5 changes: 0 additions & 5 deletions msgq/ipc_pyx.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -199,11 +199,6 @@ cdef class SubSocket:
msg = self.socket.receive(non_blocking)

if msg == NULL:
# If a blocking read returns no message check errno if SIGINT was caught in the C++ code
if errno.errno == errno.EINTR:
print("SIGINT received, exiting")
sys.exit(1)

return None
else:
sz = msg.getSize()
Expand Down

0 comments on commit 102befe

Please sign in to comment.