I was debugging the signal handling of one multi-thread process under Linux, and found one strange appearance. In a sentence of this problem is “SIGTERM handler is empty and is registered to system successfully, but the process may still be killed by SIGTERM when process starting”.
The detailed description as below:
- This process is forked by parent process, and started by
execve
Process fake operation
void sighdr(int sig) { Trace(); } main() { sigaction(); // Register SIGTERM handler prog_init(); // Very complex operation, read/write UNIX socket, // starting new thread, and so on. The whole operation // will take almost 12 seconds. prog_loop(); // loop of main thread }
If signal SIGTERM fired in the course of
prog_init()
operationThe
sighdr()
will be invoked by kernel, and the trace file can record the related information OK. But then the process stopped. As the amount of code of second thread is very large, so I couldn't position the last instruction. And a very few times (1/60), the process will not be stopped, but the running status is not correct, it's hanged inrecvmsg()
of Linux, theprog_loop()
will never be executed.If signal SIGTERM fired in
prog_loop()
The process runs fine, the SIGTERM will be ignored.
After running, the /proc/pid/status
Threads: 2
SigQ: 0/124393
SigPnd: 0000000000000000
ShdPnd: 0000000000000000
SigBlk: 0000000000000000
SigIgn: 0000000000000005
SigCgt: 0000000180005000
So my question:
1) Is there any possible reason of this phenomenon?
2) Is there any debug method to find what the process happened? I have tried the valgrind, but no valuable found.