Skip to content

Make server panic hook more error resilient - #12610

Merged
MichaReiser merged 2 commits into
mainfrom
server-panic-hook-error-resilience
Aug 2, 2024
Merged

MichaReiser merged 2 commits into
mainfrom
server-panic-hook-error-resilience

Conversation

@MichaReiser

@MichaReiser MichaReiser commented Aug 1, 2024 •

Copy link
Copy Markdown
Member

Summary

eprintln can panic when the stderr pipe is broken which is very unfortunate if it happens in the server's panic hook.

This PR uses writeln with .ok() as a more forgiving way to write the error message.

This should fix #12545 although it is still unclear to me how to get into the broken pipe case.

Test Plan

I added a panic in the diagnostics request:

  • NeoVim shows an error that the ruff server panicked
  • The log contains the panic
        0.008529319s ERROR ruff:worker:0 ruff_server::message: panicked at crates/ruff_server/src/server/api/requests/diagnostic.rs:23:9:
      no diagnostics for you
         0: ruff_server::message::init_messenger::{{closure}}
         1: std::panicking::rust_panic_with_hook
         2: std::panicking::begin_panic_handler::{{closure}}
         3: std::sys_common::backtrace::__rust_end_short_backtrace
         4: rust_begin_unwind
         5: core::panicking::panic_fmt
         6: <ruff_server::server::api::requests::diagnostic::DocumentDiagnostic as ruff_server::server::api::traits::BackgroundDocumentRequestHandler>::run_with_snapshot
         7: core::ops::function::FnOnce::call_once{{vtable.shim}}
         8: core::ops::function::FnOnce::call_once{{vtable.shim}}
         9: std::sys_common::backtrace::__rust_begin_short_backtrace
        10: core::ops::function::FnOnce::call_once{{vtable.shim}}
        11: std::sys::pal::unix::thread::Thread::new::thread_start
        12: <unknown>
        13: <unknown>
    

@MichaReiser MichaReiser changed the title Make ruff-server panic hook more error resilient Make server panic hook more error resilient Aug 1, 2024
@MichaReiser MichaReiser added bug An issue describing something that isn't working, or a PR that fixes a bug server Related to the Ruff or ty LSP server labels Aug 1, 2024
@MichaReiser
MichaReiser force-pushed the server-panic-hook-error-resilience branch from 4d02556 to 98ec2fe Compare August 1, 2024 11:18
@MichaReiser
MichaReiser marked this pull request as ready for review August 1, 2024 11:18
@MichaReiser
MichaReiser force-pushed the server-panic-hook-error-resilience branch from 98ec2fe to 16b0a9d Compare August 1, 2024 11:24
@MichaReiser
MichaReiser force-pushed the server-panic-hook-error-resilience branch from 16b0a9d to 1ff2f9b Compare August 1, 2024 11:27
Comment thread crates/ruff/src/main.rs
// This communicates that this isn't a linter error but ruff itself hard-errored for
// some reason (e.g. failed to resolve the configuration)
eprintln!("{}", "ruff failed".red().bold());
writeln!(stderr, "{}", "ruff failed".red().bold()).ok();

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at the original trace, I strongly suspect that ruff panics here... when trying to print the reason why ruff errored:

failed printing to stderr: Broken pipe (os error 32)
   0: ruff_server::message::init_messenger::{{closure}}
   1: std::panicking::rust_panic_with_hook
   2: std::panicking::begin_panic_handler::{{closure}}
   3: std::sys_common::backtrace::__rust_end_short_backtrace
   4: rust_begin_unwind
   5: core::panicking::panic_fmt
   6: std::io::stdio::_eprint
   7: ruff::main
   8: std::sys_common::backtrace::__rust_begin_short_backtrace
   9: std::rt::lang_start::{{closure}}
  10: std::rt::lang_start_internal
  11: main
  12: <unknown>
  13: __libc_start_main
  14: <unknown>

@github-actions

github-actions Bot commented Aug 1, 2024 •

Copy link
Copy Markdown
Contributor

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

@MichaReiser
MichaReiser requested a review from dhruvmanila August 1, 2024 21:35
Comment on lines +166 to +167
let mut stderr = std::io::stderr().lock();
writeln!(stderr, "{panic_info}\n{backtrace}").ok();

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The main change is to use writeln here

Comment on lines -13 to -42

// unregister any previously registered panic hook
let _ = std::panic::take_hook();

// When we panic, try to notify the client.
std::panic::set_hook(Box::new(move |panic_info| {
if let Some(messenger) = MESSENGER.get() {
let _ = messenger.send(lsp_server::Message::Notification(
lsp_server::Notification {
method: lsp_types::notification::ShowMessage::METHOD.into(),
params: serde_json::to_value(lsp_types::ShowMessageParams {
typ: lsp_types::MessageType::ERROR,
message: String::from(
"The Ruff language server exited with a panic. See the logs for more details."
),
})
.unwrap_or_default(),
},
));
}

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}");
}
}));

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I decided to move this out of Messenger to ensure that we unset the panic hook when the server shuts down. We may consider using catch_unwind on a per request/response model in the future. But I think that's good enough for now

@dhruvmanila dhruvmanila left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems reasonable although I've never faced this panic.

@MichaReiser
MichaReiser merged commit 27edade into main Aug 2, 2024
@MichaReiser
MichaReiser deleted the server-panic-hook-error-resilience branch August 2, 2024 10:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug An issue describing something that isn't working, or a PR that fixes a bug server Related to the Ruff or ty LSP server

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Ruff logging core dump messages in the system journal

2 participants