Skip to content

Commit 3b8fe6d

Browse files
committed
fix: Faulty notifications should not bring down the server
1 parent 77e1969 commit 3b8fe6d

File tree

2 files changed

+24
-28
lines changed

2 files changed

+24
-28
lines changed

crates/rust-analyzer/src/handlers/dispatch.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -325,14 +325,14 @@ impl NotificationDispatcher<'_> {
325325
pub(crate) fn on_sync_mut<N>(
326326
&mut self,
327327
f: fn(&mut GlobalState, N::Params) -> anyhow::Result<()>,
328-
) -> anyhow::Result<&mut Self>
328+
) -> &mut Self
329329
where
330330
N: lsp_types::notification::Notification,
331331
N::Params: DeserializeOwned + Send + Debug,
332332
{
333333
let not = match self.not.take() {
334334
Some(it) => it,
335-
None => return Ok(self),
335+
None => return self,
336336
};
337337

338338
let _guard = tracing::info_span!("notification", method = ?not.method).entered();
@@ -344,7 +344,7 @@ impl NotificationDispatcher<'_> {
344344
}
345345
Err(ExtractError::MethodMismatch(not)) => {
346346
self.not = Some(not);
347-
return Ok(self);
347+
return self;
348348
}
349349
};
350350

@@ -355,8 +355,10 @@ impl NotificationDispatcher<'_> {
355355
version(),
356356
N::METHOD
357357
));
358-
f(self.global_state, params)?;
359-
Ok(self)
358+
if let Err(e) = f(self.global_state, params) {
359+
tracing::error!(handler = %N::METHOD, error = %e, "notification handler failed");
360+
}
361+
self
360362
}
361363

362364
pub(crate) fn finish(&mut self) {

crates/rust-analyzer/src/main_loop.rs

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ impl GlobalState {
196196
) {
197197
return Ok(());
198198
}
199-
self.handle_event(event)?;
199+
self.handle_event(event);
200200
}
201201

202202
Err(anyhow::anyhow!("A receiver has been dropped, something panicked!"))
@@ -278,7 +278,7 @@ impl GlobalState {
278278
.map(Some)
279279
}
280280

281-
fn handle_event(&mut self, event: Event) -> anyhow::Result<()> {
281+
fn handle_event(&mut self, event: Event) {
282282
let loop_start = Instant::now();
283283
let _p = tracing::info_span!("GlobalState::handle_event", event = %event).entered();
284284

@@ -295,7 +295,7 @@ impl GlobalState {
295295
match event {
296296
Event::Lsp(msg) => match msg {
297297
lsp_server::Message::Request(req) => self.on_new_request(loop_start, req),
298-
lsp_server::Message::Notification(not) => self.on_notification(not)?,
298+
lsp_server::Message::Notification(not) => self.on_notification(not),
299299
lsp_server::Message::Response(resp) => self.complete_request(resp),
300300
},
301301
Event::QueuedTask(task) => {
@@ -487,7 +487,6 @@ impl GlobalState {
487487
"overly long loop turn took {loop_duration:?} (event handling took {event_handling_duration:?}): {event_dbg_msg}"
488488
));
489489
}
490-
Ok(())
491490
}
492491

493492
fn prime_caches(&mut self, cause: String) {
@@ -1116,37 +1115,32 @@ impl GlobalState {
11161115
}
11171116

11181117
/// Handles an incoming notification.
1119-
fn on_notification(&mut self, not: Notification) -> anyhow::Result<()> {
1118+
fn on_notification(&mut self, not: Notification) {
11201119
let _p =
11211120
span!(Level::INFO, "GlobalState::on_notification", not.method = ?not.method).entered();
11221121
use crate::handlers::notification as handlers;
11231122
use lsp_types::notification as notifs;
11241123

11251124
NotificationDispatcher { not: Some(not), global_state: self }
1126-
.on_sync_mut::<notifs::Cancel>(handlers::handle_cancel)?
1125+
.on_sync_mut::<notifs::Cancel>(handlers::handle_cancel)
11271126
.on_sync_mut::<notifs::WorkDoneProgressCancel>(
11281127
handlers::handle_work_done_progress_cancel,
1129-
)?
1130-
.on_sync_mut::<notifs::DidOpenTextDocument>(handlers::handle_did_open_text_document)?
1131-
.on_sync_mut::<notifs::DidChangeTextDocument>(
1132-
handlers::handle_did_change_text_document,
1133-
)?
1134-
.on_sync_mut::<notifs::DidCloseTextDocument>(handlers::handle_did_close_text_document)?
1135-
.on_sync_mut::<notifs::DidSaveTextDocument>(handlers::handle_did_save_text_document)?
1128+
)
1129+
.on_sync_mut::<notifs::DidOpenTextDocument>(handlers::handle_did_open_text_document)
1130+
.on_sync_mut::<notifs::DidChangeTextDocument>(handlers::handle_did_change_text_document)
1131+
.on_sync_mut::<notifs::DidCloseTextDocument>(handlers::handle_did_close_text_document)
1132+
.on_sync_mut::<notifs::DidSaveTextDocument>(handlers::handle_did_save_text_document)
11361133
.on_sync_mut::<notifs::DidChangeConfiguration>(
11371134
handlers::handle_did_change_configuration,
1138-
)?
1135+
)
11391136
.on_sync_mut::<notifs::DidChangeWorkspaceFolders>(
11401137
handlers::handle_did_change_workspace_folders,
1141-
)?
1142-
.on_sync_mut::<notifs::DidChangeWatchedFiles>(
1143-
handlers::handle_did_change_watched_files,
1144-
)?
1145-
.on_sync_mut::<lsp_ext::CancelFlycheck>(handlers::handle_cancel_flycheck)?
1146-
.on_sync_mut::<lsp_ext::ClearFlycheck>(handlers::handle_clear_flycheck)?
1147-
.on_sync_mut::<lsp_ext::RunFlycheck>(handlers::handle_run_flycheck)?
1148-
.on_sync_mut::<lsp_ext::AbortRunTest>(handlers::handle_abort_run_test)?
1138+
)
1139+
.on_sync_mut::<notifs::DidChangeWatchedFiles>(handlers::handle_did_change_watched_files)
1140+
.on_sync_mut::<lsp_ext::CancelFlycheck>(handlers::handle_cancel_flycheck)
1141+
.on_sync_mut::<lsp_ext::ClearFlycheck>(handlers::handle_clear_flycheck)
1142+
.on_sync_mut::<lsp_ext::RunFlycheck>(handlers::handle_run_flycheck)
1143+
.on_sync_mut::<lsp_ext::AbortRunTest>(handlers::handle_abort_run_test)
11491144
.finish();
1150-
Ok(())
11511145
}
11521146
}

0 commit comments

Comments
 (0)