# How to stop a file watching task in Julia 1.8? Possible regression?

**URL:** https://discourse.julialang.org/t/how-to-stop-a-file-watching-task-in-julia-1-8-possible-regression/86448
**Category:** New to Julia
**Created:** [August 27, 2022, 11:05pm UTC](https://discourse.julialang.org/t/how-to-stop-a-file-watching-task-in-julia-1-8-possible-regression/86448 "2022-08-27T23:05:14Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)
#### Post date: [August 27, 2022, 11:05pm UTC](https://discourse.julialang.org/t/how-to-stop-a-file-watching-task-in-julia-1-8-possible-regression/86448/1 "2022-08-27T23:05:14Z")

</div>

Hello!

I am using BetterFileWatching.jl and the following code snippet:

```julia
watch_task = @async watch_folder(".") do event
    @info "Something changed!" event
end

sleep(5)

# stop watching the folder
schedule(watch_task, InterruptException(); error=true)

```

The task runs fine, but the schedule to stop it never works. I assume the authors have had success with this expression in past julia versions (since they wrote it works), but for 1.8 this does not work anymore in my case. Is this a regression or is there another way to stop the task?

Kind regards

---

<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: [August 28, 2022, 2:07am UTC](https://discourse.julialang.org/t/how-to-stop-a-file-watching-task-in-julia-1-8-possible-regression/86448/2 "2022-08-28T02:07:05Z")

</div>

> [@Ahmed\_Salih](#):
>
> but for 1.8 this does not work anymore in my case.

Hi!

Can you specify what you see? In my case, with both Julia 1.7 and 1.8, running the `schedule` line produces `Task (done) @` followed by the hexadecimal task id.

You could also try running `!istaskdone(watch_task) && yield(watch_task)` after the `schedule` to explicitly yield to the `watch_task` in case it’s not picked up by the scheduler for some reason.

---

<div class="post-metadata">

### Author: ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)
#### Post date: [August 28, 2022, 7:52am UTC](https://discourse.julialang.org/t/how-to-stop-a-file-watching-task-in-julia-1-8-possible-regression/86448/3 "2022-08-28T07:52:03Z")

</div>

Hi!

Yes it indeed produces “Task done + hex id” here as well, but it keeps outputting changes in the directory after the task supposedly has been stopped. This is of course incorrect behaviour. Running your snippet piece by piece:

```julia
!istaskdone(watch_task)
false

```

Which indicates that the scheduler is not picking it up. The yield command produces:

```julia
yield(watch_task)
ERROR: yield: Task not runnable
Stacktrace:
 [1] error(s::String)
   @ Base .\error.jl:35
 [2] yield (repeats 2 times)
   @ .\task.jl:831 [inlined]
 [3] top-level scope
   @ REPL[3]:1

```

Running the snippet in one go simply outputs “false”.

I am a bit perplexed about the difficulty of stopping a simple task in Julia right now.

Kind regards

---

<div class="post-metadata">

### Author: ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)
#### Post date: [August 28, 2022, 8:57am UTC](https://discourse.julialang.org/t/how-to-stop-a-file-watching-task-in-julia-1-8-possible-regression/86448/4 "2022-08-28T08:57:30Z")

</div>

See yellow:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/4/0/4005b85bf61343ed3627848b7ef1c4b9908cab78.png)

[https://docs.julialang.org/en/v1/base/parallel/](https://docs.julialang.org/en/v1/base/parallel/)

---

<div class="post-metadata">

### Author: ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)
#### Post date: [August 28, 2022, 9:38am UTC](https://discourse.julialang.org/t/how-to-stop-a-file-watching-task-in-julia-1-8-possible-regression/86448/5 "2022-08-28T09:38:15Z")

</div>

The error seems to happen in the “do” statement. The task will be “done” since it runs through the code, but the “do” statement, never experiences “closure”.

---

<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: [August 28, 2022, 1:08pm UTC](https://discourse.julialang.org/t/how-to-stop-a-file-watching-task-in-julia-1-8-possible-regression/86448/6 "2022-08-28T13:08:42Z")

</div>

Honestly this seems like it might be a library problem i.e. a bug as you’ve mentioned in your [issue](https://github.com/JuliaPluto/BetterFileWatching.jl/issues/3).

The [`watch_folder` code creates](https://github.com/JuliaPluto/BetterFileWatching.jl/blob/main/src/BetterFileWatching.jl#L109) its own inner `Task` with `@async`, and any exception on that is handled by passing an `InterruptException` to the Deno task which should end the file watching. However, the `InterruptException` we pass in the code from the README is never passed on to this inner task, so this never happens. (Basically, there’s three layers of `Task`s, and we’re able to pass an `Interrupt` only to the outer layer, which doesn’t get propagated to the inner two.) This is all based on my so-so understanding of `Task`s though, so I’ve subscribed to the Issue on Github to see what the update is going to be.

For now, it seems like the library is pretty young and not highly used (only 3 stars on the project), so you’re probably better off using the `FileWatching` standard library.

---

<div class="post-metadata">

### Author: ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)
#### Post date: [August 28, 2022, 1:40pm UTC](https://discourse.julialang.org/t/how-to-stop-a-file-watching-task-in-julia-1-8-possible-regression/86448/7 "2022-08-28T13:40:03Z")

</div>

Thanks for the look at it!

I tried using the standard library file watching, but I simply couldn’t get it to work asynchrounosly. I would risk that if for example 4 files are created at once, then only one of them is registered - this is suboptimal.

Do you happen to know how one could write an async version doing the same thing, but without this bug?

I tried to do the straight forward, swapping “watch\_folder” function to the standard library, but it simply would not accept the do syntax, which seems necessary for this async behaviour.

Kind regards

---

<div class="post-metadata">

### Author: ![gustafsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustafsson/32/3761_2.png) [@gustafsson](https://discourse.julialang.org/u/gustafsson)
#### Post date: [August 29, 2022, 10:19am UTC](https://discourse.julialang.org/t/how-to-stop-a-file-watching-task-in-julia-1-8-possible-regression/86448/8 "2022-08-29T10:19:02Z")

</div>

> [@Ahmed\_Salih](#):
>
> Do you happen to know how one could write an async version doing the same thing, but without this bug?

How about?

```julia
import FileWatching: watch_folder
function watch_folder(f, path::AbstractString)
     while true
         f(watch_folder(path))
     end
end

```

I was able to run your original example with that function instead.

```julia
watch_task = @async watch_folder(".") ...

```

---

<div class="post-metadata">

### Author: ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)
#### Post date: [August 29, 2022, 9:02pm UTC](https://discourse.julialang.org/t/how-to-stop-a-file-watching-task-in-julia-1-8-possible-regression/86448/9 "2022-08-29T21:02:03Z")

</div>

Thanks this helps a lot!

The problem is now that I don’t understand what is “changed”, “renamed” and “timeout” actually means in the “FileEvent” result.

Renamed and timeout should just be change of name and “fail if taken too long”, but I cannot figure out how to split “changed” into “created” and “modified” which BetterFileWatching.jl does really smartly and intuitively. I am almost there, so I hope someone perhaps understands this part better.

Still surprised that something I thought would be so easy to do is actually the most difficult part to code up…

Kind regards

---

<div class="post-metadata">

### Author: ![gustafsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustafsson/32/3761_2.png) [@gustafsson](https://discourse.julialang.org/u/gustafsson)
#### Post date: [August 29, 2022, 10:58pm UTC](https://discourse.julialang.org/t/how-to-stop-a-file-watching-task-in-julia-1-8-possible-regression/86448/10 "2022-08-29T22:58:21Z")

</div>

I’m glad you found it helpful.

> [@Ahmed\_Salih](#):
>
> The problem is now that I don’t understand what is “changed”, “renamed” and “timeout” actually means in the “FileEvent” result.

For me `renamed` was set both for renamed files as well as newly created files. `timeout` refers to whether an optional argument to `watch_folder` was exceeded. You don’t need the construct `sleep...schedule(..;error)`.

Here is the documentation for the FileWatching module:  
[https://docs.julialang.org/en/v1/stdlib/FileWatching/#FileWatching.watch\_folder](https://docs.julialang.org/en/v1/stdlib/FileWatching/#FileWatching.watch_folder)

---

<div class="post-metadata">

### Author: ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)
#### Post date: [August 30, 2022, 8:28pm UTC](https://discourse.julialang.org/t/how-to-stop-a-file-watching-task-in-julia-1-8-possible-regression/86448/11 "2022-08-30T20:28:44Z")

</div>

Thank you very much!

You are indeed right that renaming = new file, even though it annoys me not being able to discern between modified name and actual new file. Still for now it is workable for me.

Thank you for your help and time - it lets me progress my general code now.

I do hope though that this becomes much better, because I find it so unbelievable that a language as Julia, which is good at so many things, actually struggles to properly close down subtasks and perform IO operations - thankfully you gave me the quick workaround, which is why I marked your answer as solution.

Kind regards

---

<div class="post-metadata">

### Author: ![cshen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cshen/32/217287_2.png) [@cshen](https://discourse.julialang.org/u/cshen)
#### Post date: [September 25, 2022, 1:41pm UTC](https://discourse.julialang.org/t/how-to-stop-a-file-watching-task-in-julia-1-8-possible-regression/86448/12 "2022-09-25T13:41:27Z")

</div>

I’ve stumbled across a similar problem but i found that the following code works:

```julia
function keep_watching_folder(f, path)
    while true
        try
            f(watch_folder(path))
        catch
            break
        end
    end
end

```

by just calling `unwatch_folder(path)` somewhere.  
Here’s an example:

```julia
julia> t = @task keep_watching_folder(println, ".")                                                                                                                                                                                                                                                                        
Task (runnable) @0x00000002995a59f0

julia> schedule(t)                                                                                                                                                                                                                                                                                                         
Task (runnable) @0x00000002995a59f0

julia> istaskstarted(t)                                                                                                                                                                                                                                                                                                    
true

julia> istaskdone(t)                                                                                                                                                                                                                                                                                                       
false

julia> unwatch_folder(".")                                                                                                                                                                                                                                                                                                 

julia> istaskdone(t)                                                                                                                                                                                                                                                                                                       
true

julia> istaskfailed(t)                                                                                                                                                                                                                                                                                                     
false

```

---

<div class="post-metadata">

### Author: ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)
#### Post date: [October 1, 2022, 1:50pm UTC](https://discourse.julialang.org/t/how-to-stop-a-file-watching-task-in-julia-1-8-possible-regression/86448/13 "2022-10-01T13:50:47Z")

</div>

Uh, I will give that a try and get back to you!

Kind regards
