# How to interrupt (not kill) julia from the REPL (again)?

**URL:** <https://discourse.julialang.org/t/how-to-interrupt-not-kill-julia-from-the-repl-again/99753>\
**Category:** General Usage\
**Tags:** interruptexception\
**Created:** [June 2, 2023, 5:01am UTC](https://discourse.julialang.org/t/how-to-interrupt-not-kill-julia-from-the-repl-again/99753 "2023-06-02T05:01:26Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ryofurue](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ryofurue/32/24531_2.png) [@ryofurue](https://discourse.julialang.org/u/ryofurue)\
**Post date:** [June 2, 2023, 5:01am UTC](https://discourse.julialang.org/t/how-to-interrupt-not-kill-julia-from-the-repl-again/99753/1 "2023-06-02T05:01:26Z")

</div>

Instead of adding a question to the old thread

> [@How to interrupt (not kill) julia from the REPL?](https://discourse.julialang.org/t/how-to-interrupt-not-kill-julia-from-the-repl/22565):
>
> From the REPL, how do I interrupt execution of a function? Basically, I want to press some command key and cause it to throw an error that I can catch. For example: function interruptable\_loop() try while not\_done # some computation end catch println("function terminated by user") # cleanup end end From reading some old threads, I got the impression that (on Windows) pressing ctrl+c would do what I want. I find that it does nothing unless I press it four times ra…

from 2019 of almost the same title, I create a new Topic.

From the old thread, I learned that Ctrl-C doesn’t always work, for example, during I/O. So, my question is, how to specify the points where Ctrl-C is accepted?

```julia
while until_done
   # . . . Some julia code without I/O . . . 
   # . . . If you must interrupt, you want to interrupt here. . . .
   func_doing_intensive_io()
   # . . . You want to allow interruption here, too . . .
end

```

I run a program like this on the REPL but I’ve never succeeded to interrupt it, perhaps because the program spends 99% of the time in I/O.

I tried something like this:

```julia
try
while until_done
   # . . . Some julia code without I/O . . . 
   func_doing_intensive_io()
   sleep(2)
   # . . . . . .
end
catch e
   display(e)
   exit(1)
end

```

This one isn’t interruptible either. If `sleep()` is interruptible, the above code should work, I thought.

How are signals handled on REPL? Is there a mechanism like `trap` of Unix shells?

---

<div class="post-metadata">

**Author:** ![albheim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albheim/32/34660_2.png) [@albheim](https://discourse.julialang.org/u/albheim)\
**Post date:** [June 2, 2023, 6:22am UTC](https://discourse.julialang.org/t/how-to-interrupt-not-kill-julia-from-the-repl-again/99753/2 "2023-06-02T06:22:33Z")

</div>

Both sleep and print seems to be easily interruptable for me, do you have a more specific example that doesn’t work for you?

```julia
julia> for i in 1:10
       sleep(1)
       end
^CERROR: InterruptException:
Stacktrace:
 [1-6] ⋮ internal
     @ Base, Unknown
   [7] sleep
     @ ./asyncevent.jl:240 [inlined]
   [8] top-level scope
     @ ./REPL[1]:2
Use `err` to retrieve the full stack trace.

julia> while true
       print("\rHello")
       end
HelloERROR: InterruptException:
Stacktrace:
 [1-6] ⋮ internal
     @ Base, Unknown
   [7] print(xs::String)
     @ Base ./coreio.jl:3
   [8] top-level scope
     @ ./REPL[2]:2
Use `err` to retrieve the full stack trace.

```

---

<div class="post-metadata">

**Author:** ![ryofurue](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ryofurue/32/24531_2.png) [@ryofurue](https://discourse.julialang.org/u/ryofurue)\
**Post date:** [June 3, 2023, 1:07pm UTC](https://discourse.julialang.org/t/how-to-interrupt-not-kill-julia-from-the-repl-again/99753/3 "2023-06-03T13:07:45Z")

</div>

> [@albheim](#):
>
> Both sleep and print seems to be easily interruptable for me,

Thanks! You are right. In the following example, I confirm that.

> do you have a more specific example that doesn’t work for you?

Yes, I’ve successfully stripped my program down so that it clearly demonstrates my problem:

```julia
using Plots

function plot_slp()
  xax = -10:0.1:10
  display(plot(xax, sin.(xax)))
end

try
for t in 1:4
  plot_slp() # <- Comment out and the program is interruptible
  print("sleeping . . . "); flush(stdout)
  sleep(3)
  println("wake up."); flush(stdout)
end
catch e
  display(e)
end

```

1. Launch the REPL on a terminal window.
2. `include("this-sample-program.jl")`
3. Type Ctrl-C. (Make sure the signal is sent to the terminal, by clicking on it.) Doesn’t work.
4. On a text editor, comment out the plotting function.
5. `include("this-sample-program.jl")` on the julia prompt.
6. Ctrl-C doesn’t work.
7. Quit the window showing the plot.
8. `include("this-sample-program.jl")`
9. Now the program is interruptible.

That suggests that the presence of the plot window is blocking the interruption.

So, a workaround is: During the sleep, quit the plot window and then type Ctrl-C on the terminal. I’ve confirmed that this workaround works.

Well, we need a complete signal handling mechanism as in C and shells.

---

<div class="post-metadata">

**Author:** ![albheim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albheim/32/34660_2.png) [@albheim](https://discourse.julialang.org/u/albheim)\
**Post date:** [June 3, 2023, 2:39pm UTC](https://discourse.julialang.org/t/how-to-interrupt-not-kill-julia-from-the-repl-again/99753/4 "2023-06-03T14:39:01Z")

</div>

Yeah, seems inconsistent there. If I do Ctrl-C x 2 it stops, though I don’t really see why that should be needed. Maybe something with threads and the gui that affect it?

---

<div class="post-metadata">

**Author:** ![ryofurue](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ryofurue/32/24531_2.png) [@ryofurue](https://discourse.julialang.org/u/ryofurue)\
**Post date:** [June 3, 2023, 3:15pm UTC](https://discourse.julialang.org/t/how-to-interrupt-not-kill-julia-from-the-repl-again/99753/5 "2023-06-03T15:15:41Z")

</div>

> If I do Ctrl-C x 2 it stops

Which operating system are you on? I can never ever interrupt my program however many times I press Ctrl-C, as long as the plotting window is there.

I’m on macOS 13.4. I run the julia REPL on iTerm2, if that matters.

---

<div class="post-metadata">

**Author:** ![albheim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albheim/32/34660_2.png) [@albheim](https://discourse.julialang.org/u/albheim)\
**Post date:** [June 3, 2023, 4:35pm UTC](https://discourse.julialang.org/t/how-to-interrupt-not-kill-julia-from-the-repl-again/99753/6 "2023-06-03T16:35:55Z")

</div>

I tried it on wsl.

---

<div class="post-metadata">

**Author:** ![ryofurue](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ryofurue/32/24531_2.png) [@ryofurue](https://discourse.julialang.org/u/ryofurue)\
**Post date:** [June 5, 2023, 3:59am UTC](https://discourse.julialang.org/t/how-to-interrupt-not-kill-julia-from-the-repl-again/99753/7 "2023-06-05T03:59:56Z")

</div>

For a test, I just launched the julia REPL and sent SIGTERM to the julia REPL process, issuing `kill -INT <process id>` from another terminal. Then the julia REPL reported `InterruptException` and quitted.

I launched the julia REPL again and typed Ctrl+C but it didn’t quit. It just displayed “^C” at its prompt.

So, although I understand very little, I guess it’s julia’s command line which decides what to do with the key stroke Ctrl+C and somehow it doesn’t generate an exception when a plotting window (`gksqt`) exists. (For a normal shell prompt, it’s the tty facility which translates the key stroke to the INT signal.)
