Skip to content
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

ignore: gracefully quit worker threads upon panic in ParallelVisitor #3010

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

cosmicexplorer
Copy link

Fixes #3009.

Problem

WalkParallel::visit() will nondeterministically hang if the ParallelVisitor::visit() implementation panics. This also occurs when providing a closure to WalkParallel::run(). Minimal repro is provided in #3009:

        WalkBuilder::new(path)
            .build_parallel()
            .run(|| Box::new(|_| panic!("oops!")));

The above code will nondeterministically hang, because of an infinite loop when no new work is available:

loop {
if let Some(v) = self.recv() {
self.activate_worker();
value = Some(v);
break;
}
// Our stack isn't blocking. Instead of burning the
// CPU waiting, we let the thread sleep for a bit. In
// general, this tends to only occur once the search is
// approaching termination.
let dur = std::time::Duration::from_millis(1);
std::thread::sleep(dur);
}

Solution

  1. Check the quit_now flag in our wait loop.
  2. Catch any panic in the run() method and set quit_now before propagating the panic.

Breaking change: In order to ensure soundness, we also enforce that the filter method provided to WalkBuilder#filter_entry() is UnwindSafe. Users can circumvent this by wrapping in AssertUnwindSafe as needed.

Result

The added test panic_in_parallel() always succeeds instead of hanging.

@cosmicexplorer cosmicexplorer changed the title correctly quit out of busy loops if a ParallelVisitor panics to avoid a hang ignore: correctly quit out of busy loops if a ParallelVisitor panics to avoid a hang Mar 6, 2025
@cosmicexplorer cosmicexplorer changed the title ignore: correctly quit out of busy loops if a ParallelVisitor panics to avoid a hang ignore: gracefully quit worker threads upon panic in ParallelVisitor Mar 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ignore: parallel walker occasionally hangs if visitor panics
1 participant