Skip to content

internal: Recognize proc macro server unexpectedly exiting and report the exit code #16309

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions crates/proc-macro-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ mod version;
use indexmap::IndexSet;
use paths::AbsPathBuf;
use span::Span;
use std::{fmt, io, sync::Mutex};
use triomphe::Arc;
use std::{
fmt, io,
sync::{Arc, Mutex},
};

use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -81,9 +83,11 @@ impl PartialEq for ProcMacro {
}
}

#[derive(Clone, Debug)]
pub struct ServerError {
pub message: String,
pub io: Option<io::Error>,
// io::Error isn't Clone for some reason
pub io: Option<Arc<io::Error>>,
}

impl fmt::Display for ServerError {
Expand Down
51 changes: 43 additions & 8 deletions crates/proc-macro-api/src/process.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
//! Handle process life-time and message passing for proc-macro client

use std::{
io::{self, BufRead, BufReader, Write},
io::{self, BufRead, BufReader, Read, Write},
process::{Child, ChildStdin, ChildStdout, Command, Stdio},
sync::Arc,
};

use paths::{AbsPath, AbsPathBuf};
Expand All @@ -15,9 +16,11 @@ use crate::{

#[derive(Debug)]
pub(crate) struct ProcMacroProcessSrv {
_process: Process,
process: Process,
stdin: ChildStdin,
stdout: BufReader<ChildStdout>,
/// Populated when the server exits.
server_exited: Option<ServerError>,
version: u32,
mode: SpanMode,
}
Expand All @@ -29,9 +32,10 @@ impl ProcMacroProcessSrv {
let (stdin, stdout) = process.stdio().expect("couldn't access child stdio");

io::Result::Ok(ProcMacroProcessSrv {
_process: process,
process,
stdin,
stdout,
server_exited: None,
version: 0,
mode: SpanMode::Id,
})
Expand Down Expand Up @@ -105,8 +109,35 @@ impl ProcMacroProcessSrv {
}

pub(crate) fn send_task(&mut self, req: Request) -> Result<Response, ServerError> {
if let Some(server_error) = &self.server_exited {
return Err(server_error.clone());
}

let mut buf = String::new();
send_request(&mut self.stdin, &mut self.stdout, req, &mut buf)
send_request(&mut self.stdin, &mut self.stdout, req, &mut buf).map_err(|e| {
if e.io.as_ref().map(|it| it.kind()) == Some(io::ErrorKind::BrokenPipe) {
match self.process.child.try_wait() {
Ok(None) => e,
Ok(Some(status)) => {
let mut msg = String::new();
if !status.success() {
if let Some(stderr) = self.process.child.stderr.as_mut() {
_ = stderr.read_to_string(&mut msg);
}
}
let server_error = ServerError {
message: format!("server exited with {status}: {msg}"),
io: None,
};
self.server_exited = Some(server_error.clone());
server_error
}
Err(_) => e,
}
} else {
e
}
})
}
}

Expand Down Expand Up @@ -145,9 +176,13 @@ fn send_request(
req: Request,
buf: &mut String,
) -> Result<Response, ServerError> {
req.write(&mut writer)
.map_err(|err| ServerError { message: "failed to write request".into(), io: Some(err) })?;
let res = Response::read(&mut reader, buf)
.map_err(|err| ServerError { message: "failed to read response".into(), io: Some(err) })?;
req.write(&mut writer).map_err(|err| ServerError {
message: "failed to write request".into(),
io: Some(Arc::new(err)),
})?;
let res = Response::read(&mut reader, buf).map_err(|err| ServerError {
message: "failed to read response".into(),
io: Some(Arc::new(err)),
})?;
res.ok_or_else(|| ServerError { message: "server exited".into(), io: None })
}
3 changes: 2 additions & 1 deletion crates/proc-macro-srv-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ fn main() -> std::io::Result<()> {

#[cfg(not(any(feature = "sysroot-abi", rust_analyzer)))]
fn run() -> io::Result<()> {
panic!("proc-macro-srv-cli requires the `sysroot-abi` feature to be enabled");
eprintln!("proc-macro-srv-cli requires the `sysroot-abi` feature to be enabled");
std::process::exit(70);
}

#[cfg(any(feature = "sysroot-abi", rust_analyzer))]
Expand Down