Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reinstall GAP SIGINT handler when Julia is started #3256

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/julia_gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "gasman.h"
#include "objects.h"
#include "plist.h"
#include "sysfiles.h"
#include "sysmem.h"
#include "system.h"
#include "vars.h"
Expand Down Expand Up @@ -744,6 +745,8 @@ void InitBags(UInt initial_size, Bag * stack_bottom, UInt stack_align)
Panic("could not read GapObj variable from Julia");
}

SyInstallAnswerIntr();
Copy link
Member

@fingolfin fingolfin Feb 1, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, so let's see, this is the code for SyInstallAnswerIntr:

void SyInstallAnswerIntr ( void )
{
    if ( signal( SIGINT, SIG_IGN ) != SIG_IGN )
    {
        signal( SIGINT, syAnswerIntr );
        siginterrupt( SIGINT, 0 );
    }
}

This doesn't do anything if there already is a SIGINT handler installed, does it? But doesn't Julia install a SIGINT handler? Since I assume you tested this, the answer seems to be no; yet Julia's doc/src/devdocs/init.md says this:

Finally sigint_handler() is hooked up to SIGINT and calls jl_throw(jl_interrupt_exception).

Digging some more into signal-unix.c also reveals this:

        if (sig == SIGINT) {
            if (jl_ignore_sigint()) {
                continue;
            }
            else if (exit_on_sigint) {
                critical = 1;
            }
            else {
                jl_try_deliver_sigint();
                continue;
            }
        }

Which suggests to me that it does handle SIGINT, just not via signal() but rather via sigprocmask...
(Funny story on the side: On macOS, the relevant C file is signals-mach.c, which also defines e.g. static void jl_try_deliver_sigint(), but never calls it, and seems to contain no provisions at all to handle SIGINT ?!? And finally signals-win.c does have some, but written in a completely different style?!?). Of course libuv might also be involved somehow?

All of this is to say: I don't quite understand why the above works; and I am slightly worried that it might cause unexpected problems... Like, what happens if our custom SIGINT handler is triggered while we are inside Julia code? Could this leave Julia in an undefined state?

So I guess what I'd like to know is what kind of tests you performed with this modified code... Like, I assume if one starts GAP and loads JuliaInterface in it, but does not use it, everything works fine (Ctrl-C in GAP code enters a break loop). Correct? And what happens if one presses Ctrl-C while Julia code is running? And what happens when one starts Julia and loads GAPJulia, and then presses Ctrl-C, either while Julia code is running, or while GAP code is running? (And I guess ultimately, we also need to think about more deeply nested callchains with ...->GAP code->Julia code->GAP->Julia->...)

None of this is meant to kill of this PR, I simply would like to understand better what it does and why it works, and then perhaps we can put some of it in a comment and/or the commit message?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One problem that you may be running into is that on macOS, Julia does not install a signal handler, but goes directly for the Mach system calls. When they do that, normal signal handling is subsequently ignored. There are ways to insert yourself in the signal handling process in Julia, but I haven't delved too deep into those.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just realized that signals-mach.c is included by signals-posix.c, and now feel dumb for not considering this possibility earlier 🤣


JuliaTLS = jl_get_ptls_states();
// These callbacks potentially require access to the Julia
// TLS and thus need to be installed after initialization.
Expand Down