Cannot catch Ctrl-C gracefully in this script

chunjiw
Wed, 20 Apr 2022 21:15:21 +0000
General Usage

This is related to this issue.

Put the following into a script file.jl:

function try_interrupt()
    while true
        try
            sleep(1)
            println("trying...")
            @async 1+1
        catch e
            if typeof(e) <: InterruptException
                println("caught Interrupt")
                return
            end
        end
    end
    return
end

try_interrupt()

If I run the script in any of the following ways, I cannot gracefully interrupt the loop with Ctrl-C:

julia file.jl

julia -i file.jl

julia -e 'include(popfirst!(ARGS))' file.jl

The error looks like:

trying...
trying...
^Cfatal: error thrown and no exception handler available.
InterruptException()
jl_mutex_unlock at /buildworker/worker/package_linux64/build/src/julia_locks.h:129 [inlined]
jl_task_get_next at /buildworker/worker/package_linux64/build/src/partr.c:484
poptask at ./task.jl:827
wait at ./task.jl:836
task_done_hook at ./task.jl:544
_jl_invoke at /buildworker/worker/package_linux64/build/src/gf.c:2247 [inlined]
jl_apply_generic at /buildworker/worker/package_linux64/build/src/gf.c:2429
jl_apply at /buildworker/worker/package_linux64/build/src/julia.h:1788 [inlined]
jl_finish_task at /buildworker/worker/package_linux64/build/src/task.c:218
start_task at /buildworker/worker/package_linux64/build/src/task.c:888

Note that it works if I run the code in the REPL.

The last one is from the docs, where it specifically says I can use it to catch InterruptException.

This is my versioninfo():

Julia Version 1.7.2
Commit bf53498635 (2022-02-06 15:21 UTC)
Platform Info:
  OS: Linux (x86_64-pc-linux-gnu)
  CPU: Intel(R) Core(TM) i7-6850K CPU @ 3.60GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-12.0.1 (ORCJIT, broadwell)

Any help is welcome, thank you!

13 posts - 5 participants