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::{
fmt,
io::{self, BufReader},
ops,
path::PathBuf,
process::{Command, Stdio},
process::{self, Command, Stdio},
time::Duration,
};
@ -49,8 +50,8 @@ impl fmt::Display for FlycheckConfig {
#[derive(Debug)]
pub struct FlycheckHandle {
// XXX: drop order is significant
cmd_send: Sender<Restart>,
handle: jod_thread::JoinHandle,
sender: Sender<Restart>,
thread: jod_thread::JoinHandle,
}
impl FlycheckHandle {
@ -59,16 +60,15 @@ impl FlycheckHandle {
config: FlycheckConfig,
workspace_root: PathBuf,
) -> FlycheckHandle {
let (cmd_send, cmd_recv) = unbounded::<Restart>();
let handle = jod_thread::spawn(move || {
FlycheckActor::new(sender, config, workspace_root).run(cmd_recv);
});
FlycheckHandle { cmd_send, handle }
let actor = FlycheckActor::new(sender, config, workspace_root);
let (sender, receiver) = unbounded::<Restart>();
let thread = jod_thread::spawn(move || actor.run(receiver));
FlycheckHandle { sender, thread }
}
/// Schedule a re-start of the cargo check worker.
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 {
DidStart,
DidCheckCrate(String),
DidFinish,
DidFinish(io::Result<()>),
DidCancel,
}
@ -100,8 +100,7 @@ struct FlycheckActor {
/// 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
/// back over a channel.
// XXX: drop order is significant
check_process: Option<(Receiver<cargo_metadata::Message>, jod_thread::JoinHandle)>,
cargo_handle: Option<CargoHandle>,
}
enum Event {
@ -115,29 +114,36 @@ impl FlycheckActor {
config: FlycheckConfig,
workspace_root: PathBuf,
) -> 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> {
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! {
recv(inbox) -> msg => msg.ok().map(Event::Restart),
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) {
match event {
Event::Restart(Restart) => {
while let Ok(Restart) = inbox.recv_timeout(Duration::from_millis(50)) {}
self.cancel_check_process();
self.check_process = Some(self.start_check_process());
self.send(Message::Progress(Progress::DidStart));
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));
}
}
Event::CheckEvent(None) => {
// Watcher finished, replace it with a never channel to
// avoid busy-waiting.
assert!(self.check_process.take().is_some());
self.send(Message::Progress(Progress::DidFinish));
let cargo_handle = self.cargo_handle.take().unwrap();
let res = cargo_handle.join();
self.send(Message::Progress(Progress::DidFinish(res)));
}
Event::CheckEvent(Some(message)) => match message {
cargo_metadata::Message::CompilerArtifact(msg) => {
@ -162,11 +168,11 @@ impl FlycheckActor {
self.cancel_check_process();
}
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));
}
}
fn start_check_process(&self) -> (Receiver<cargo_metadata::Message>, jod_thread::JoinHandle) {
fn check_command(&self) -> Command {
let mut cmd = match &self.config {
FlycheckConfig::CargoCommand {
command,
@ -198,33 +204,7 @@ impl FlycheckActor {
}
};
cmd.current_dir(&self.workspace_root);
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)
cmd
}
fn send(&self, check_task: Message) {
@ -232,54 +212,104 @@ impl FlycheckActor {
}
}
fn run_cargo(
mut command: Command,
on_message: &mut dyn FnMut(cargo_metadata::Message) -> bool,
) -> io::Result<()> {
let mut child =
command.stdout(Stdio::piped()).stderr(Stdio::null()).stdin(Stdio::null()).spawn()?;
// We manually read a line at a time, instead of using serde's
// stream deserializers, because the deserializer cannot recover
// from an error, resulting in it getting stuck, because we try to
// be resillient against failures.
//
// Because cargo only outputs one JSON object per line, we can
// simply skip a line if it doesn't parse, which just ignores any
// erroneus output.
let stdout = BufReader::new(child.stdout.take().unwrap());
let mut read_at_least_one_message = false;
for message in cargo_metadata::Message::parse_stream(stdout) {
let message = match message {
Ok(message) => message,
Err(err) => {
log::error!("Invalid json from cargo check, ignoring ({})", err);
continue;
}
};
read_at_least_one_message = true;
if !on_message(message) {
break;
}
}
// 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()?;
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!(
"the command produced no valid metadata (exit code: {:?}): {:?}",
exit_status, command
),
));
}
Ok(())
struct CargoHandle {
child: JodChild,
#[allow(unused)]
thread: jod_thread::JoinHandle<io::Result<bool>>,
receiver: Receiver<cargo_metadata::Message>,
}
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
// stream deserializers, because the deserializer cannot recover
// from an error, resulting in it getting stuck, because we try to
// be resilient against failures.
//
// Because cargo only outputs one JSON object per line, we can
// simply skip a line if it doesn't parse, which just ignores any
// erroneus output.
let stdout = BufReader::new(self.child_stdout);
let mut read_at_least_one_message = false;
for message in cargo_metadata::Message::parse_stream(stdout) {
let message = match message {
Ok(message) => message,
Err(err) => {
log::error!("Invalid json from cargo check, ignoring ({})", err);
continue;
}
};
read_at_least_one_message = true;
// Skip certain kinds of messages to only spend time on what's useful
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)
}
}
struct JodChild(process::Child);
impl ops::Deref for JodChild {
type Target = process::Child;
fn deref(&self) -> &process::Child {
&self.0
}
}
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();
}
}

View file

@ -216,7 +216,11 @@ impl GlobalState {
flycheck::Progress::DidCheckCrate(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)
}
};

View file

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