# What makes a function interruptible with ctrl+c?

**URL:** <https://discourse.julialang.org/t/what-makes-a-function-interruptible-with-ctrl-c/86625>\
**Category:** General Usage\
**Created:** [September 1, 2022, 4:23am UTC](https://discourse.julialang.org/t/what-makes-a-function-interruptible-with-ctrl-c/86625 "2022-09-01T04:23:36Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [September 1, 2022, 4:23am UTC](https://discourse.julialang.org/t/what-makes-a-function-interruptible-with-ctrl-c/86625/1 "2022-09-01T04:23:36Z")

</div>

Ctrl+c fails to interrupt many functions. What makes a function interruptible?

---

<div class="post-metadata">

**Author:** ![tamasgal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamasgal/32/27946_2.png) [@tamasgal](https://discourse.julialang.org/u/tamasgal)\
**Post date:** [September 1, 2022, 6:52am UTC](https://discourse.julialang.org/t/what-makes-a-function-interruptible-with-ctrl-c/86625/2 "2022-09-01T06:52:18Z")

</div>

That’s a good question. Generally you can “try” to catch it using something like this:

```julia
    try
        ...
    catch err
        if typeof(err) == InterruptException
           ...
        end
    end

```

but it was always unpredictable in the past and it is still confusing (at least to me) in 1.8.0, see e.g.

- [Cannot safely exit Pluto on Windows + git bash · Issue #2136 · fonsp/Pluto.jl · GitHub](https://github.com/fonsp/Pluto.jl/issues/2136)
- [Ctrl-C in script: atexit is ignored](https://discourse.julialang.org/t/ctrl-c-in-script-atexit-is-ignored/82413)
- [Cannot catch Ctrl-C gracefully in this script - #4 by chunjiw](https://discourse.julialang.org/t/cannot-catch-ctrl-c-gracefully-in-this-script/79757/4)

and so on.

The problem is – as far as I understood – that the SIGINT is sometimes hold back by some other code which may prevent to reach your desired try-catch. It may even end up in nirvana so that the `InterruptException` is never bubbling up, although all background tasks have finished and the program is in an idle state waiting for any kind of input whatsoever.

I remember some discussions many years ago where more details were revealed but I cannot find them. Performance is btw. one of the reasons you cannot have instant interruption, since you would need to regularly check for signals during runtime, so it’s always a trade-off between those.

---

<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:** [September 1, 2022, 7:12am UTC](https://discourse.julialang.org/t/what-makes-a-function-interruptible-with-ctrl-c/86625/3 "2022-09-01T07:12:07Z")

</div>

> [@tamasgal](#):
>
> Performance is btw. one of the reasons you cannot have instant interruption, since you would need to regularly check for signals during runtime, so it’s always a trade-off between those.

Is there a parameter to tune this trade-off? Even if it has to be at the process-level, and not any more fine-grained, it would be useful to be able to say “I’m willing to take a performance hit for the sake of responsiveness to interrupts”.

---

<div class="post-metadata">

**Author:** ![Iulian.Cioarca](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iulian.cioarca/32/30166_2.png) [@Iulian.Cioarca](https://discourse.julialang.org/u/Iulian.Cioarca)\
**Post date:** [September 1, 2022, 8:50am UTC](https://discourse.julialang.org/t/what-makes-a-function-interruptible-with-ctrl-c/86625/4 "2022-09-01T08:50:04Z")

</div>

+1 on that, especially when you’re at the beginning exploring solutions to your problem. Many times I had to kill julia completely and then wait again and again for startup and compilation…

---

<div class="post-metadata">

**Author:** ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)\
**Post date:** [September 1, 2022, 9:08am UTC](https://discourse.julialang.org/t/what-makes-a-function-interruptible-with-ctrl-c/86625/5 "2022-09-01T09:08:27Z")

</div>

> [@digital\_carver](#):
>
> I’m willing to take a performance hit for the sake of responsiveness to interrupts”

I think if you add a call to `yield()` in any inner loops, that can help. But if those loops are in other package’s code then that’s not as feasible.

---

<div class="post-metadata">

**Author:** ![tamasgal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamasgal/32/27946_2.png) [@tamasgal](https://discourse.julialang.org/u/tamasgal)\
**Post date:** [September 1, 2022, 10:50am UTC](https://discourse.julialang.org/t/what-makes-a-function-interruptible-with-ctrl-c/86625/6 "2022-09-01T10:50:26Z")

</div>

The problem is that foreign code can get into states and loops where the interrupt is not checked, so I would guess there is no way, but I hope someone with more insights will answer `;)`
