@@ -291,7 +291,14 @@ public void Disconnect(bool flushOutgoing = false)
291291 ) ;
292292 }
293293
294- _cancellationSource ? . Cancel ( ) ;
294+ try
295+ {
296+ _cancellationSource ? . Cancel ( ) ;
297+ }
298+ catch ( AggregateException e ) when ( e . InnerException is ObjectDisposedException )
299+ {
300+ // Swallow object disposed exception
301+ }
295302 _sendLoop = null ;
296303 _receiveLoop = null ;
297304 _dispatchLoop = null ;
@@ -312,8 +319,7 @@ public void SendEmptyNotification(string method)
312319 if ( ! IsOpen )
313320 throw new LspException ( "Not connected to the language server." ) ;
314321
315- _outgoing . TryAdd ( new ClientMessage
316- {
322+ _outgoing . TryAdd ( new ClientMessage {
317323 // No Id means it's a notification.
318324 Method = method
319325 } ) ;
@@ -339,8 +345,7 @@ public void SendNotification(string method, object notification)
339345 if ( ! IsOpen )
340346 throw new LspException ( "Not connected to the language server." ) ;
341347
342- _outgoing . TryAdd ( new ClientMessage
343- {
348+ _outgoing . TryAdd ( new ClientMessage {
344349 // No Id means it's a notification.
345350 Method = method ,
346351 Params = JToken . FromObject ( notification , Serializer . JsonSerializer )
@@ -376,17 +381,15 @@ public void SendNotification(string method, object notification)
376381 string requestId = Interlocked . Increment ( ref _nextRequestId ) . ToString ( ) ;
377382
378383 var responseCompletion = new TaskCompletionSource < ServerMessage > ( state : requestId ) ;
379- cancellationToken . Register ( ( ) =>
380- {
384+ cancellationToken . Register ( ( ) => {
381385 responseCompletion . TrySetException (
382386 new OperationCanceledException ( "The request was canceled via the supplied cancellation token." , cancellationToken )
383387 ) ;
384388
385389 // Send notification telling server to cancel the request, if possible.
386390 if ( ! _outgoing . IsAddingCompleted )
387391 {
388- _outgoing . TryAdd ( new ClientMessage
389- {
392+ _outgoing . TryAdd ( new ClientMessage {
390393 Method = JsonRpcNames . CancelRequest ,
391394 Params = new JObject (
392395 new JProperty ( "id" , requestId )
@@ -397,8 +400,7 @@ public void SendNotification(string method, object notification)
397400
398401 _responseCompletions . TryAdd ( requestId , responseCompletion ) ;
399402
400- _outgoing . TryAdd ( new ClientMessage
401- {
403+ _outgoing . TryAdd ( new ClientMessage {
402404 Id = requestId ,
403405 Method = method ,
404406 Params = request != null ? JToken . FromObject ( request , Serializer . JsonSerializer ) : null
@@ -439,17 +441,15 @@ public void SendNotification(string method, object notification)
439441 string requestId = Interlocked . Increment ( ref _nextRequestId ) . ToString ( ) ;
440442
441443 var responseCompletion = new TaskCompletionSource < ServerMessage > ( state : requestId ) ;
442- cancellationToken . Register ( ( ) =>
443- {
444+ cancellationToken . Register ( ( ) => {
444445 responseCompletion . TrySetException (
445446 new OperationCanceledException ( "The request was canceled via the supplied cancellation token." , cancellationToken )
446447 ) ;
447448
448449 // Send notification telling server to cancel the request, if possible.
449450 if ( ! _outgoing . IsAddingCompleted )
450451 {
451- _outgoing . TryAdd ( new ClientMessage
452- {
452+ _outgoing . TryAdd ( new ClientMessage {
453453 Method = JsonRpcNames . CancelRequest ,
454454 Params = new JObject (
455455 new JProperty ( "id" , requestId )
@@ -460,8 +460,7 @@ public void SendNotification(string method, object notification)
460460
461461 _responseCompletions . TryAdd ( requestId , responseCompletion ) ;
462462
463- _outgoing . TryAdd ( new ClientMessage
464- {
463+ _outgoing . TryAdd ( new ClientMessage {
465464 Id = requestId ,
466465 Method = method ,
467466 Params = request != null ? JToken . FromObject ( request , Serializer . JsonSerializer ) : null
@@ -871,8 +870,7 @@ private void DispatchRequest(ServerMessage requestMessage)
871870 }
872871
873872#pragma warning disable CS4014 // Continuation does the work we need; no need to await it as this would tie up the dispatch loop.
874- handlerTask . ContinueWith ( _ =>
875- {
873+ handlerTask . ContinueWith ( _ => {
876874 if ( handlerTask . IsCanceled )
877875 Log . LogDebug ( "{RequestMethod} request {RequestId} canceled." , requestMessage . Method , requestId ) ;
878876 else if ( handlerTask . IsFaulted )
@@ -893,8 +891,7 @@ private void DispatchRequest(ServerMessage requestMessage)
893891 {
894892 Log . LogDebug ( "{RequestMethod} request {RequestId} complete (Result = {@Result})." , requestMessage . Method , requestId , handlerTask . Result ) ;
895893
896- _outgoing . TryAdd ( new ClientMessage
897- {
894+ _outgoing . TryAdd ( new ClientMessage {
898895 Id = requestMessage . Id ,
899896 Method = requestMessage . Method ,
900897 Result = handlerTask . Result != null ? JToken . FromObject ( handlerTask . Result , Serializer . JsonSerializer ) : null
@@ -962,8 +959,7 @@ void DispatchNotification(ServerMessage notificationMessage)
962959 handlerTask = _dispatcher . TryHandleEmptyNotification ( notificationMessage . Method ) ;
963960
964961#pragma warning disable CS4014 // Continuation does the work we need; no need to await it as this would tie up the dispatch loop.
965- handlerTask . ContinueWith ( completedHandler =>
966- {
962+ handlerTask . ContinueWith ( completedHandler => {
967963 if ( handlerTask . IsCanceled )
968964 Log . LogDebug ( "{NotificationMethod} notification canceled." , notificationMessage . Method ) ;
969965 else if ( handlerTask . IsFaulted )
@@ -1006,31 +1002,31 @@ static LspException CreateLspException(ServerMessage message)
10061002 switch ( message . Error . Code )
10071003 {
10081004 case LspErrorCodes . InvalidRequest :
1009- {
1010- return new LspInvalidRequestException ( requestId ) ;
1011- }
1005+ {
1006+ return new LspInvalidRequestException ( requestId ) ;
1007+ }
10121008 case LspErrorCodes . InvalidParameters :
1013- {
1014- return new LspInvalidParametersException ( requestId ) ;
1015- }
1009+ {
1010+ return new LspInvalidParametersException ( requestId ) ;
1011+ }
10161012 case LspErrorCodes . InternalError :
1017- {
1018- return new LspInternalErrorException ( requestId ) ;
1019- }
1013+ {
1014+ return new LspInternalErrorException ( requestId ) ;
1015+ }
10201016 case LspErrorCodes . MethodNotSupported :
1021- {
1022- return new LspMethodNotSupportedException ( requestId , message . Method ) ;
1023- }
1017+ {
1018+ return new LspMethodNotSupportedException ( requestId , message . Method ) ;
1019+ }
10241020 case LspErrorCodes . RequestCancelled :
1025- {
1026- return new LspRequestCancelledException ( requestId ) ;
1027- }
1021+ {
1022+ return new LspRequestCancelledException ( requestId ) ;
1023+ }
10281024 default :
1029- {
1030- string exceptionMessage = $ "Error processing request '{ message . Id } ' ({ message . Error . Code } ): { message . Error . Message } ";
1025+ {
1026+ string exceptionMessage = $ "Error processing request '{ message . Id } ' ({ message . Error . Code } ): { message . Error . Message } ";
10311027
1032- return new LspRequestException ( exceptionMessage , requestId , message . Error . Code ) ;
1033- }
1028+ return new LspRequestException ( exceptionMessage , requestId , message . Error . Code ) ;
1029+ }
10341030 }
10351031 }
10361032 }
0 commit comments