Arc Mutex locked in hotwatch handler

Here is my code snippet:

use std::{path::Path, thread, time::Duration, sync::{Arc, Mutex},};

use hotwatch::{notify::event::ModifyKind,Hotwatch, EventKind, notify::Event};

fn main() {
    let mut hotwatch1:Arc<Mutex<Hotwatch>> =Arc::new(Mutex::new( Hotwatch::new().expect("msg")));
    let hotwatch1_arc=Arc::clone(&hotwatch1);
   let result= hotwatch1.lock().unwrap().watch(Path::new("./cargo.toml"), move|event: Event| {
        if let EventKind::Modify(ModifyKind::Data(_)) = event.kind {
            println!("Cargo has changed.");
            hotwatch1_arc.lock().unwrap().watch(Path::new("./cargo.lock"), move|event: Event| {
                if let EventKind::Modify(ModifyKind::Data(_)) = event.kind {
                    println!("Cargo lock has changed.");
    if result.is_err(){


I need to add some files to watch or unwatch depending on watched file content.
The problem is that Arc Mutex is locking the handler, so the new watch is never added to the handlers Hashmap and the program is blocked...
Is there any workaround ? (ie without message passing if possible)

It doesn't seem like the program should deadlock, because the lock is only taken in a temporary (the guard is not assigned to any variable) in order to call watch(), which should return immediately.

Something non-obvious is going on. How about attaching a debugger or profiler to view the program's call stacks and confirm exactly where it is deadlocking?

Maybe it's not your lock at all — maybe Hotwatch doesn't like being modified from its own notification thread?

Yep, that's it. Look at the source code and you can see that watch() and run() take the same lock on the list of handlers. (They should document this deadlock hazard.)

You will have to, as you say, use message passing, or something, so that you don't try to modify the handlers while inside a handler.

Yes, that's the heart of the problem.
I was thinking of a silly idea which would be to use 2 hotwatchs...
But I will probably try to dig into hot watch code.
Thank you anyway for your precious time.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.