# Cannot catch Ctrl-C gracefully in this script

**URL:** https://discourse.julialang.org/t/cannot-catch-ctrl-c-gracefully-in-this-script/79757
**Category:** General Usage
**Tags:** question, interruptexception
**Created:** [April 20, 2022, 9:15pm UTC](https://discourse.julialang.org/t/cannot-catch-ctrl-c-gracefully-in-this-script/79757 "2022-04-20T21:15:21Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![chunjiw](https://avatars.discourse-cdn.com/v4/letter/c/e79b87/32.png) [@chunjiw](https://discourse.julialang.org/u/chunjiw)
#### Post date: [April 20, 2022, 9:15pm UTC](https://discourse.julialang.org/t/cannot-catch-ctrl-c-gracefully-in-this-script/79757/1 "2022-04-20T21:15:22Z")

</div>

This is related to this [issue](https://github.com/JuliaLang/julia/issues/19467).

Put the following into a script `file.jl`:

```julia
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:

```julia
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](https://docs.julialang.org/en/v1/manual/faq/#catch-ctrl-c), where it specifically says I can use it to catch `InterruptException`.

This is my `versioninfo()`:

```julia
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!

---

<div class="post-metadata">

### Author: ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)
#### Post date: [April 20, 2022, 9:34pm UTC](https://discourse.julialang.org/t/cannot-catch-ctrl-c-gracefully-in-this-script/79757/2 "2022-04-20T21:34:14Z")

</div>

Seems to work for me on (Windows) Julia 1.7.2 and Julia 1.8.0-beta3, i.e. not reproducible here. Sorry for not being helpful.

Edit: try to clarify.

---

<div class="post-metadata">

### Author: ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)
#### Post date: [April 20, 2022, 11:53pm UTC](https://discourse.julialang.org/t/cannot-catch-ctrl-c-gracefully-in-this-script/79757/3 "2022-04-20T23:53:28Z")

</div>

Despite you naming it try\_segfault, I don’t think this is actually a segment[ation] fault, but that’s a nitpick (in your case, nor mine despite “unknown function (ip: (nil))” which I have and you don’t; I don’t think you would get a stack-trace, for a segfault?).

You might want to open an issue about this, but just give the facts. Please check this in the latest 1.9 master (and 1.6.5), and report against it. Julia 1.8 should be out any day now, so it’s likely not helpful to ask for a backport to 1.7.x, but to 1.8.x would be good, and to 1.6.x. I didn’t check 1.6.5 or 1.7.2.

I tried your script or even an empty infinite loop, starting with:

```julia
atexit(exit)

```

I got similar results. Note, not with `atexit(exit())` # then I got a confusing, at first, error.

I’m on Linux, and I get similar as you in my julia-1.9-DEV-76cf1761e6 (and also in 1.3.1):

```julia
 trying...
^C
signal (2): Interrupt
in expression starting at /home/pharaldsson_sym/gdp_speed/gdp_speeds/ctrlc.jl:25
epoll_wait at /lib/x86_64-linux-gnu/libc.so.6 (unknown line)
uv__io_poll at /workspace/srcdir/libuv/src/unix/epoll.c:240
uv_run at /workspace/srcdir/libuv/src/unix/core.c:383
ijl_task_get_next at /buildworker/worker/package_linux64/build/src/partr.c:556
poptask at ./task.jl:884
wait at ./task.jl:893
task_done_hook at ./task.jl:597
jfptr_task_done_hook_54049.clone_1 at /home/pharaldsson_sym/julia-1.9-DEV-76cf1761e6/lib/julia/sys.so (unknown line)
_jl_invoke at /buildworker/worker/package_linux64/build/src/gf.c:2349 [inlined]
ijl_apply_generic at /buildworker/worker/package_linux64/build/src/gf.c:2531
jl_apply at /buildworker/worker/package_linux64/build/src/julia.h:1825 [inlined]
jl_finish_task at /buildworker/worker/package_linux64/build/src/task.c:254
start_task at /buildworker/worker/package_linux64/build/src/task.c:942
unknown function (ip: (nil))
Allocations: 2904 (Pool: 2892; Big: 12); GC: 0

```

What was the point of `@async` line? Then I get a longer error, when you report you may want to omit it, or show with if it’s supposed to be there, or maybe show both.

Note, I reported a bug previously about CTRL-C not working, and it didn’t for your code in my Julia 1.7.0:

> <https://github.com/JuliaLang/julia/issues/43103>
>
> It's a regression from rc1 at least, there CRTL-C worked.
> 
> With this program:
> …
> https://benchmarksgame-team.pages.debian.net/benchmarksgame/program/binarytrees-julia-3.html
> 
> \`\`\`
> $ ~/julia-1.7.0-rc3/bin/julia -O3 --cpu-target=ivybridge --math-mode=ieee -- bt.jl 21
> stretch tree of depth 22 check: 8388607
> ^C
> \`\`\`
> 
> the program hangs over a minute and counting. Without pressing CTRL-C it finishes in 13 sec. max.
> 
> I believe there's no guarantee that CTRL-C always working (nor kill, is that the same signal?), but it's an annoying regression (and misleads while benchmarking if not realized), and it seems if Julia isn't listening, then it should at least be ignored.
> 
> I may be getting slightly better speed though, with 0m12,267s for rc3 vs 0m12,705s for rc1, Or rather random load effect as later I got 0m12,385s.

That issue was closed, so I assumed fixed, but maybe not. See also the open issue linked from it at the bottom. Please report what you think relevant and maybe link to this discussion.

> [@goerch](#):
>
> Seems to work for me on (Windows)

If you mean as intended, just exiting, i.e. not working the same, with the error, that’s also relevant info to report.

---

<div class="post-metadata">

### Author: ![chunjiw](https://avatars.discourse-cdn.com/v4/letter/c/e79b87/32.png) [@chunjiw](https://discourse.julialang.org/u/chunjiw)
#### Post date: [April 21, 2022, 6:01pm UTC](https://discourse.julialang.org/t/cannot-catch-ctrl-c-gracefully-in-this-script/79757/4 "2022-04-21T18:01:54Z")

</div>

> Despite you naming it try\_segfault, I don’t think this is actually a segment[ation] fault

You are right, I edited the post

> What was the point of `@async` line?

Good question, I don’t know… I got the code from [here](https://github.com/JuliaLang/julia/issues/19467). I will do more tests both with and without it.

I will open an issue about this. Thank you for your suggestions!

---

<div class="post-metadata">

### Author: ![chunjiw](https://avatars.discourse-cdn.com/v4/letter/c/e79b87/32.png) [@chunjiw](https://discourse.julialang.org/u/chunjiw)
#### Post date: [April 21, 2022, 8:22pm UTC](https://discourse.julialang.org/t/cannot-catch-ctrl-c-gracefully-in-this-script/79757/5 "2022-04-21T20:22:49Z")

</div>

Because running a script does not throw [`InterruptException`](https://docs.julialang.org/en/v1/base/base/#Core.InterruptException) when you try to terminate it with CTRL-C (SIGINT) [(docs)](https://docs.julialang.org/en/v1/manual/faq/#catch-ctrl-c), you got the error even without @async line. To avoid it, use `julia -i "file.jl"` or `julia -e 'include(popfirst!(ARGS))' file.jl`.

The @async line has to be there to have the error using `julia -i "file.jl"`

---

<div class="post-metadata">

### Author: ![chunjiw](https://avatars.discourse-cdn.com/v4/letter/c/e79b87/32.png) [@chunjiw](https://discourse.julialang.org/u/chunjiw)
#### Post date: [April 26, 2022, 10:37pm UTC](https://discourse.julialang.org/t/cannot-catch-ctrl-c-gracefully-in-this-script/79757/6 "2022-04-26T22:37:09Z")

</div>

This is the opened issue on Github: [https://github.com/JuliaLang/julia/issues/45055](https://github.com/JuliaLang/julia/issues/45055)

---

<div class="post-metadata">

### Author: ![bluesmoon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bluesmoon/32/327_2.png) [@bluesmoon](https://discourse.julialang.org/u/bluesmoon)
#### Post date: [October 30, 2023, 3:28pm UTC](https://discourse.julialang.org/t/cannot-catch-ctrl-c-gracefully-in-this-script/79757/7 "2023-10-30T15:28:59Z")

</div>

Have you tried running `julia --handle-signals=no` to disable julia’s own signal handling?

---

<div class="post-metadata">

### Author: ![chunjiw](https://avatars.discourse-cdn.com/v4/letter/c/e79b87/32.png) [@chunjiw](https://discourse.julialang.org/u/chunjiw)
#### Post date: [December 27, 2023, 10:46pm UTC](https://discourse.julialang.org/t/cannot-catch-ctrl-c-gracefully-in-this-script/79757/8 "2023-12-27T22:46:17Z")

</div>

I just tried with just released 1.10.0, `julia --handle-signals=no -i file.jl` and I get this error:

```julia
trying...
trying...
^CError: Maybe we should never reach this?

```

Is this better? I honestly don’t know …

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [December 27, 2023, 10:54pm UTC](https://discourse.julialang.org/t/cannot-catch-ctrl-c-gracefully-in-this-script/79757/9 "2023-12-27T22:54:11Z")

</div>

Linking [this related post](https://discourse.julialang.org/t/segmentation-fault-when-a-try-to-catch-a-interruptexception/2285/7) in case it helps.

---

<div class="post-metadata">

### Author: ![chunjiw](https://avatars.discourse-cdn.com/v4/letter/c/e79b87/32.png) [@chunjiw](https://discourse.julialang.org/u/chunjiw)
#### Post date: [December 27, 2023, 11:28pm UTC](https://discourse.julialang.org/t/cannot-catch-ctrl-c-gracefully-in-this-script/79757/10 "2023-12-27T23:28:24Z")

</div>

Thank you for the suggestion. I need to change `Void` to `Cvoid` to execute the file (is it correct?). But then the problem persists.

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [December 28, 2023, 7:42am UTC](https://discourse.julialang.org/t/cannot-catch-ctrl-c-gracefully-in-this-script/79757/11 "2023-12-28T07:42:58Z")

</div>

Perhaps try:

```julia
ccall(:jl_exit_on_sigint, Nothing, (Cint,), 0)

```

---

<div class="post-metadata">

### Author: ![chunjiw](https://avatars.discourse-cdn.com/v4/letter/c/e79b87/32.png) [@chunjiw](https://discourse.julialang.org/u/chunjiw)
#### Post date: [December 29, 2023, 12:40am UTC](https://discourse.julialang.org/t/cannot-catch-ctrl-c-gracefully-in-this-script/79757/12 "2023-12-29T00:40:07Z")

</div>

I tried but the problem persists.

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [December 31, 2023, 10:54am UTC](https://discourse.julialang.org/t/cannot-catch-ctrl-c-gracefully-in-this-script/79757/13 "2023-12-31T10:54:03Z")

</div>

Sorry for the bad pointer.

To handle the `@async` case, I don’t know what the Julia equivalent of [Python’s asyncio.run()](https://stackoverflow.com/a/72333069/14071775) solution is.
