Is it possible to terminate only the one thread on receiving a SIGSEGV?

喜欢而已 提交于 2019-12-13 15:43:22

问题


I have an application which starts multiple threads. I am using a signal handler to catch the signals.

I don't want my application to quit on SIGSEGV; I want to terminate only the thread that incurred the signal, and to continue the flow of the full application in the other threads. Is it possible?


回答1:


If a SIGSEGV happens, it indicates that your program has already invoked undefined behavior, i.e. the state of the entire program is undefined/indeterminate/invalid. In practice it's possible that you may be able to recover and keep running, but there's no guarantee, and it could be dangerous.

As asveikau mentioned, you could longjmp out of the signal handler and try to clean up, but this could make an even worse mess if the crash happened in the middle of malloc, free, printf, or any function modifying the state of global data or data that's shared with other threads or that will be accessed in the cleanup code at the longjmp destination. The state may be corrupt/inconsistent, and/or locks may be held and left permanently unreleasable.

If you can ensure this won't happen - for example if the misbehaving thread never calls any async-signal-unsafe functions - then it may be safe to longjmp out of the signal handler then call pthread_exit.

An alternative might be to permanently freeze the thread in the signal handler, by adding all signals to the sa_mask for SIGSEGV and then writing for (;;) pause(); in the signal handler. This is 100% "safe", but may leave the process in a deadlocked state if any locks were held by the crashing thread. This is perhaps "less bad" than exposing corrupt state to other threads and further clobbering your data to hell...



来源:https://stackoverflow.com/questions/7291273/is-it-possible-to-terminate-only-the-one-thread-on-receiving-a-sigsegv

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!