Skip to content

Commit

Permalink
Write to stderr during connection shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
snowsignal committed Jun 5, 2024
1 parent d2986ba commit 4a4197e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
16 changes: 11 additions & 5 deletions crates/ruff_server/src/server/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,21 +88,27 @@ impl Connection {
lsp::Message::Request(lsp::Request { id, method, .. })
if method == lsp_types::request::Shutdown::METHOD =>
{
self.sender
.send(lsp::Response::new_ok(id.clone(), ()).into())?;
tracing::info!("Shutdown request received. Waiting for an exit notification...");
match self.receiver.recv_timeout(std::time::Duration::from_secs(30))? {
tracing::subscriber::with_default(crate::trace::stderr_subscriber(), || {
self.sender
.send(lsp::Response::new_ok(id.clone(), ()).into())?;
tracing::info!(
"Shutdown request received. Waiting for an exit notification..."
);
match self.receiver.recv_timeout(std::time::Duration::from_secs(30))? {
lsp::Message::Notification(lsp::Notification { method, .. }) if method == lsp_types::notification::Exit::METHOD => {
tracing::info!("Exit notification received. Server shutting down...");
Ok(true)
},
message => anyhow::bail!("Server received unexpected message {message:?} while waiting for exit notification")
}
})
}
lsp::Message::Notification(lsp::Notification { method, .. })
if method == lsp_types::notification::Exit::METHOD =>
{
tracing::error!("Server received an exit notification before a shutdown request was sent. Exiting...");
tracing::subscriber::with_default(crate::trace::stderr_subscriber(), || {
tracing::error!("Server received an exit notification before a shutdown request was sent. Exiting...");
});
Ok(true)
}
_ => Ok(false),
Expand Down
5 changes: 5 additions & 0 deletions crates/ruff_server/src/trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ static LOGGING_SENDER: OnceLock<ClientSender> = OnceLock::new();

static TRACE_VALUE: Mutex<lsp_types::TraceValue> = Mutex::new(lsp_types::TraceValue::Off);

pub(crate) fn stderr_subscriber() -> impl tracing::Subscriber {
tracing_subscriber::registry()
.with(tracing_subscriber::fmt::layer().with_writer(|| Box::new(std::io::stderr())))
}

pub(crate) fn set_trace_value(trace_value: TraceValue) {
let mut global_trace_value = TRACE_VALUE
.lock()
Expand Down

0 comments on commit 4a4197e

Please sign in to comment.