# Async hangs with continue

**URL:** <https://discourse.julialang.org/t/async-hangs-with-continue/114472>\
**Category:** General Usage\
**Created:** [May 20, 2024, 10:09am UTC](https://discourse.julialang.org/t/async-hangs-with-continue/114472 "2024-05-20T10:09:50Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![lstagner](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lstagner/32/448_2.png) [@lstagner](https://discourse.julialang.org/u/lstagner)\
**Post date:** [May 20, 2024, 10:09am UTC](https://discourse.julialang.org/t/async-hangs-with-continue/114472/1 "2024-05-20T10:09:50Z")

</div>

I have a function that listens to a redis stream in a while loop but it hangs and doesnt release back to the REPL. I managed to create a MWE.

```julia
function foo()
    i = 0
    while true
        if i==0
            continue # hangs
            #nothing # doesnt hang
        end
        println(i)
        sleep(5)
        i = i+1
   end
end

@async foo() # this hangs

```

---

<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:** [May 20, 2024, 10:32am UTC](https://discourse.julialang.org/t/async-hangs-with-continue/114472/2 "2024-05-20T10:32:42Z")

</div>

In your example, `i` is always `0`, so the branch with `continue` is always hit. If you want your code to yield to some other asynchronous code, explicitly call `yield()`.

Also, how many threads are you starting julia with and what version are you running? On some older versions, the REPL didn’t run on its own thread by default, so if another task hogs the sole thread without yielding, it’ll never regain control by itself.

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [May 20, 2024, 11:17am UTC](https://discourse.julialang.org/t/async-hangs-with-continue/114472/3 "2024-05-20T11:17:24Z")

</div>

> [@lstagner](#):
>
> ```julia
> while true
> if i==0
> continue # hangs
> 
> ```

In other words, your code is stuck looping these lines indefinitely, you never reach the `sleep` that goes back to the task queue. You probably intended the `i = i+1` to happen earlier, maybe at the very beginning of the `while true` loop (you sure you want that to go on forever though?).

---

<div class="post-metadata">

**Author:** ![lstagner](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lstagner/32/448_2.png) [@lstagner](https://discourse.julialang.org/u/lstagner)\
**Post date:** [May 20, 2024, 5:05pm UTC](https://discourse.julialang.org/t/async-hangs-with-continue/114472/4 "2024-05-20T17:05:36Z")

</div>

The expected behavior would be that the function `foo` would just get put into a task and move on, freeing up the REPL.

I run into the same issue if I do

```julia
julia> function foo()
       i = 0
       while true
           i += 1
       end
       end
foo (generic function with 1 method)

julia> @async foo() #hangs

```

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [May 20, 2024, 5:25pm UTC](https://discourse.julialang.org/t/async-hangs-with-continue/114472/5 "2024-05-20T17:25:38Z")

</div>

Your REPL is freed, it just yields to the scheduler when idle and ends up running your task, which has nothing in the loop or the method that would yield back to the scheduler.

---

<div class="post-metadata">

**Author:** ![Satvik](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/satvik/32/20486_2.png) [@Satvik](https://discourse.julialang.org/u/Satvik)\
**Post date:** [May 20, 2024, 5:32pm UTC](https://discourse.julialang.org/t/async-hangs-with-continue/114472/6 "2024-05-20T17:32:25Z")

</div>

> [@lstagner](#):
>
> ```julia
> function foo()
> i = 0
> while true
> i += 1
> end
> end
> 
> ```

`foo()` is getting put into a task, but it runs on the main thread, and it’s fast enough to use the thread’s whole resources, which starves the thread for the REPL. If you use something like

```julia
function foo()
       i = 0
       while true
           sleep(.1)
       end
       end

```

then you’ll see control returned to the REPL as promised.

Julia uses cooperative scheduling, so by default you’re not going to see anything interrupt `foo()` if it continuously has work to do.

You could start it on another thread:

```julia
Threads.@spawn foo()

```

Or use an explicit yield() (the task will still keep working whenever there’s nothing else to do):

```julia
function yielding_foo()
    i = 0
    while true
        i = i + 1
        yield()
    end
end

@async yielding_foo()

```
