SIGPROF kills my server when using google perftools

I have a multithreaded server process written in C / C ++ that I am trying to profile using Google perftools. However, when I start the process with perftools, pretty soon my server stops with the error "syscall aborted", which, I think, is caused by the incoming SIGPROF. (The actual system call that is interrupted is inside my zmq_recv call, but I don't think it really matters what it is.)

Is this the expected behavior? Do I have to explicitly handle this case somehow? Or is something wrong here?

+6
source share
1 answer

From the zeroMQ documentation for zmq_recv (), you can expect an EINTR return if a signal is received when it is running.

Generating signals right in the middle of a call to zmq_recv() would be a difficult task for any test. Fortunately, the gperftools generating a ton of SIGPROF detected this subtle โ€œerrorโ€ in your code.

This one should be properly handled in your code, since the zeroMQ framework is an elegant control. Repeat logic can be as simple as modifying an existing call:

  /* Block until a message is available to be received from socket */ rc = zmq_recv (socket, &part, 0); 

with a new one (with retry logic) as follows:

  /* Block until a message is available to be received from socket * Keep retrying if interrupted by any signal */ rc = 0; while(rc != EINTR) { rc = zmq_recv (socket, &part, 0); } 

Also set the signal handler function in your program. You can simply ignore the interrupt due to SIGPROF and continue the retry.

Finally, you can process certain signals and act accordingly. For example, gracefully terminating your program, even if the user presses CTRL + C while your program is waiting on zmq_recv() .

  /* Block until a message is available to be received from socket * If interrupted by any signal, * - in handler-code: Check for signal number and update status accordingly. * - in regular-code: Check for status and retry/exit as appropriate */ rc = 0; while(rc != EINTR && status == RETRY) { rc = zmq_recv (socket, &part, 0); } 

In the interest of keeping the code โ€œclean,โ€ you will be better served by using the above snippet to write your own shell of static inline functions around zmq_recv() , which you can call in your program.

Regarding the decision to make zmq_recv() return EINTR after receiving the signals, you can check out this article, which talks about a worse philosophy behind such a design, which is simpler from the point of view of implementation.


UPDATE Documented code for signal processing in the context of zmq_recv() is available at git.lucina.net/zeromq-examples.git/tree/zmq-camera.c . It corresponds to the same lines as explained above, but it looks well tested and ready to use with detailed comments, kindly showered (yayyy zeromq!).

+8
source

Source: https://habr.com/ru/post/949151/


All Articles