Skip to content

Commit

Permalink
Make ruff-server panic hook more error resilient
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaReiser committed Aug 1, 2024
1 parent a3e67ab commit 98ec2fe
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions crates/ruff_server/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ pub(crate) fn init_messenger(client_sender: ClientSender) {

// When we panic, try to notify the client.
std::panic::set_hook(Box::new(move |panic_info| {
use std::io::Write;

if let Some(messenger) = MESSENGER.get() {
let _ = messenger.send(lsp_server::Message::Notification(
lsp_server::Notification {
Expand All @@ -33,12 +35,12 @@ pub(crate) fn init_messenger(client_sender: ClientSender) {

let backtrace = std::backtrace::Backtrace::force_capture();
tracing::error!("{panic_info}\n{backtrace}");
#[allow(clippy::print_stderr)]
{
// we also need to print to stderr directly in case tracing hasn't
// been initialized.
eprintln!("{panic_info}\n{backtrace}");
}

// we also need to print to stderr directly in case tracing hasn't
// been initialized.
// But use `writeln` instead of `eprintln` to avoid panicking when the stderr pipe is broken.
let mut stderr = std::io::stderr().lock();
writeln!(stderr, "{panic_info}\n{backtrace}").ok();
}));
}

Expand Down

0 comments on commit 98ec2fe

Please sign in to comment.