# Code intermittently not being interruptible

**URL:** <https://discourse.julialang.org/t/code-intermittently-not-being-interruptible/88881>\
**Category:** General Usage\
**Created:** [October 18, 2022, 3:20am UTC](https://discourse.julialang.org/t/code-intermittently-not-being-interruptible/88881 "2022-10-18T03:20:58Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nathaniel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathaniel/32/37854_2.png) [@Nathaniel](https://discourse.julialang.org/u/Nathaniel)\
**Post date:** [October 18, 2022, 3:20am UTC](https://discourse.julialang.org/t/code-intermittently-not-being-interruptible/88881/1 "2022-10-18T03:20:59Z")

</div>

I’ve found that I sometimes can’t interrupt my code using ctrl-C or CMD-dot.

The following was working for me earlier today as a minimal example:

```julia
using Plots

function main()
    while true
        plt = plot(rand(100))
        display(plt)
        println("here")
    end
end

main()

```

I was putting this in a file called `mwe.jl` and then running it with

```julia
julia -i mwe.jl

```

Pressing CMD-. or ctrl-C would print the characters “^C” to the screen but not interrupt the code. The only way I could stop it was by pressing ctrl-Z and then ending the stopped job - I couldn’t find any way to get back to the Julia prompt without starting a new julia process.

But then at some point it started working again, and now I can interrupt it as expected. I don’t know what I did that might have changed that. (I _think_ what I did was remove the println to see if it would work without it. After that it seemed to work even with the println. But it doesn’t seem very likely that that would be what fixed the issue.)

Is this a known issue, and if so, is there anything I can do to guarantee that my code can be interrupted? It’s kind of annoying to have to suspend the job every time when interrupting decides not to work.

I’m on MacOS (version 12.6 Monterey) if that makes any difference, and running Julia 1.8.1.

---

<div class="post-metadata">

**Author:** ![fins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fins/32/43444_2.png) [@fins](https://discourse.julialang.org/u/fins)\
**Post date:** [October 18, 2022, 7:00am UTC](https://discourse.julialang.org/t/code-intermittently-not-being-interruptible/88881/2 "2022-10-18T07:00:17Z")

</div>

not sure ,if there is a better way, but you can put a [`yield()`](https://docs.julialang.org/en/v1/base/parallel/#Scheduling-1) somewhere in that loop.

---

<div class="post-metadata">

**Author:** ![Nathaniel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathaniel/32/37854_2.png) [@Nathaniel](https://discourse.julialang.org/u/Nathaniel)\
**Post date:** [October 26, 2022, 8:20am UTC](https://discourse.julialang.org/t/code-intermittently-not-being-interruptible/88881/3 "2022-10-26T08:20:10Z")

</div>

(sorry for replying late, I missed your message.) I _think_ I tried putting the `yield()` in the loop, with no effect, but the next time this happens I will make sure of that.

---

<div class="post-metadata">

**Author:** ![Nathaniel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathaniel/32/37854_2.png) [@Nathaniel](https://discourse.julialang.org/u/Nathaniel)\
**Post date:** [April 18, 2023, 4:09am UTC](https://discourse.julialang.org/t/code-intermittently-not-being-interruptible/88881/4 "2023-04-18T04:09:45Z")

</div>

I am having the same problem again recently, and adding in yield() doesn’t help.

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [April 18, 2023, 5:10am UTC](https://discourse.julialang.org/t/code-intermittently-not-being-interruptible/88881/5 "2023-04-18T05:10:02Z")

</div>

How about `sleep(0)` ?

---

<div class="post-metadata">

**Author:** ![anon56330260](https://avatars.discourse-cdn.com/v4/letter/a/f07891/32.png) [@anon56330260](https://discourse.julialang.org/u/anon56330260)\
**Post date:** [April 18, 2023, 5:53am UTC](https://discourse.julialang.org/t/code-intermittently-not-being-interruptible/88881/6 "2023-04-18T05:53:38Z")

</div>

Can confirm this on Julia 1.8. It seems that the sigint signal is blocked and ignored, even if `yield/print/sleep` is added manually. This might be related to the plot window. Consider this code:

```julia
function main()
    close = true
    while true
        if close
            close = false
            plt = plot(rand(100))
            display(plt)
        end
        println("here")
    end
end

```

Only after you close the plot window can you interrupt the loop. When the window is opened, the interrupt is simply ignored.

EDIT : I also test this with GR.jl’s plot function and the same issue can be reproduced.
