Skip to content

Commit d4a04da

Browse files
committed
Auto merge of #6515 - ehuss:fix-fix-diag-race, r=alexcrichton
Fix a very minor race condition in `cargo fix`. There is a very rare race where a "fix" message was getting posted *after* `Message::Finish`, and thus being dropped without being printed. This is caused by the diagnostic server disconnecting the client before posting `Message::FixDiagnostic`. The solution is to keep the client on the line until after the message is posted. Fixes some errors in `fixes_missing_ampersand` seen in CI: https://travis-ci.org/rust-lang/cargo/jobs/474384359 https://travis-ci.org/rust-lang/cargo/jobs/429809800 I also moved the `done` check to the beginning of the loop because every time the server was shut down it was needlessly trying to parse an empty string (and failing).
2 parents dd97f8b + 7584dce commit d4a04da

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

src/cargo/util/diagnostic_server.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -258,14 +258,23 @@ impl RustfixDiagnosticServer {
258258

259259
fn run(self, on_message: &dyn Fn(Message), done: &AtomicBool) {
260260
while let Ok((client, _)) = self.listener.accept() {
261-
let client = BufReader::new(client);
262-
match serde_json::from_reader(client) {
263-
Ok(message) => on_message(message),
264-
Err(e) => warn!("invalid diagnostics message: {}", e),
265-
}
266261
if done.load(Ordering::SeqCst) {
267262
break;
268263
}
264+
let mut client = BufReader::new(client);
265+
let mut s = String::new();
266+
if let Err(e) = client.read_to_string(&mut s) {
267+
warn!("diagnostic server failed to read: {}", e);
268+
} else {
269+
match serde_json::from_str(&s) {
270+
Ok(message) => on_message(message),
271+
Err(e) => warn!("invalid diagnostics message: {}", e),
272+
}
273+
}
274+
// The client should be kept alive until after `on_message` is
275+
// called to ensure that the client doesn't exit too soon (and
276+
// Message::Finish getting posted before Message::FixDiagnostic).
277+
drop(client);
269278
}
270279
}
271280
}

0 commit comments

Comments
 (0)