-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
fix(daemon): kill daemon when root is removed #5038
Merged
Merged
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
cbcd6b7
fix: make sure to take down the daemon when the root goes away
arlyon 0937efe
add log statement on repo root deletion
2fd5af3
no longer recursively watch repo root
803065b
fix windows repo root deletion detection
chris-olszewski 845cb64
surface possible config error in daemon code
632d927
add timeout to server daemon kill test
51b217a
Merge branch 'main' into fix/root-watch-daemon-kill
chris-olszewski File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,7 @@ use std::{ | |
use futures::{stream::iter, StreamExt}; | ||
use globwatch::{ConfigError, GlobWatcher, StopToken, WatchConfig, Watcher}; | ||
use itertools::Itertools; | ||
use notify::RecommendedWatcher; | ||
use notify::{EventKind, RecommendedWatcher}; | ||
use tokio::time::timeout; | ||
use tracing::{trace, warn}; | ||
use turbopath::AbsoluteSystemPathBuf; | ||
|
@@ -66,7 +66,7 @@ impl<T: Watcher> HashGlobWatcher<T> { | |
/// Watches a given path, using the flush_folder as temporary storage to | ||
/// make sure that file events are handled in the appropriate order. | ||
#[tracing::instrument(skip(self, token))] | ||
pub async fn watch(&self, token: StopToken) { | ||
pub async fn watch(&self, token: StopToken) -> Result<(), ConfigError> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do other callers need to handle this error as well? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch, the only caller happens in a macro that ended up suppressing the lint. |
||
let start_globs = { | ||
let lock = self.hash_globs.lock().expect("only fails if poisoned"); | ||
lock.iter() | ||
|
@@ -79,17 +79,25 @@ impl<T: Watcher> HashGlobWatcher<T> { | |
Some(watcher) => watcher.into_stream(token), | ||
None => { | ||
warn!("watcher already consumed"); | ||
return; | ||
return Err(ConfigError::WatchingAlready); | ||
} | ||
}; | ||
|
||
// watch the root of the repo to shut down if the folder is deleted | ||
self.config.include_path(&self.relative_to).await?; | ||
|
||
// watch all the globs currently in the map | ||
for glob in start_globs { | ||
self.config.include(&self.relative_to, &glob).await.ok(); | ||
} | ||
|
||
while let Some(Ok(event)) = stream.next().await { | ||
trace!("processing event: {:?}", event); | ||
if event.paths.contains(&self.relative_to) && matches!(event.kind, EventKind::Remove(_)) | ||
{ | ||
// if the root of the repo is deleted, we shut down | ||
trace!("repo root was removed, shutting down"); | ||
break; | ||
} | ||
|
||
let repo_relative_paths = event | ||
.paths | ||
|
@@ -115,6 +123,8 @@ impl<T: Watcher> HashGlobWatcher<T> { | |
self.config.exclude(&self.relative_to, &glob).await; | ||
} | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
/// registers a hash with a set of globs to watch for changes | ||
|
@@ -657,4 +667,36 @@ mod test { | |
watcher.glob_statuses.lock().unwrap() | ||
); | ||
} | ||
|
||
#[tokio::test] | ||
#[tracing_test::traced_test] | ||
async fn delete_root_kill_daemon() { | ||
let dir = setup(); | ||
let flush = tempdir::TempDir::new("globwatch-flush").unwrap(); | ||
let watcher = Arc::new( | ||
super::HashGlobWatcher::new( | ||
AbsoluteSystemPathBuf::new(dir.path()).unwrap(), | ||
flush.path().to_path_buf(), | ||
) | ||
.unwrap(), | ||
); | ||
|
||
let stop = StopSource::new(); | ||
|
||
let task_watcher = watcher.clone(); | ||
let token = stop.token(); | ||
|
||
// dropped when the test ends | ||
let task = tokio::task::spawn(async move { task_watcher.watch(token).await }); | ||
|
||
watcher.config.flush().await.unwrap(); | ||
std::fs::remove_dir_all(dir.path()).unwrap(); | ||
|
||
// it should shut down | ||
let finish = task.await; | ||
assert!( | ||
finish.is_ok(), | ||
"expected task to finish when root is deleted" | ||
); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I changed this to be
NonRecursive
as I don't think we want to recursively watch the repo root: @arlyon