# Signal Handlers

TERR supports installing signal handlers to catch illegal operations that occur in foreign code called via the `.C()` or `.Call()` functions.

A signal handler can catch such illegal operations as referencing an illegal memory location. If an illegal operation is caught by a signal handler, it generates an error "Unhandled exception in foreign function" rather than crashing the process. Currently, these signal handlers are disabled by default, because we found that they could possibly interfere with the signal handlers used by Java.

Sometimes when investigating unexpected failures, you might want to enable the signal handlers. You can enable signal handlers when starting the Spotfire Enterprise Runtime for R console application by specifying the option:

    --enable-signal-handlers

Alternatively, you can enable them by starting the engine from Java with the following engine parameter:

    TerrJava.startEngine("FFInterface.SignalHandlersEnabled=TRUE")

If you want to enable signal handling when using Java on Linux, it might be helpful to set the environment variable `LD_PRELOAD` so the signal handlers set up by TERR are "chained" after the Java signal handlers, as described in the URL http://www.oracle.com/technetwork/java/javase/signals-139944.html#gbzcz.

According to this URL, setting the environment variable `LD_PRELOAD` to <libjvm.so-directory-in-java-tree>/libjsig.so causes Java to link in the special libjsig.so library, which handles both Java signal handlers and native code with its own handlers. We have found several cases where this workaround solves the problem of embedding TERR within a complex Java application.