5114: Cleanup cargo process handling in flycheck r=matklad a=matklad



bors r+
🤖

Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
bors[bot] 2020-06-28 21:43:18 +00:00 committed by GitHub
commit 11f31ae4c3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 137 additions and 103 deletions

View file

@ -5,8 +5,9 @@
use std::{ use std::{
fmt, fmt,
io::{self, BufReader}, io::{self, BufReader},
ops,
path::PathBuf, path::PathBuf,
process::{Command, Stdio}, process::{self, Command, Stdio},
time::Duration, time::Duration,
}; };
@ -49,8 +50,8 @@ impl fmt::Display for FlycheckConfig {
#[derive(Debug)] #[derive(Debug)]
pub struct FlycheckHandle { pub struct FlycheckHandle {
// XXX: drop order is significant // XXX: drop order is significant
cmd_send: Sender<Restart>, sender: Sender<Restart>,
handle: jod_thread::JoinHandle, thread: jod_thread::JoinHandle,
} }
impl FlycheckHandle { impl FlycheckHandle {
@ -59,16 +60,15 @@ impl FlycheckHandle {
config: FlycheckConfig, config: FlycheckConfig,
workspace_root: PathBuf, workspace_root: PathBuf,
) -> FlycheckHandle { ) -> FlycheckHandle {
let (cmd_send, cmd_recv) = unbounded::<Restart>(); let actor = FlycheckActor::new(sender, config, workspace_root);
let handle = jod_thread::spawn(move || { let (sender, receiver) = unbounded::<Restart>();
FlycheckActor::new(sender, config, workspace_root).run(cmd_recv); let thread = jod_thread::spawn(move || actor.run(receiver));
}); FlycheckHandle { sender, thread }
FlycheckHandle { cmd_send, handle }
} }
/// Schedule a re-start of the cargo check worker. /// Schedule a re-start of the cargo check worker.
pub fn update(&self) { pub fn update(&self) {
self.cmd_send.send(Restart).unwrap(); self.sender.send(Restart).unwrap();
} }
} }
@ -85,7 +85,7 @@ pub enum Message {
pub enum Progress { pub enum Progress {
DidStart, DidStart,
DidCheckCrate(String), DidCheckCrate(String),
DidFinish, DidFinish(io::Result<()>),
DidCancel, DidCancel,
} }
@ -100,8 +100,7 @@ struct FlycheckActor {
/// doesn't provide a way to read sub-process output without blocking, so we /// doesn't provide a way to read sub-process output without blocking, so we
/// have to wrap sub-processes output handling in a thread and pass messages /// have to wrap sub-processes output handling in a thread and pass messages
/// back over a channel. /// back over a channel.
// XXX: drop order is significant cargo_handle: Option<CargoHandle>,
check_process: Option<(Receiver<cargo_metadata::Message>, jod_thread::JoinHandle)>,
} }
enum Event { enum Event {
@ -115,29 +114,36 @@ impl FlycheckActor {
config: FlycheckConfig, config: FlycheckConfig,
workspace_root: PathBuf, workspace_root: PathBuf,
) -> FlycheckActor { ) -> FlycheckActor {
FlycheckActor { sender, config, workspace_root, check_process: None } FlycheckActor { sender, config, workspace_root, cargo_handle: None }
} }
fn next_event(&self, inbox: &Receiver<Restart>) -> Option<Event> { fn next_event(&self, inbox: &Receiver<Restart>) -> Option<Event> {
let check_chan = self.check_process.as_ref().map(|(chan, _thread)| chan); let check_chan = self.cargo_handle.as_ref().map(|cargo| &cargo.receiver);
select! { select! {
recv(inbox) -> msg => msg.ok().map(Event::Restart), recv(inbox) -> msg => msg.ok().map(Event::Restart),
recv(check_chan.unwrap_or(&never())) -> msg => Some(Event::CheckEvent(msg.ok())), recv(check_chan.unwrap_or(&never())) -> msg => Some(Event::CheckEvent(msg.ok())),
} }
} }
fn run(&mut self, inbox: Receiver<Restart>) { fn run(mut self, inbox: Receiver<Restart>) {
while let Some(event) = self.next_event(&inbox) { while let Some(event) = self.next_event(&inbox) {
match event { match event {
Event::Restart(Restart) => { Event::Restart(Restart) => {
while let Ok(Restart) = inbox.recv_timeout(Duration::from_millis(50)) {} while let Ok(Restart) = inbox.recv_timeout(Duration::from_millis(50)) {}
self.cancel_check_process(); self.cancel_check_process();
self.check_process = Some(self.start_check_process());
let mut command = self.check_command();
command.stdout(Stdio::piped()).stderr(Stdio::null()).stdin(Stdio::null());
if let Ok(child) = command.spawn().map(JodChild) {
self.cargo_handle = Some(CargoHandle::spawn(child));
self.send(Message::Progress(Progress::DidStart)); self.send(Message::Progress(Progress::DidStart));
} }
}
Event::CheckEvent(None) => { Event::CheckEvent(None) => {
// Watcher finished, replace it with a never channel to // Watcher finished, replace it with a never channel to
// avoid busy-waiting. // avoid busy-waiting.
assert!(self.check_process.take().is_some()); let cargo_handle = self.cargo_handle.take().unwrap();
self.send(Message::Progress(Progress::DidFinish)); let res = cargo_handle.join();
self.send(Message::Progress(Progress::DidFinish(res)));
} }
Event::CheckEvent(Some(message)) => match message { Event::CheckEvent(Some(message)) => match message {
cargo_metadata::Message::CompilerArtifact(msg) => { cargo_metadata::Message::CompilerArtifact(msg) => {
@ -162,11 +168,11 @@ impl FlycheckActor {
self.cancel_check_process(); self.cancel_check_process();
} }
fn cancel_check_process(&mut self) { fn cancel_check_process(&mut self) {
if self.check_process.take().is_some() { if self.cargo_handle.take().is_some() {
self.send(Message::Progress(Progress::DidCancel)); self.send(Message::Progress(Progress::DidCancel));
} }
} }
fn start_check_process(&self) -> (Receiver<cargo_metadata::Message>, jod_thread::JoinHandle) { fn check_command(&self) -> Command {
let mut cmd = match &self.config { let mut cmd = match &self.config {
FlycheckConfig::CargoCommand { FlycheckConfig::CargoCommand {
command, command,
@ -198,33 +204,7 @@ impl FlycheckActor {
} }
}; };
cmd.current_dir(&self.workspace_root); cmd.current_dir(&self.workspace_root);
cmd
let (message_send, message_recv) = unbounded();
let thread = jod_thread::spawn(move || {
// If we trigger an error here, we will do so in the loop instead,
// which will break out of the loop, and continue the shutdown
let res = run_cargo(cmd, &mut |message| {
// Skip certain kinds of messages to only spend time on what's useful
match &message {
cargo_metadata::Message::CompilerArtifact(artifact) if artifact.fresh => {
return true
}
cargo_metadata::Message::BuildScriptExecuted(_)
| cargo_metadata::Message::Unknown => return true,
_ => {}
}
// if the send channel was closed, we want to shutdown
message_send.send(message).is_ok()
});
if let Err(err) = res {
// FIXME: make the `message_send` to be `Sender<Result<CheckEvent, CargoError>>`
// to display user-caused misconfiguration errors instead of just logging them here
log::error!("Cargo watcher failed {:?}", err);
}
});
(message_recv, thread)
} }
fn send(&self, check_task: Message) { fn send(&self, check_task: Message) {
@ -232,22 +212,63 @@ impl FlycheckActor {
} }
} }
fn run_cargo( struct CargoHandle {
mut command: Command, child: JodChild,
on_message: &mut dyn FnMut(cargo_metadata::Message) -> bool, #[allow(unused)]
) -> io::Result<()> { thread: jod_thread::JoinHandle<io::Result<bool>>,
let mut child = receiver: Receiver<cargo_metadata::Message>,
command.stdout(Stdio::piped()).stderr(Stdio::null()).stdin(Stdio::null()).spawn()?; }
impl CargoHandle {
fn spawn(mut child: JodChild) -> CargoHandle {
let child_stdout = child.stdout.take().unwrap();
let (sender, receiver) = unbounded();
let actor = CargoActor::new(child_stdout, sender);
let thread = jod_thread::spawn(move || actor.run());
CargoHandle { child, thread, receiver }
}
fn join(mut self) -> io::Result<()> {
// It is okay to ignore the result, as it only errors if the process is already dead
let _ = self.child.kill();
let exit_status = self.child.wait()?;
let read_at_least_one_message = self.thread.join()?;
if !exit_status.success() && !read_at_least_one_message {
// FIXME: Read the stderr to display the reason, see `read2()` reference in PR comment:
// https://github.com/rust-analyzer/rust-analyzer/pull/3632#discussion_r395605298
return Err(io::Error::new(
io::ErrorKind::Other,
format!(
"Cargo watcher failed,the command produced no valid metadata (exit code: {:?})",
exit_status
),
));
}
Ok(())
}
}
struct CargoActor {
child_stdout: process::ChildStdout,
sender: Sender<cargo_metadata::Message>,
}
impl CargoActor {
fn new(
child_stdout: process::ChildStdout,
sender: Sender<cargo_metadata::Message>,
) -> CargoActor {
CargoActor { child_stdout, sender }
}
fn run(self) -> io::Result<bool> {
// We manually read a line at a time, instead of using serde's // We manually read a line at a time, instead of using serde's
// stream deserializers, because the deserializer cannot recover // stream deserializers, because the deserializer cannot recover
// from an error, resulting in it getting stuck, because we try to // from an error, resulting in it getting stuck, because we try to
// be resillient against failures. // be resilient against failures.
// //
// Because cargo only outputs one JSON object per line, we can // Because cargo only outputs one JSON object per line, we can
// simply skip a line if it doesn't parse, which just ignores any // simply skip a line if it doesn't parse, which just ignores any
// erroneus output. // erroneus output.
let stdout = BufReader::new(child.stdout.take().unwrap()); let stdout = BufReader::new(self.child_stdout);
let mut read_at_least_one_message = false; let mut read_at_least_one_message = false;
for message in cargo_metadata::Message::parse_stream(stdout) { for message in cargo_metadata::Message::parse_stream(stdout) {
let message = match message { let message = match message {
@ -260,26 +281,35 @@ fn run_cargo(
read_at_least_one_message = true; read_at_least_one_message = true;
if !on_message(message) { // Skip certain kinds of messages to only spend time on what's useful
break; match &message {
cargo_metadata::Message::CompilerArtifact(artifact) if artifact.fresh => (),
cargo_metadata::Message::BuildScriptExecuted(_)
| cargo_metadata::Message::Unknown => (),
_ => self.sender.send(message).unwrap(),
} }
} }
Ok(read_at_least_one_message)
// It is okay to ignore the result, as it only errors if the process is already dead }
let _ = child.kill(); }
let exit_status = child.wait()?; struct JodChild(process::Child);
if !exit_status.success() && !read_at_least_one_message {
// FIXME: Read the stderr to display the reason, see `read2()` reference in PR comment: impl ops::Deref for JodChild {
// https://github.com/rust-analyzer/rust-analyzer/pull/3632#discussion_r395605298 type Target = process::Child;
return Err(io::Error::new( fn deref(&self) -> &process::Child {
io::ErrorKind::Other, &self.0
format!( }
"the command produced no valid metadata (exit code: {:?}): {:?}", }
exit_status, command
), impl ops::DerefMut for JodChild {
)); fn deref_mut(&mut self) -> &mut process::Child {
&mut self.0
}
}
impl Drop for JodChild {
fn drop(&mut self) {
let _ = self.0.kill();
} }
Ok(())
} }

View file

@ -216,7 +216,11 @@ impl GlobalState {
flycheck::Progress::DidCheckCrate(target) => { flycheck::Progress::DidCheckCrate(target) => {
(Progress::Report, Some(target)) (Progress::Report, Some(target))
} }
flycheck::Progress::DidFinish | flycheck::Progress::DidCancel => { flycheck::Progress::DidCancel => (Progress::End, None),
flycheck::Progress::DidFinish(result) => {
if let Err(err) = result {
log::error!("cargo check failed: {}", err)
}
(Progress::End, None) (Progress::End, None)
} }
}; };

View file

@ -10,7 +10,7 @@ mod include;
use std::convert::{TryFrom, TryInto}; use std::convert::{TryFrom, TryInto};
use crossbeam_channel::{select, unbounded, Receiver}; use crossbeam_channel::{select, unbounded, Receiver, Sender};
use notify::{RecommendedWatcher, RecursiveMode, Watcher}; use notify::{RecommendedWatcher, RecursiveMode, Watcher};
use paths::{AbsPath, AbsPathBuf}; use paths::{AbsPath, AbsPathBuf};
use rustc_hash::FxHashSet; use rustc_hash::FxHashSet;
@ -22,8 +22,8 @@ use crate::include::Include;
#[derive(Debug)] #[derive(Debug)]
pub struct NotifyHandle { pub struct NotifyHandle {
// Relative order of fields below is significant. // Relative order of fields below is significant.
sender: crossbeam_channel::Sender<Message>, sender: Sender<Message>,
_thread: jod_thread::JoinHandle, thread: jod_thread::JoinHandle,
} }
#[derive(Debug)] #[derive(Debug)]
@ -37,7 +37,7 @@ impl loader::Handle for NotifyHandle {
let actor = NotifyActor::new(sender); let actor = NotifyActor::new(sender);
let (sender, receiver) = unbounded::<Message>(); let (sender, receiver) = unbounded::<Message>();
let thread = jod_thread::spawn(move || actor.run(receiver)); let thread = jod_thread::spawn(move || actor.run(receiver));
NotifyHandle { sender, _thread: thread } NotifyHandle { sender, thread }
} }
fn set_config(&mut self, config: loader::Config) { fn set_config(&mut self, config: loader::Config) {
self.sender.send(Message::Config(config)).unwrap() self.sender.send(Message::Config(config)).unwrap()