Merge pull request #902 from tavianator/quit-senders
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Quit senders more aggressively
authored by Tavian Barnes · committed by GitHub · parents 5ea7cb7, 4baefb2 · Browse files · 2 files·+44 added−34 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+44 added−34 removed
- other:+44 added−34 removed
Provenance
- Commit
81669f4- Tree
8c1a9e2- Author
Tavian Barnes <tavianator@tavianator.com>- Committer
GitHub <noreply@github.com>- Parent 1
5ea7cb7- Parent 2
4baefb2
2 files changed+44 added−34 removed
modified:src/output.rs+2 added-15 removed
1 use std::borrow::Cow;2 use std::io::{self, Write};3 use std::path::Path;4-use std::sync::atomic::{AtomicBool, Ordering};4 5 use lscolors::{Indicator, LsColors, Style};6 7 use crate::config::Config;5 unmodified lines13 path.replace(std::path::MAIN_SEPARATOR, new_path_separator)14 }15 16 // TODO: this function is performance critical and can probably be optimized18-pub fn print_entry<W: Write>(19- stdout: &mut W,20- entry: &Path,21- config: &Config,22- wants_to_quit: &AtomicBool,23-) {17+pub fn print_entry<W: Write>(stdout: &mut W, entry: &Path, config: &Config) {18 let path = if config.strip_cwd_prefix {19 strip_current_dir(entry)20 } else {21 entry22 };23 24 let r = if let Some(ref ls_colors) = config.ls_colors {31- print_entry_colorized(stdout, path, config, ls_colors, wants_to_quit)25+ print_entry_colorized(stdout, path, config, ls_colors)26 } else {27 print_entry_uncolorized(stdout, path, config)28 };29 13 unmodified lines43 stdout: &mut W,44 path: &Path,45 config: &Config,46 ls_colors: &LsColors,53- wants_to_quit: &AtomicBool,47 ) -> io::Result<()> {48 // Split the path between the parent and the last component49 let mut offset = 0;50 let path_str = path.to_string_lossy();33 unmodified lines84 } else {85 writeln!(stdout)?;86 }87 95- if wants_to_quit.load(Ordering::Relaxed) {96- // Ignore any errors on flush, because we're about to exit anyway97- let _ = stdout.flush();98- ExitCode::KilledBySigint.exit();99- }100-88 Ok(())89 }90 91 // TODO: this function is performance critical and can probably be optimized41 unmodified linesmodified:src/walk.rs+42 added-19 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 }27 unmodified lines256 self.stream()?;257 }258 }259 ReceiverMode::Streaming => {247- self.print(&path);260+ self.print(&path)?;261 self.flush()?;262 }263 }264 20 unmodified lines285 Ok(())286 }287 288 /// Output a path.276- fn print(&mut self, path: &Path) {277- output::print_entry(&mut self.stdout, path, &self.config, &self.wants_to_quit)289+ fn print(&mut self, path: &Path) -> Result<(), ExitCode> {290+ output::print_entry(&mut self.stdout, path, &self.config);291+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+ }297+298+ Ok(())299 }300 301 /// Switch ourselves into streaming mode.302 fn stream(&mut self) -> Result<(), ExitCode> {303 self.mode = ReceiverMode::Streaming;304 305 let buffer = mem::take(&mut self.buffer);306 for path in buffer {286- self.print(&path);307+ self.print(&path)?;308 }309 310 self.flush()311 }23 unmodified lines335 }336 337 fn spawn_receiver(338 config: &Arc<Config>,318- 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);322- 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 378- 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>,442- 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();451- let wants_to_quit = Arc::clone(wants_to_quit);474+ let quit_flag = Arc::clone(quit_flag);475 476 Box::new(move |entry_o| {454- 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