@@ -25,7 +25,10 @@ impl LanguageClient {
2525
2626 pub fn loop_call ( & self , rx : & crossbeam_channel:: Receiver < Call > ) -> Fallible < ( ) > {
2727 for call in rx. iter ( ) {
28- let language_client = Self ( self . 0 . clone ( ) ) ;
28+ let language_client = LanguageClient {
29+ state_mutex : self . state_mutex . clone ( ) ,
30+ clients_mutex : self . clients_mutex . clone ( ) , // not sure if useful to clone this
31+ } ;
2932 thread:: spawn ( move || {
3033 if let Err ( err) = language_client. handle_call ( call) {
3134 error ! ( "Error handling request:\n {:?}" , err) ;
@@ -2827,6 +2830,29 @@ impl LanguageClient {
28272830 let cmdparams = vim_cmd_args_to_value ( & cmdargs) ?;
28282831 let params = params. combine ( & cmdparams) ;
28292832
2833+ // When multiple buffers get opened up concurrently,
2834+ // startServer gets called concurrently.
2835+ // This lock ensures that at most one language server is starting up at a time per
2836+ // languageId.
2837+ // We keep the mutex in scope to satisfy the borrow checker.
2838+ // This ensures that the mutex isn't garbage collected while the MutexGuard is held.
2839+ //
2840+ // - e.g. prevents starting multiple servers with `vim -p`.
2841+ // - This continues to allow distinct language servers to start up concurrently
2842+ // by languageId (e.g. java and rust)
2843+ // - Revisit this when more than one server is allowed per languageId.
2844+ // (ensure that the mutex is acquired by what starts the group of servers)
2845+ //
2846+ // TODO: May want to lock other methods that update the list of clients.
2847+ let mutex_for_language_id = self . get_client_update_mutex ( Some ( languageId. clone ( ) ) ) ?;
2848+ let _raii_lock: MutexGuard < ( ) > = mutex_for_language_id. lock ( ) . map_err ( |err| {
2849+ format_err ! (
2850+ "Failed to lock client creation for languageId {:?}: {:?}" ,
2851+ languageId,
2852+ err
2853+ )
2854+ } ) ?;
2855+
28302856 if self . get ( |state| state. clients . contains_key ( & Some ( languageId. clone ( ) ) ) ) ? {
28312857 return Ok ( json ! ( { } ) ) ;
28322858 }
0 commit comments