jnr / jnr-posix

Java Posix layer
Other
239 stars 91 forks source link

NativeClosureProxy - Callable is null #180

Open PhBastiani opened 1 year ago

PhBastiani commented 1 year ago

Hi,

What is wrong with this code ?

    POSIX posix = POSIXFactory.getPOSIX();
    posix.signal(Signal.SIGUSR1, new SignalHandler() {
      @Override
      public void handle(int i) {
        System.out.println("handle: " + i);
        }
    });

kill -s USR1 <PID> gives me a nullPointerException during the call of NativeClosureProxy.getCallable()

Tested on : Operating System: Red Hat Enterprise Linux Server 7.4 (Maipo) CPE OS Name: cpe:/o:redhat:enterprise_linux:7.4:GA:server Kernel: Linux 3.10.0-693.el7.x86_64 Architecture: x86-64

jnr.posix 3.0.29 (and 3.1.15')

Note : same exception with LibC part of jnr

PhBastiani commented 1 year ago

Any comments on this issue would be appreciated.

Is there another alternative to hijack a system signal ?

Best wishes, Philippe

headius commented 1 year ago

Sorry for the delay in responding!

Can you turn this into a runnable test or simple compilable project? That would help me investigate.

However... I suspect this is the JVM getting in your way. Most builds of OpenJDK include some logic that uses the USR1 and other signals for JVM features. In order to avoid those signals being trapped by user code (primarily from C) the logic to bind signals is hooked so that it passes through JDK logic first that can protect these signals.

You may be able to get this work by telling the JVM to reduce the signals it traps using the -XX:+UseAltSigs flag:

$ java -XX:+PrintFlagsFinal -version | grep UseAltSigs
     bool UseAltSigs                                = false                               {product}
openjdk version "1.8.0_352"
OpenJDK Runtime Environment (build 1.8.0_352-b08)
OpenJDK 64-Bit Server VM (build 25.352-b08, mixed mode)

See this article for more information: https://www.oracle.com/java/technologies/javase/signals.html#gbzcj

Some newer versions of Java may not block USR1 in this way, but I'm not sure which platforms or versions.

PhBastiani commented 1 year ago

Sorry, I couldn't investigate further...

$java -XX:+PrintFlagsFinal -version | grep UseAltSigs

java version "11.0.9" 2020-10-20 LTS Java(TM) SE Runtime Environment 18.9 (build 11.0.9+7-LTS) Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.9+7-LTS, mixed mode)

So, no UseAltSigs on this JVM ! But, I've the following flags bool AllowUserSignalHandlers = false {product} {default} bool ReduceSignalUsage = false {product} {default} bool UseSignalChaining = true {product} {default}

From my goldfish memory, I also tested with other signals (since in my outdated code I was using sun.misc.Signal.HUP...)