walk: Split wants_to_quit into two separate flags
Before adoption: this commit came in with the history the project adopted, so no landing records it.
quit_flag is now used to quit the sender threads for any reason, either due to an interrupt or because the receiver is done. interrupt_flag is used specifically for ^C interrupts, and causes the receiver to stop between printing paths, to avoid unfinished escape sequences when colors are being used. Fixes #867.
authored by Tavian Barnes · committed by Tavian Barnes · parent 97a8825 · Browse files · 1 file·+31 added−16 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+31 added−16 removed
- other:+31 added−16 removed
Provenance
- Commit
4baefb2- Tree
aa74bf3- Author
Tavian Barnes <tavianator@tavianator.com>- Committer
Tavian Barnes <tavianator@tavianator.com>- Parent
97a8825
1 file changed+31 added−16 removed
modified:src/walk.rs+31 added-16 removed
132 unmodified lines133 }134 135 let parallel_walker = walker.threads(config.threads).build_parallel();136 137+ // Flag for cleanly shutting down the parallel walk137- let wants_to_quit = Arc::new(AtomicBool::new(false));138+ let quit_flag = Arc::new(AtomicBool::new(false));139+ // Flag specifically for quitting due to ^C140+ let interrupt_flag = Arc::new(AtomicBool::new(false));141+142 if config.ls_colors.is_some() && config.command.is_none() {139- let wq = Arc::clone(&wants_to_quit);143+ let quit_flag = Arc::clone(&quit_flag);144+ let interrupt_flag = Arc::clone(&interrupt_flag);145+146 ctrlc::set_handler(move || {147+ quit_flag.store(true, Ordering::Relaxed);148+141- if wq.fetch_or(true, Ordering::Relaxed) {149+ if interrupt_flag.fetch_or(true, Ordering::Relaxed) {150 // Ctrl-C has been pressed twice, exit NOW151 ExitCode::KilledBySigint.exit();152 }153 })154 .unwrap();155 }156 157 // Spawn the thread that receives all results through the channel.150- let receiver_thread = spawn_receiver(&config, &wants_to_quit, rx);158+ let receiver_thread = spawn_receiver(&config, &quit_flag, &interrupt_flag, rx);159 160 // Spawn the sender threads.153- spawn_senders(&config, &wants_to_quit, pattern, parallel_walker, tx);161+ spawn_senders(&config, &quit_flag, pattern, parallel_walker, tx);162 163 // Wait for the receiver thread to print out all results.164 let exit_code = receiver_thread.join().unwrap();165 158- if wants_to_quit.load(Ordering::Relaxed) {166+ if interrupt_flag.load(Ordering::Relaxed) {167 Ok(ExitCode::KilledBySigint)168 } else {169 Ok(exit_code)170 }2 unmodified lines173 /// Wrapper for the receiver thread's buffering behavior.174 struct ReceiverBuffer<W> {175 /// The configuration.176 config: Arc<Config>,177+ /// For shutting down the senders.178+ quit_flag: Arc<AtomicBool>,179 /// The ^C notifier.170- wants_to_quit: Arc<AtomicBool>,180+ interrupt_flag: Arc<AtomicBool>,181 /// Receiver for worker results.182 rx: Receiver<WorkerResult>,183 /// Standard output.184 stdout: W,10 unmodified lines195 impl<W: Write> ReceiverBuffer<W> {196 /// Create a new receiver buffer.197 fn new(198 config: Arc<Config>,189- wants_to_quit: Arc<AtomicBool>,199+ quit_flag: Arc<AtomicBool>,200+ interrupt_flag: Arc<AtomicBool>,201 rx: Receiver<WorkerResult>,202 stdout: W,203 ) -> Self {204 let max_buffer_time = config.max_buffer_time.unwrap_or(DEFAULT_MAX_BUFFER_TIME);205 let deadline = Instant::now() + max_buffer_time;206 207 Self {208 config,198- wants_to_quit,209+ quit_flag,210+ interrupt_flag,211 rx,212 stdout,213 mode: ReceiverMode::Buffering,214 deadline,5 unmodified lines220 /// Process results until finished.221 fn process(&mut self) -> ExitCode {222 loop {223 if let Err(ec) = self.poll() {224+ self.quit_flag.store(true, Ordering::Relaxed);225 return ec;226 }227 }228 }59 unmodified lines288 /// Output a path.289 fn print(&mut self, path: &Path) -> Result<(), ExitCode> {290 output::print_entry(&mut self.stdout, path, &self.config);291 279- if self.wants_to_quit.load(Ordering::Relaxed) {292+ if self.interrupt_flag.load(Ordering::Relaxed) {293 // Ignore any errors on flush, because we're about to exit anyway294 let _ = self.flush();295 return Err(ExitCode::KilledBySigint);296 }38 unmodified lines335 }336 337 fn spawn_receiver(338 config: &Arc<Config>,326- wants_to_quit: &Arc<AtomicBool>,339+ quit_flag: &Arc<AtomicBool>,340+ interrupt_flag: &Arc<AtomicBool>,341 rx: Receiver<WorkerResult>,342 ) -> thread::JoinHandle<ExitCode> {343 let config = Arc::clone(config);330- let wants_to_quit = Arc::clone(wants_to_quit);344+ let quit_flag = Arc::clone(quit_flag);345+ let interrupt_flag = Arc::clone(interrupt_flag);346 347 let show_filesystem_errors = config.show_filesystem_errors;348 let threads = config.threads;349 // This will be used to check if output should be buffered when only running a single thread47 unmodified lines397 let stdout = io::stdout();398 let stdout = stdout.lock();399 let stdout = io::BufWriter::new(stdout);400 386- let mut rxbuffer = ReceiverBuffer::new(config, wants_to_quit, rx, stdout);401+ let mut rxbuffer = ReceiverBuffer::new(config, quit_flag, interrupt_flag, rx, stdout);402 rxbuffer.process()403 }404 })405 }55 unmodified lines461 }462 463 fn spawn_senders(464 config: &Arc<Config>,450- wants_to_quit: &Arc<AtomicBool>,465+ quit_flag: &Arc<AtomicBool>,466 pattern: Arc<Regex>,467 parallel_walker: ignore::WalkParallel,468 tx: Sender<WorkerResult>,469 ) {470 parallel_walker.run(|| {471 let config = Arc::clone(config);472 let pattern = Arc::clone(&pattern);473 let tx_thread = tx.clone();459- let wants_to_quit = Arc::clone(wants_to_quit);474+ let quit_flag = Arc::clone(quit_flag);475 476 Box::new(move |entry_o| {462- if wants_to_quit.load(Ordering::Relaxed) {477+ if quit_flag.load(Ordering::Relaxed) {478 return ignore::WalkState::Quit;479 }480 481 let entry = match entry_o {146 unmodified lines