9

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.

Community
  • 1
  • 1
C. Ross
  • 31,137
  • 42
  • 147
  • 238
  • Are public debugging symbols available for the native code? If you can find more about why it's happening, you might be able to submit a comprehensive enough bug report to get them to fix their library. – Benj Jul 04 '12 at 20:04
  • @Benj Working on that in parallel. They'll be getting more info than they want. Was hoping to have a backup plan. – C. Ross Jul 04 '12 at 20:07
  • For example, on Windows you can use structured exception handling (SEH) to catch a seg fault. If you could write a native wrapper which calls their code that might be an option. It really depends on the reason for the SEGV though, a bad pointer dereference can be caught this way, but heap/stack corruption can also a SEGV and catching this is likely to lead to more issues. – Benj Jul 04 '12 at 20:12
  • @Benj AIX 64bit, using the IBM 64bit JVM. – C. Ross Jul 04 '12 at 20:15
  • @Benj I don't really want to do anything on catching a failure besides log it and notify. – C. Ross Jul 04 '12 at 20:26
  • 1
    If you'ved created the JVM yourself using JNI, it appears you could log something on abnormal termination: http://www.ibm.com/developerworks/java/library/i-signalhandling/ – Benj Jul 04 '12 at 20:28

2 Answers2

2

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.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • 1
    Some significant performance implications with doing this... It'd have to be the only option left to tempt me to go this way. – Benj Jul 04 '12 at 20:03
1

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.

C. Ross
  • 31,137
  • 42
  • 147
  • 238