Порівняти коміти

...
Автор SHA1 Повідомлення Дата
l0ng-ai 1438876254 fix(vfs): don't cache ignore rules read before an invalidation
GitignoreStack::matcher_for read a directory's .gitignore outside the cache
lock and stored the result afterwards. When the watcher thread was matching a
new file while a command rewrote .gitignore and invalidated the cache, the
rules it had read before the rewrite landed in the cache after the
invalidation, and the next listing missed the new entry. Each invalidation now
bumps a generation counter under the cache lock, and a lookup stores its
matcher only when no invalidation happened since it started reading. This
made rpc::serves_projects_files_documents_search_and_runs fail intermittently
on Windows, where watcher events arrive later and file reads are slower.
2026-11-30 11:47:49 +08:00
+56 -6
Переглянути файл
@@ -8,6 +8,7 @@ use parking_lot::{Mutex, RwLock};
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::{Duration, Instant, SystemTime};
/// Exclude globs from settings (`files.exclude`), e.g. `**/node_modules`.
@@ -97,6 +98,11 @@ pub(crate) struct GitignoreStack {
/// `.git/info/exclude` and the global excludes file, rooted at the repo root.
repo_level: RwLock<Vec<Gitignore>>,
cache: Mutex<HashMap<PathBuf, Option<Arc<Gitignore>>>>,
/// Bumped (under the `cache` lock) by every invalidation. A lookup that read the ignore
/// files before an invalidation doesn't store what it read: the watcher thread may be
/// matching a new file while a command rewrites `.gitignore` and invalidates, and its
/// older rules must not land in the cache after that.
generation: AtomicU64,
nested: Mutex<NestedRepos>,
}
@@ -127,7 +133,7 @@ impl GitignoreStack {
pub fn new(near: &Path) -> Self {
let repo_root = find_repo_root(near);
let repo_level = RwLock::new(Self::load_repo_level(repo_root.as_deref()));
Self { repo_root, repo_level, cache: Mutex::default(), nested: Mutex::default() }
Self { repo_root, repo_level, cache: Mutex::default(), generation: AtomicU64::new(0), nested: Mutex::default() }
}
/// [`new`](Self::new) for listing a directory over and over (expanding a tree, a slow share):
@@ -163,6 +169,7 @@ impl GitignoreStack {
repo_root: entry.repo_root.clone(),
repo_level: RwLock::new(entry.rules.clone()),
cache: Mutex::default(),
generation: AtomicU64::new(0),
nested: Mutex::default(),
}
}
@@ -217,6 +224,7 @@ impl GitignoreStack {
}
}
let mut cache = self.cache.lock();
self.generation.fetch_add(1, Ordering::Relaxed);
// Lookups always populate every ancestor, so no key below `dir` exists without `dir`.
if cache.contains_key(dir) {
cache.retain(|k, _| !k.starts_with(dir));
@@ -232,7 +240,9 @@ impl GitignoreStack {
for stack in self.nested.lock().by_root.values() {
stack.invalidate(dir);
}
self.cache.lock().remove(dir);
let mut cache = self.cache.lock();
self.generation.fetch_add(1, Ordering::Relaxed);
cache.remove(dir);
}
/// Returns the shared `GitignoreStack` of the nested repository enclosing `dir`, if any.
@@ -273,6 +283,7 @@ impl GitignoreStack {
repo_root: Some(root),
repo_level,
cache: Mutex::default(),
generation: AtomicU64::new(0),
nested: Mutex::default(),
})
})
@@ -281,9 +292,13 @@ impl GitignoreStack {
}
fn matcher_for(&self, dir: &Path) -> Option<Arc<Gitignore>> {
if let Some(cached) = self.cache.lock().get(dir) {
return cached.clone();
}
let generation = {
let cache = self.cache.lock();
if let Some(cached) = cache.get(dir) {
return cached.clone();
}
self.generation.load(Ordering::Relaxed)
};
let mut builder = GitignoreBuilder::new(dir);
let mut any = false;
for name in [".gitignore", ".ignore"] {
@@ -307,7 +322,10 @@ impl GitignoreStack {
} else {
None
};
self.cache.lock().insert(dir.to_path_buf(), matcher.clone());
let mut cache = self.cache.lock();
if self.generation.load(Ordering::Relaxed) == generation {
cache.insert(dir.to_path_buf(), matcher.clone());
}
matcher
}
@@ -476,6 +494,38 @@ mod tests {
assert!(Arc::ptr_eq(&nested, stack.nested.lock().by_root.get(&repo).unwrap()));
}
#[test]
fn a_lookup_racing_an_invalidation_does_not_cache_the_older_rules() {
// The watcher thread matches new files while a command rewrites `.gitignore` and
// invalidates; whatever it read before the rewrite must not outlive the invalidation.
// A long file keeps the reader busy building its matcher, widening the window.
let tmp = tempfile::tempdir().unwrap();
let root = dunce::canonicalize(tmp.path()).unwrap();
std::fs::create_dir(root.join(".git")).unwrap();
let filler: String = (0..20_000).map(|n| format!("generated-{n}/\n")).collect();
let stack = Arc::new(GitignoreStack::new(&root));
for i in 0..20 {
let name = format!("f{i}.tmp-ignore");
std::fs::write(root.join(".gitignore"), &filler).unwrap();
stack.invalidate(&root);
let stop = Arc::new(std::sync::atomic::AtomicBool::new(false));
let reader = {
let (stack, stop, path) = (stack.clone(), stop.clone(), root.join(&name));
std::thread::spawn(move || {
while !stop.load(Ordering::Relaxed) {
stack.is_ignored(&path, false);
}
})
};
std::thread::sleep(Duration::from_millis(5));
std::fs::write(root.join(".gitignore"), format!("{filler}{name}\n")).unwrap();
stack.invalidate(&root);
stop.store(true, Ordering::Relaxed);
reader.join().unwrap();
assert!(stack.is_ignored(&root.join(&name), false), "round {i} kept the older rules");
}
}
#[test]
fn a_repository_created_inside_the_workspace_becomes_a_boundary() {
let tmp = tempfile::tempdir().unwrap();