Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PollWatcher panics if a watched file is removed and recreated. #359

Closed
jhscheer opened this issue Sep 18, 2021 · 2 comments · Fixed by #406
Closed

PollWatcher panics if a watched file is removed and recreated. #359

jhscheer opened this issue Sep 18, 2021 · 2 comments · Fixed by #406
Labels
A-bug Z-needs info Needs more information

Comments

@jhscheer
Copy link
Contributor

jhscheer commented Sep 18, 2021

System details

  • OS/Platform name and version: Linux (5.13.15-200.fc34.x86_64)
  • Rust version (if building from source): rustc --version: 1.54.0
  • Notify version (or commit hash if building from git): 4.0.17 / 5.0.0-pre.13

What you did (as detailed as you can)

fn watch<P: AsRef<Path>>(path: P) -> notify::Result<()> {                                                                                          
    let (tx, rx) = channel();                                                                                                                      
                                                                                                                                                                                                                              
    // let mut watcher = PollWatcher::new(tx, Duration::from_secs(1)).unwrap();                                                                    
    let mut watcher = PollWatcher::with_delay_ms(tx, 1000).unwrap();                                                                               
                                                                                                                                                                                                                                                          
    watcher.watch(path.as_ref(), RecursiveMode::NonRecursive)?;                                                                                    
                                                                                                                                                   
    loop {                                                                                                                                         
        match rx.recv() {                                                                                                                          
            Ok(event) => println!("{:?}\n", event),                                                                                                                                                                                                  
            Err(e) => println!("watch error: {:?}", e),                                                                                            
        }                                                                                                                                          
    }                                                                                                                                              
} 

Run above code to monitor testfile, then run:

rm testfile && sleep 1 && touch testfile

Both the raw PollWatcher and the debounced PollWatcher panic if a watched file is remove and recreated.

Edit:
Same issue exists on 5.0.0-pre.13:

let event_handler = Arc::new(Mutex::new(tx));
let mut watcher = PollWatcher::with_delay(event_handler, Duration::from_secs(1))?;
watch error: Error { kind: Io(Os { code: 2, kind: NotFound, message: "No such file or directory" }), paths: ["testfile"] }
Event { kind: Remove(Any), paths: ["testfile"], attr:tracker: None, attr:flag: None, attr:info: None, attr:source: None }

watch error: Error { kind: Io(Os { code: 2, kind: NotFound, message: "No such file or directory" }), paths: ["testfile"] }
thread '<unnamed>' panicked at 'internal error: entered unreachable code', ~/.cargo/registry/src/github.com-1ecc6299db9ec823/notify-5.0.0-pre.13/src/poll.rs:108:45

What you expected

NoticeRemove("testfile")
Remove("testfile")
RawEvent { path: Some("testfile"), op: Ok(REMOVE), cookie: None }

What happened

Error(Io(Os { code: 2, kind: NotFound, message: "No such file or directory" }), Some("testfile"))

NoticeRemove("testfile")

Remove("testfile")

Error(Io(Os { code: 2, kind: NotFound, message: "No such file or directory" }), Some("testfile"))

Error(Io(Os { code: 2, kind: NotFound, message: "No such file or directory" }), Some("testfile"))

thread '<unnamed>' panicked at 'internal error: entered unreachable code', ~/.cargo/registry/src/github.com-1ecc6299db9ec823/notify-4.0.17/src/poll.rs:97:45
RawEvent { path: Some("testfile"), op: Err(Io(Os { code: 2, kind: NotFound, message: "No such file or directory" })), cookie: None }

RawEvent { path: Some("testfile"), op: Ok(REMOVE), cookie: None }

RawEvent { path: Some("testfile"), op: Err(Io(Os { code: 2, kind: NotFound, message: "No such file or directory" })), cookie: None }

RawEvent { path: Some("testfile"), op: Err(Io(Os { code: 2, kind: NotFound, message: "No such file or directory" })), cookie: None }

thread '<unnamed>' panicked at 'internal error: entered unreachable code', ~/.cargo/registry/src/github.com-1ecc6299db9ec823/notify-4.0.17/src/poll.rs:97:45
@jhscheer jhscheer changed the title Both the raw PollWatcher and the debounced PollWatcher panic if a watched file is removed and recreated. (4.0.1.7) PollWatcher panics if a watched file is removed and recreated. Sep 18, 2021
@0xpr03 0xpr03 added A-bug Z-needs info Needs more information labels Sep 29, 2021
@visig9
Copy link
Contributor

visig9 commented May 18, 2022

This code can reproduce bug.

//! # Dependencies
//!
//! ```toml
//! [dependencies]
//! notify = "5.0.0-pre.15"
//! ```
//!
//! # Test
//!
//! Panic with following shell command.
//!
//! ```sh
//! # in first terminal
//! touch mew.txt
//! cargo run
//!
//! # in second terminal
//! rm mew.txt && sleep 1 && touch mew.txt
//! ```

use notify::Watcher;
use std::{thread, time::Duration};

fn main() {
    let mut watcher = notify::PollWatcher::with_config(
        |event| println!("{event:?}"),
        notify::poll::PollWatcherConfig {
            poll_interval: Duration::from_secs(1),
            ..Default::default()
        },
    )
    .unwrap();

    watcher
        .watch("mew.txt".as_ref(), notify::RecursiveMode::Recursive)
        .unwrap();

    thread::park();
}

Output:

Err(Error { kind: Io(Os { code: 2, kind: NotFound, message: "No such file or directory" }), paths: ["mew.txt"] })
Ok(Event { kind: Remove(Any), paths: ["mew.txt"], attr:tracker: None, attr:flag: None, attr:info: None, attr:source: None })
thread 'notify-rs poll loop' panicked at 'internal error: entered unreachable code', /home/visig/.cargo/registry/src/github.com-1ecc6299db9ec823/notify-5.0.0-pre.15/src/poll.rs:206:49
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

visig9 added a commit to visig9/notify that referenced this issue May 18, 2022
@visig9
Copy link
Contributor

visig9 commented May 18, 2022

I create #406 to fix. After this PR, the previous test code should output:

Err(Error { kind: Io(Os { code: 2, kind: NotFound, message: "No such file or directory" }), paths: ["mew.txt"] })
Ok(Event { kind: Remove(Any), paths: ["mew.txt"], attr:tracker: None, attr:flag: None, attr:info: None, attr:source: None })
Ok(Event { kind: Create(Any), paths: ["mew.txt"], attr:tracker: None, attr:flag: None, attr:info: None, attr:source: None })

visig9 added a commit to visig9/notify that referenced this issue May 18, 2022
0xpr03 pushed a commit that referenced this issue May 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-bug Z-needs info Needs more information
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants