# Can you find where this asynchronous program get stuck at?

**URL:** https://discourse.julialang.org/t/can-you-find-where-this-asynchronous-program-get-stuck-at/131907
**Category:** General Usage
**Tags:** question, async
**Created:** [August 28, 2025, 7:41am UTC](https://discourse.julialang.org/t/can-you-find-where-this-asynchronous-program-get-stuck-at/131907 "2025-08-28T07:41:40Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![WalterMadelim](https://avatars.discourse-cdn.com/v4/letter/w/3e96dc/32.png) [@WalterMadelim](https://discourse.julialang.org/u/WalterMadelim)
#### Post date: [August 28, 2025, 7:41am UTC](https://discourse.julialang.org/t/can-you-find-where-this-asynchronous-program-get-stuck-at/131907/1 "2025-08-28T07:41:40Z")

</div>

I wrote a weird `test` function

```julia
function test()
    function taskbody(k, x, j, e)
        sleep(0.5 * j)
        println("At k = $k, j = $j, x = $x")
        @lock mylock i+=1
        notify(e)
    end
    my_lock = Threads.ReentrantLock()
    i = 0
    e = Base.Event()
    for k = 1
        x = [-10i, -i, i, 10i] # update!
        t_vec = let e = e
            [@task taskbody(k, x, j, e) for j = 1:7]
        end
        schedule.(t_vec)
        wait(e) # any j can remove this block
        error("Can you reach here?")
    end
end;

```

You see, I miswrote `my_lock` as `mylock` carelessly. But surprisingly I cannot see an ERROR!

```julia
julia> Threads.nthreads(:default), Threads.nthreads(:interactive)
(8, 0)

julia> @isdefined mylock
false

julia> # load the definition of `test` above

julia> test()
At k = 1, j = 1, x = [0, 0, 0, 0]
At k = 1, j = 2, x = [0, 0, 0, 0]
At k = 1, j = 3, x = [0, 0, 0, 0]
At k = 1, j = 4, x = [0, 0, 0, 0]
At k = 1, j = 5, x = [0, 0, 0, 0]
At k = 1, j = 6, x = [0, 0, 0, 0]
At k = 1, j = 7, x = [0, 0, 0, 0]
# get stuck at here 

```

Can you find where the call `test()` get stuck at?

---

<div class="post-metadata">

### Author: ![WalterMadelim](https://avatars.discourse-cdn.com/v4/letter/w/3e96dc/32.png) [@WalterMadelim](https://discourse.julialang.org/u/WalterMadelim)
#### Post date: [August 28, 2025, 7:50am UTC](https://discourse.julialang.org/t/can-you-find-where-this-asynchronous-program-get-stuck-at/131907/2 "2025-08-28T07:50:47Z")

</div>

Oh, I see, it won’t error if you don’t wait for the task.

```julia
julia> t = @task error("miswrote");

julia> schedule(t);

julia> 

```

---

<div class="post-metadata">

### Author: ![sgaure](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sgaure/32/14779_2.png) [@sgaure](https://discourse.julialang.org/u/sgaure)
#### Post date: [August 28, 2025, 9:45am UTC](https://discourse.julialang.org/t/can-you-find-where-this-asynchronous-program-get-stuck-at/131907/3 "2025-08-28T09:45:13Z")

</div>

It’s quite annoying that you won’t see the error until the task is `wait`ed for, but there’s nowhere to throw the error.

There’s a similar problem with `Channel`s, which seems to be fixed in 1.12.

> [@Exception Control in Iteration over Channels, Latest Version](https://discourse.julialang.org/t/exception-control-in-iteration-over-channels-latest-version/131651):
>
> Dear all, I got intrigued when I realized that exceptions can be silenced when iterating over a channel. However, the source code on GitHub indicates that exceptions should actually be rethrown. Then I realized that the file Channels.jl on GitHub is actually different from its counterpart on my computer, though I should have the latest version of Julia. Now to details. Here is my example along with version details: \_ \_ \_ \_(\_)\_ | Documentation: https://docs.juliala…
