From 7584dced280f09fa30d7a101a145b09854f90832 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Wed, 2 Jan 2019 21:46:14 -0800 Subject: [PATCH] Fix a very minor race condition in `cargo fix`. --- src/cargo/util/diagnostic_server.rs | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/cargo/util/diagnostic_server.rs b/src/cargo/util/diagnostic_server.rs index 48da0a3d2bd..e7e1aff1bb7 100644 --- a/src/cargo/util/diagnostic_server.rs +++ b/src/cargo/util/diagnostic_server.rs @@ -258,14 +258,23 @@ impl RustfixDiagnosticServer { fn run(self, on_message: &dyn Fn(Message), done: &AtomicBool) { while let Ok((client, _)) = self.listener.accept() { - let client = BufReader::new(client); - match serde_json::from_reader(client) { - Ok(message) => on_message(message), - Err(e) => warn!("invalid diagnostics message: {}", e), - } if done.load(Ordering::SeqCst) { break; } + let mut client = BufReader::new(client); + let mut s = String::new(); + if let Err(e) = client.read_to_string(&mut s) { + warn!("diagnostic server failed to read: {}", e); + } else { + match serde_json::from_str(&s) { + Ok(message) => on_message(message), + Err(e) => warn!("invalid diagnostics message: {}", e), + } + } + // The client should be kept alive until after `on_message` is + // called to ensure that the client doesn't exit too soon (and + // Message::Finish getting posted before Message::FixDiagnostic). + drop(client); } } }