Unit test fails from R

I copied this snipped from the unit tests:

test_interrupt(Goal) :-
        thread_create(run(Me, Goal), Id, []),
        thread_signal(Id, throw(stop)),
        (   between(1, 40, _),
            thread_property(Id, status(Status)),
            (   Status == running
            ->  sleep(0.05),
            ;   true
        ->  thread_join(Id, _),
            (   Status == exception(stop)
            ->  true
            ;   throw(error(unexpected_status(Status), _))

run(Parent, Goal) :-
        thread_send_message(Parent, running),

cp_zero_null :-
        open('/dev/zero', read, In, [type(binary)]),
        open('/dev/null', write, Out, [type(binary)]),
        call_cleanup(copy_stream_data(In, Out),
                     (   close(In),

test :-

The unit test runs fine from the bash (Ubuntu). However, the same thing fails when called from R package rswipl:

matthias@hp:~/test$ R
R version 4.2.2 Patched (2022-11-10 r83330) -- "Innocent and Trusting" (man, these childish nicknames...)
> library(rswipl)
> query(call("consult", "interrupt.pl"))
[1] TRUE
> submit()
list() # this just means that the consult succeeded.
> clear()
> query(call("test"))
[1] TRUE
> submit()
Error: no more error handlers available (recursive errors?); invoking 'abort' restart
Save workspace image? [y/n/c]: c
> *** stack smashing detected ***: terminated
ERROR: Received fatal signal 11 (segv)
Time: Tue May 14 08:57:58 2024
Inferences: 11
Thread: 2 ()
C-stack trace labeled "crash":
  [0] save_backtrace() at /tmp/RtmpC3fY47/R.INSTALL6435408257b7/rswipl/src/swipl-devel/src/os/pl-cstack.c:335 [0x7fc230306131]
  [1] sigCrashHandler() at /tmp/RtmpC3fY47/R.INSTALL6435408257b7/rswipl/src/swipl-devel/src/os/pl-cstack.c:937 [0x7fc230306589]
  [2] __restore_rt() at libc_sigaction.c:? [0x7fc23449f050]
  [3] __memcpy_avx_unaligned_erms() at ./string/../sysdeps/x86_64/multiarch/memmove-vec-unaligned-erms.S:531 [0x7fc2345b598c]
  [4] __libc_message() at ./libio/../sysdeps/posix/libc_fatal.c:138 (discriminator 3) [0x7fc2344e240a]

Segmentation fault

This is a bit of a special case, but maybe someone can help debugging.

Could be related to either handling Prolog threads or Prolog signal handling. I’d first try using gdb with a breakpoint on the R error handling function (if you can find that).

Do other thread-tests pass? If so, does the R documentation say anything about signal handling in foreign code? See also SWI-Prolog -- Command line options for running Prolog, options --no-signals and --sigalert. If you disable using sigalert this test probably fails. You can try using another signal though.

I have disabled sigalert, as suggested. I’ll submit a PR in which the test is disabled if prolog_alert_signal(0, 0) succeeds.

Edit: For reasons not yet fully understood, the test passes even with --no-signals.