From c9e8f3e48004256f57565e71bbbae9480a0a4c71 Mon Sep 17 00:00:00 2001 From: Federico Terzi Date: Wed, 1 Sep 2021 22:43:25 +0200 Subject: [PATCH] fix(core): fix race condition when multiple files are changed --- espanso/src/cli/daemon/watcher.rs | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/espanso/src/cli/daemon/watcher.rs b/espanso/src/cli/daemon/watcher.rs index e868d33..c6181fa 100644 --- a/espanso/src/cli/daemon/watcher.rs +++ b/espanso/src/cli/daemon/watcher.rs @@ -17,7 +17,10 @@ * along with espanso. If not, see . */ -use std::{path::Path, time::Duration}; +use std::{ + path::Path, + time::{Duration, Instant}, +}; use notify::{DebouncedEvent, RecommendedWatcher, RecursiveMode, Watcher}; @@ -52,6 +55,8 @@ fn watcher_main(config_dir: &Path, watcher_notify: &Sender<()>) { info!("watching for changes in path: {:?}", config_dir); + let mut last_event_arrival = Instant::now(); + loop { let should_reload = match rx.recv() { Ok(event) => { @@ -69,10 +74,13 @@ fn watcher_main(config_dir: &Path, watcher_notify: &Sender<()>) { .unwrap_or_default() .to_string_lossy() .to_ascii_lowercase(); - + if ["yml", "yaml"].iter().any(|ext| ext == &extension) { // Only load non-hidden yml files !is_file_hidden(&path) + } else if extension == "" { + // No extension, probably a folder + true } else { false } @@ -86,11 +94,16 @@ fn watcher_main(config_dir: &Path, watcher_notify: &Sender<()>) { } }; - if should_reload { + // Send only one event, otherwise we could run the risk of useless reloads or even race conditions. + if should_reload + && last_event_arrival.elapsed() > std::time::Duration::from_secs(WATCHER_DEBOUNCE_DURATION) + { if let Err(error) = watcher_notify.send(()) { error!("unable to send watcher file changed event: {}", error); } } + + last_event_arrival = Instant::now(); } }