Stop/terminate a (sub)task started with @async

This is not a julia 1.8 problem but instead an issue with the package BetterFileWatching.jl.

The culprit is the line:

try wait(watch_task) catch; end

at BetterFileWatching.jl/BetterFileWatching.jl at main · JuliaPluto/BetterFileWatching.jl · GitHub

When you schedule an InterruptException() you silently interrupt the wait of the watch_task async task but not the watch_task task that outlives watch_folder.

This is a simplified version that shows the same issue:

function watch_task()
    while true
        @info "doing something ..."
        sleep(3)
    end
    
end

function watch_folder()
    t = @async watch_task()
    try wait(t) catch; end
end

task = @async watch_folder()

sleep(5)

schedule(task, InterruptException(), error=true)

Eventually consider to open a BetterFileWatchingIssue.jl github issue …

Greetings, Attilio

3 Likes