@@ -28,23 +28,28 @@ pub(super) fn request(req: server::Request) -> Task {
2828 let id = req. id . clone ( ) ;
2929
3030 match req. method . as_str ( ) {
31- requests:: DocumentDiagnosticRequestHandler :: METHOD => background_request_task :: <
31+ requests:: DocumentDiagnosticRequestHandler :: METHOD => background_document_request_task :: <
3232 requests:: DocumentDiagnosticRequestHandler ,
3333 > (
3434 req, BackgroundSchedule :: Worker
3535 ) ,
36- requests:: GotoTypeDefinitionRequestHandler :: METHOD => background_request_task :: <
36+ requests:: WorkspaceDiagnosticRequestHandler :: METHOD => background_request_task :: <
37+ requests:: WorkspaceDiagnosticRequestHandler ,
38+ > (
39+ req, BackgroundSchedule :: Worker
40+ ) ,
41+ requests:: GotoTypeDefinitionRequestHandler :: METHOD => background_document_request_task :: <
3742 requests:: GotoTypeDefinitionRequestHandler ,
3843 > (
3944 req, BackgroundSchedule :: Worker
4045 ) ,
41- requests:: HoverRequestHandler :: METHOD => background_request_task :: <
46+ requests:: HoverRequestHandler :: METHOD => background_document_request_task :: <
4247 requests:: HoverRequestHandler ,
4348 > ( req, BackgroundSchedule :: Worker ) ,
44- requests:: InlayHintRequestHandler :: METHOD => background_request_task :: <
49+ requests:: InlayHintRequestHandler :: METHOD => background_document_request_task :: <
4550 requests:: InlayHintRequestHandler ,
4651 > ( req, BackgroundSchedule :: Worker ) ,
47- requests:: CompletionRequestHandler :: METHOD => background_request_task :: <
52+ requests:: CompletionRequestHandler :: METHOD => background_document_request_task :: <
4853 requests:: CompletionRequestHandler ,
4954 > (
5055 req, BackgroundSchedule :: LatencySensitive
@@ -135,7 +140,51 @@ where
135140 } ) )
136141}
137142
138- fn background_request_task < R : traits:: BackgroundDocumentRequestHandler > (
143+ fn background_request_task < R : traits:: BackgroundRequestHandler > (
144+ req : server:: Request ,
145+ schedule : BackgroundSchedule ,
146+ ) -> Result < Task >
147+ where
148+ <<R as RequestHandler >:: RequestType as Request >:: Params : UnwindSafe ,
149+ {
150+ let retry = R :: RETRY_ON_CANCELLATION . then ( || req. clone ( ) ) ;
151+ let ( id, params) = cast_request :: < R > ( req) ?;
152+
153+ Ok ( Task :: background ( schedule, move |session : & Session | {
154+ let cancellation_token = session
155+ . request_queue ( )
156+ . incoming ( )
157+ . cancellation_token ( & id)
158+ . expect ( "request should have been tested for cancellation before scheduling" ) ;
159+
160+ let snapshot = session. take_workspace_snapshot ( ) ;
161+
162+ Box :: new ( move |client| {
163+ let _span = tracing:: debug_span!( "request" , %id, method = R :: METHOD ) . entered ( ) ;
164+
165+ // Test again if the request was cancelled since it was scheduled on the background task
166+ // and, if so, return early
167+ if cancellation_token. is_cancelled ( ) {
168+ tracing:: trace!(
169+ "Ignoring request id={id} method={} because it was cancelled" ,
170+ R :: METHOD
171+ ) ;
172+
173+ // We don't need to send a response here because the `cancel` notification
174+ // handler already responded with a message.
175+ return ;
176+ }
177+
178+ let result = ruff_db:: panic:: catch_unwind ( || R :: run ( snapshot, client, params) ) ;
179+
180+ if let Some ( response) = request_result_to_response :: < R > ( & id, client, result, retry) {
181+ respond :: < R > ( & id, response, client) ;
182+ }
183+ } )
184+ } ) )
185+ }
186+
187+ fn background_document_request_task < R : traits:: BackgroundDocumentRequestHandler > (
139188 req : server:: Request ,
140189 schedule : BackgroundSchedule ,
141190) -> Result < Task >
@@ -168,7 +217,7 @@ where
168217 } ;
169218
170219 let Some ( snapshot) = session. take_snapshot ( url) else {
171- tracing:: warn!( "Ignoring request because snapshot for path `{path:?}` doesn't exist. " ) ;
220+ tracing:: warn!( "Ignoring request because snapshot for path `{path:?}` doesn't exist" ) ;
172221 return Box :: new ( |_| { } ) ;
173222 } ;
174223
@@ -209,7 +258,7 @@ fn request_result_to_response<R>(
209258 request : Option < lsp_server:: Request > ,
210259) -> Option < Result < <<R as RequestHandler >:: RequestType as Request >:: Result > >
211260where
212- R : traits:: BackgroundDocumentRequestHandler ,
261+ R : traits:: RetriableRequestHandler ,
213262{
214263 match result {
215264 Ok ( response) => Some ( response) ,
0 commit comments