Skip to content

Commit

Permalink
Backport #302 to v4
Browse files Browse the repository at this point in the history
(based on commit 86421e5)
Signed-off-by: Aron Heinecke <[email protected]>
  • Loading branch information
0xpr03 committed May 8, 2021
1 parent 81b2ab4 commit 3c8d159
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## 4.0.17 (future)

- FIX: Don't crash on macos when creating & deleting folders in rapid succession [#303]

[#303]: https://github.com/notify-rs/notify/pull/303

## 4.0.16 (2021-04-14)

Expand Down
10 changes: 10 additions & 0 deletions src/fsevent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ impl FsEventWatcher {
unsafe {
let mut err: cf::CFErrorRef = ptr::null_mut();
let cf_path = cf::str_path_to_cfstring_ref(str_path, &mut err);
if cf_path.is_null() {
cf::CFRelease(err as cf::CFRef);
return Err(Error::WatchNotFound);
}

let mut to_remove = Vec::new();
for idx in 0..cf::CFArrayGetCount(self.paths) {
Expand Down Expand Up @@ -173,6 +177,12 @@ impl FsEventWatcher {
unsafe {
let mut err: cf::CFErrorRef = ptr::null_mut();
let cf_path = cf::str_path_to_cfstring_ref(str_path, &mut err);
if cf_path.is_null() {
// Most likely the directory was deleted, or permissions changed,
// while the above code was running.
cf::CFRelease(err as cf::CFRef);
return Err(Error::PathNotFound);
}
cf::CFArrayAppendValue(self.paths, cf_path);
cf::CFRelease(cf_path);
}
Expand Down
34 changes: 34 additions & 0 deletions tests/race-with-remove-dir.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use std::{env, fs, thread, time::Duration};

extern crate notify;
use notify::{RecommendedWatcher, RecursiveMode, Watcher};
use std::sync::mpsc;

/// Test for <https://github.com/notify-rs/notify/issues/301>.
/// Note: This test will fail if your temp directory is not writable.
#[test]
fn test_race_with_remove_dir() {
let tmpdir = env::temp_dir().join(".tmprPcUcB");
fs::create_dir_all(&tmpdir).unwrap();

{
let tmpdir = tmpdir.clone();
let (tx, rx) = mpsc::channel();
thread::spawn(move || {
let mut watcher = RecommendedWatcher::new_raw(tx)
.unwrap();

watcher.watch(tmpdir, RecursiveMode::NonRecursive).unwrap();
});
thread::spawn(move || {
for msg in rx {
eprintln!("received event: {:?}", msg);
}
});
}

let subdir = tmpdir.join("146d921d.tmp");
fs::create_dir_all(&subdir).unwrap();
fs::remove_dir_all(&tmpdir).unwrap();
thread::sleep(Duration::from_secs(1));
}

0 comments on commit 3c8d159

Please sign in to comment.