Skip to content
Myr

ignore: fix deadlock when visitor panics

Before adoption: this commit came in with the history the project adopted, so no landing records it.

When a visitor panics and the workers shutdown, we indicate as such and quit everything. This effectively acts as a sort of poison that will cause all other workers to stop even when in their idle loop. Fixes #3009

authored by Andrew Gallant · committed by Andrew Gallant · parent 5e16a5c · Browse files · 1 file+66 added−13 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+66 added−13 removed

  • other:+66 added−13 removed

1 file+66 added−13 removed

modified:crates/ignore/src/walk.rs+66 added-13 removed
1443 unmodified lines
1444 // Create the workers and then wait for them to finish.1445 let quit_now = Arc::new(AtomicBool::new(false));1446 let active_workers = Arc::new(AtomicUsize::new(threads));1447 let stacks = Stack::new_for_each_thread(threads, stack);1448 // Collect all of the workers first. In the case that1449 // `builder.build()` panics, we want that to happen and1450 // propagate before we actually start to run any of the1451 // workers.1452 let workers: Vec<_> = stacks1453 .into_iter()1454 .map(|stack| Worker {1455 visitor: builder.build(),1456 stack,1457 quit_now: quit_now.clone(),1458 active_workers: active_workers.clone(),1459 max_depth: self.max_depth,1460 min_depth: self.min_depth,1461 max_filesize: self.max_filesize,1462 follow_links: self.follow_links,1463 skip: self.skip.clone(),1464 filter: self.filter.clone(),1465 })1466 .collect();1467 std::thread::scope(|s| {1449 let handles: Vec<_> = stacks1468 let handles: Vec<_> = workers1469 .into_iter()1451 .map(|stack| Worker {1452 visitor: builder.build(),1453 stack,1454 quit_now: quit_now.clone(),1455 active_workers: active_workers.clone(),1456 max_depth: self.max_depth,1457 min_depth: self.min_depth,1458 max_filesize: self.max_filesize,1459 follow_links: self.follow_links,1460 skip: self.skip.clone(),1461 filter: self.filter.clone(),1462 })1470 .map(|worker| s.spawn(|| worker.run()))1471 .collect();1472 for handle in handles {1473 handle.join().unwrap();
437 unmodified lines
1911 return None;1912 }1913 // Wait for next `Work` or `Quit` message.1914 loop {1915 if self.is_quit_now() {1916 return None;1917 }1918 if let Some(v) = self.recv() {1919 self.activate_worker();1920 value = Some(v);1921 break;
45 unmodified lines
1967 self.active_workers.fetch_add(1, AtomicOrdering::Release);1968 }1969}19701971impl<'s> Drop for Worker<'s> {1972 fn drop(&mut self) {1973 if std::thread::panicking() {1974 self.quit_now();1975 }1976 }1977}19781979fn check_symlink_loop(1980 ig_parent: &Ignore,1981 child_path: &Path,1982 child_depth: usize,
638 unmodified lines
2621 "a/b/c",2622 ],2623 );2624 }26252626 // This should always panic and never hang.2627 //2628 // Ref: https://github.com/BurntSushi/ripgrep/issues/30092629 #[test]2630 #[should_panic]2631 fn panic_in_parallel() {2632 let td = tmpdir();2633 wfile(td.path().join("foo.txt"), "");26342635 WalkBuilder::new(td.path())2636 .threads(40)2637 .build_parallel()2638 .run(|| Box::new(|_| panic!("oops!")));2639 }26402641 // This should always panic and never hang. The first call to the visitor2642 // builder is used while processing the root paths. Previously, a panic on2643 // the third call occurred after the first worker had already been spawned,2644 // leaving it waiting indefinitely for workers that were never created.2645 #[test]2646 #[should_panic(expected = "builder panic")]2647 fn panic_in_parallel_builder() {2648 let td = tmpdir();2649 wfile(td.path().join("foo.txt"), "");26502651 let mut builds = 0;2652 WalkBuilder::new(td.path()).threads(2).build_parallel().run(|| {2653 builds += 1;2654 if builds == 3 {2655 panic!("builder panic");2656 }2657 Box::new(|_| WalkState::Continue)2658 });2659 }2660}