# How can I handle SIGINT when using libjulia C API (in Python ctypes)?

**URL:** <https://discourse.julialang.org/t/how-can-i-handle-sigint-when-using-libjulia-c-api-in-python-ctypes/14794>\
**Category:** Internals & Design\
**Tags:** question\
**Created:** [September 11, 2018, 2:06am UTC](https://discourse.julialang.org/t/how-can-i-handle-sigint-when-using-libjulia-c-api-in-python-ctypes/14794 "2018-09-11T02:06:21Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)\
**Post date:** [September 11, 2018, 2:06am UTC](https://discourse.julialang.org/t/how-can-i-handle-sigint-when-using-libjulia-c-api-in-python-ctypes/14794/1 "2018-09-11T02:06:21Z")

</div>

After I initialize libjulia by calling `jl_init_with_image`, I found that libjulia takes over all SIGINT handlings. Is it correct? Is there any way for a host program (Python in my case) to handle the signals? I tried `--handle-signals=no` but it seems to have no effect.

Here is the sample program:

> <https://gist.github.com/tkf/91be62b2d38b7aeb2c15d157893bca86>

and a sample output is:

```julia
$ python libjulia_sigint_demo.py
Please hit Ctrl-C
^CGot a KeyboardInterrupt
Base.Sys.BINDIR = /home/takafumi/julia/julia-1.0.0/bin
libjulia_path = /home/takafumi/julia/julia-1.0.0/bin/../lib/libjulia.so.1
calling jl_init_with_image(/home/takafumi/julia/julia-1.0.0/bin, /home/takafumi/julia/julia-1.0.0/lib/julia/sys.so)
seems to work...
Please hit Ctrl-C
^Ctypeof(err) = InterruptException
9-element Array{Base.StackTraces.StackFrame,1}:
 process_events at libuv.jl:98 [inlined]
 wait() at event.jl:246
 wait(::Condition) at event.jl:46
 wait_readbyte(::Base.TTY, ::UInt8) at stream.jl:278
 #readuntil#477 at stream.jl:769 [inlined]
 #readuntil at none:0 [inlined]
 #readline#268(::Bool, ::Function, ::Base.TTY) at io.jl:370
 readline(::Base.TTY) at io.jl:370
 top-level scope at none:4

Please hit Ctrl-C
^C^C^C^C^C^CWARNING: Force throwing a SIGINT
fatal: error thrown and no exception handler available.
InterruptException()
__read at /usr/lib/libc.so.6 (unknown line)
_IO_file_underflow at /usr/lib/libc.so.6 (unknown line)
_IO_default_uflow at /usr/lib/libc.so.6 (unknown line)
_IO_getline_info at /usr/lib/libc.so.6 (unknown line)
fgets at /usr/lib/libc.so.6 (unknown line)
unknown function (ip: 0x7faeeb58bb57)
PyOS_StdioReadline at /usr/lib/libpython3.7m.so.1.0 (unknown line)
PyOS_Readline at /usr/lib/libpython3.7m.so.1.0 (unknown line)
unknown function (ip: 0x7faeeb5ad014)
_PyMethodDef_RawFastCallKeywords at /usr/lib/libpython3.7m.so.1.0 (unknown line)
_PyCFunction_FastCallKeywords at /usr/lib/libpython3.7m.so.1.0 (unknown line)
_PyEval_EvalFrameDefault at /usr/lib/libpython3.7m.so.1.0 (unknown line)
_PyFunction_FastCallKeywords at /usr/lib/libpython3.7m.so.1.0 (unknown line)
_PyEval_EvalFrameDefault at /usr/lib/libpython3.7m.so.1.0 (unknown line)
_PyEval_EvalCodeWithName at /usr/lib/libpython3.7m.so.1.0 (unknown line)
PyEval_EvalCodeEx at /usr/lib/libpython3.7m.so.1.0 (unknown line)
PyEval_EvalCode at /usr/lib/libpython3.7m.so.1.0 (unknown line)
unknown function (ip: 0x7faeeb73176f)
PyRun_FileExFlags at /usr/lib/libpython3.7m.so.1.0 (unknown line)
PyRun_SimpleFileExFlags at /usr/lib/libpython3.7m.so.1.0 (unknown line)
unknown function (ip: 0x7faeeb736a8e)
_Py_UnixMain at /usr/lib/libpython3.7m.so.1.0 (unknown line)
__libc_start_main at /usr/lib/libc.so.6 (unknown line)
_start at python (unknown line)
python libjulia_sigint_demo.py 4.17s user 1.45s system 63% cpu 8.801 total

```

Note that the first Ctrl-C works because it was before initializing libjulia. The second Ctrl-C also works because I’m inside `jl_eval_string`. The last Ctrl-C (fast repeated types) does not trigger the normal SIGINT handling of Python (`KeyboardInterrupt`) but it seems it’s rather via the “force exit” path (`WARNING: Force throwing a SIGINT`) in julia:

> <https://github.com/JuliaLang/julia/blob/6caabc9d8b0146ef0edf5749d890a270d6a3da7e/src/signals-unix.c#L391>

Edit: I tried `signal.pthread_sigmask(signal.SIG_UNBLOCK, [signal.SIGINT])` to unmask `SIGINT` but it didn’t work (= no `KeyboardInterrupt` with the last Ctrl-C). But it causes `julia` to ignore `SIGINT` as well.
