Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to handle a SEGFAULT orginating in native code?

I have a Java 1.6 application that accesses a third party native module, through a JNI class provided as the interface. Recently we noticed that a SEGFAULT is occurring in the native module, and is causing our application to crash. Is it possible to catch and handle this event, at least to log it properly before dieing?


I tried both Java techniques in the article from kjp's answer. Neither worked. Attempting to install a signal handler on 'SEGV' results in the exception

Signal already used by VM: SEGV

The shutdown handler I installed simply failed to fire, presumably because of what the IBM article states:

Shutdown hooks will not be run if

Runtime.halt() method is called to terminate the JVM. Runtime.halt() is provided to allow a quick shutdown of the JVM.
The -Xrs JVM option is specified.
The JVM exits abnormally, such as an exception condition or forced abort generated by the JVM software.

like image 363
C. Ross Avatar asked Jul 04 '12 19:07

C. Ross


2 Answers

If all you want to do is log and notify you can write a script which runs your application. When the application dies, the script can detect whether the application terminated normally and from the hs_errXXXX file which has all the crash/SEGV information and mail it to someone (and restart the application if you want)


What you need to do is to run the faulty JNI code in another JVM and communicate with that JVM using RMI or JMS or Sockets. This way when the library dies, it won't bring down your main application and you can restart it.

like image 150
Peter Lawrey Avatar answered Oct 30 '22 11:10

Peter Lawrey


Based on several weeks of research at the time, as well as conversations with JVM engineers at a conference this is not possible. The system will not let you install a SignalHandler on the SEGV signal.

like image 28
C. Ross Avatar answered Oct 30 '22 09:10

C. Ross