# Is there a macro to make Ctrl+C work in REPL?

**URL:** <https://discourse.julialang.org/t/is-there-a-macro-to-make-ctrl-c-work-in-repl/88794>\
**Category:** New to Julia\
**Tags:** interruptexception\
**Created:** [October 16, 2022, 7:42am UTC](https://discourse.julialang.org/t/is-there-a-macro-to-make-ctrl-c-work-in-repl/88794 "2022-10-16T07:42:06Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![HugoTrentesaux](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hugotrentesaux/32/12583_2.png) [@HugoTrentesaux](https://discourse.julialang.org/u/HugoTrentesaux)\
**Post date:** [October 16, 2022, 7:42am UTC](https://discourse.julialang.org/t/is-there-a-macro-to-make-ctrl-c-work-in-repl/88794/1 "2022-10-16T07:42:06Z")

</div>

This topic shows that Julia REPL can not be interrupted : [How to interrupt (not kill) julia from the REPL?](https://discourse.julialang.org/t/how-to-interrupt-not-kill-julia-from-the-repl/22565)

Is there a decorator to make a loop able to be interrupted from `Ctrl+C` in the REPL?

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [October 16, 2022, 8:12am UTC](https://discourse.julialang.org/t/is-there-a-macro-to-make-ctrl-c-work-in-repl/88794/2 "2022-10-16T08:12:52Z")

</div>

Julia does not have python-style decorators - perhaps you’re thinking of a macro?

In any case, you can try to write your loop like

```julia
for x in y
    try
       # loop body
    catch e 
       e isa InterruptException && break
       rethrow(e) # so we don't swallow true exceptions
    end
end

```

But be aware that this is not a surefire way of achieving this (and I’m fairly certain it’s not guaranteed to work, depending on the loop body). For example, the interrupt can happen at any point in time, so also when the iterator state is advanced (which is not covered by this) or in another task (should there be another task running at the same time, either spawned before this loop runs or spawned in the loop itself).

---

<div class="post-metadata">

**Author:** ![HugoTrentesaux](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hugotrentesaux/32/12583_2.png) [@HugoTrentesaux](https://discourse.julialang.org/u/HugoTrentesaux)\
**Post date:** [October 16, 2022, 9:52am UTC](https://discourse.julialang.org/t/is-there-a-macro-to-make-ctrl-c-work-in-repl/88794/3 "2022-10-16T09:52:32Z")

</div>

Yes, sorry for the name, I was thinking about a macro.

Doing

```julia
while true
    try
        # nothing in the loop
    catch e
        # in any case
        break
    end
end

```

does not allow to interrupt with `Ctrl+C`.

How can we achieve this and put this in a macro so that we can write

```julia
@allow_interrupt while true
    # nothing
end

```

And interrupt the loop with `Ctrl+C`?

---

<div class="post-metadata">

**Author:** ![digital\_carver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/digital_carver/32/33818_2.png) [@digital\_carver](https://discourse.julialang.org/u/digital_carver)\
**Post date:** [October 16, 2022, 10:18am UTC](https://discourse.julialang.org/t/is-there-a-macro-to-make-ctrl-c-work-in-repl/88794/4 "2022-10-16T10:18:27Z")

</div>

Calling `yield()` from within the loop is the generally recommended option for this. It’s not a 100% guarantee as I understand it, but makes it much more likely that the interrupt comes through. See for eg.

> [@What to do when Julia runs away from you? (how to interrupt on C-c?)](https://discourse.julialang.org/t/what-to-do-when-julia-runs-away-from-you-how-to-interrupt-on-c-c/85310):
>
> I’m currently working on a model that somehow get’s in a solving loop that I can’t cancel with Ctrl+C. To prove this I’m printing to the console from within the while loop that is running away. I can see the print statement reporting the ever increasing index to the console, but my keyboard input of Ctrl+C is ignored. Is there another way to interrupt Julia? My only solution at the moment is to kill Julia, but this is not ideal since I need to spend minutes re-compiling the current project.

> [@Interruptible routine: the routine does not receive SIGINT if there is no gap in the loop](https://discourse.julialang.org/t/interruptible-routine-the-routine-does-not-receive-sigint-if-there-is-no-gap-in-the-loop/72746):
>
> Hi all, I am learning to create an Interruptible routine for my camera sensor. My goal is to run an image acquisition routine on a remote process so that the local process can receive command from the user to perform other tasks. I start with this simple example. The routine creates a file, opens it and writes something into it at a certain period as shown below (Please note that the code in the while loop can be anything, I picked writing to a file as an example) @everywhere function worker\_f…

> [@What makes a function interruptible with ctrl+c?](https://discourse.julialang.org/t/what-makes-a-function-interruptible-with-ctrl-c/86625):
>
> Ctrl+c fails to interrupt many functions. What makes a function interruptible?

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [October 16, 2022, 10:37am UTC](https://discourse.julialang.org/t/is-there-a-macro-to-make-ctrl-c-work-in-repl/88794/5 "2022-10-16T10:37:22Z")

</div>

> [@HugoTrentesaux](#):
>
> does not allow to interrupt with `Ctrl+C`.

That seems to be a bug - you can force throw a `SIGINT` by repeatedly pressing `Ctrl+C`.

Regardless, it’s not reliable as it can happen at any time - it’s usually preferable to have an explicit synchronization point where an external signal is checked for being set or not.

---

<div class="post-metadata">

**Author:** ![blackeneth](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/blackeneth/32/10353_2.png) [@blackeneth](https://discourse.julialang.org/u/blackeneth)\
**Post date:** [October 16, 2022, 3:42pm UTC](https://discourse.julialang.org/t/is-there-a-macro-to-make-ctrl-c-work-in-repl/88794/6 "2022-10-16T15:42:57Z")

</div>

Take a look at the docs [here](https://docs.julialang.org/en/v1/base/c/)

Haven’t tried it, but looks promising.

---

<div class="post-metadata">

**Author:** ![HugoTrentesaux](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hugotrentesaux/32/12583_2.png) [@HugoTrentesaux](https://discourse.julialang.org/u/HugoTrentesaux)\
**Post date:** [October 16, 2022, 4:58pm UTC](https://discourse.julialang.org/t/is-there-a-macro-to-make-ctrl-c-work-in-repl/88794/7 "2022-10-16T16:58:47Z")

</div>

`disable_sigint` takes a function, I have no idea what to give it so that I can `Ctrl-C` a `while` loop.
