@@ -291,7 +291,14 @@ public void Disconnect(bool flushOutgoing = false)
291
291
) ;
292
292
}
293
293
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
+ }
295
302
_sendLoop = null ;
296
303
_receiveLoop = null ;
297
304
_dispatchLoop = null ;
@@ -312,8 +319,7 @@ public void SendEmptyNotification(string method)
312
319
if ( ! IsOpen )
313
320
throw new LspException ( "Not connected to the language server." ) ;
314
321
315
- _outgoing . TryAdd ( new ClientMessage
316
- {
322
+ _outgoing . TryAdd ( new ClientMessage {
317
323
// No Id means it's a notification.
318
324
Method = method
319
325
} ) ;
@@ -339,8 +345,7 @@ public void SendNotification(string method, object notification)
339
345
if ( ! IsOpen )
340
346
throw new LspException ( "Not connected to the language server." ) ;
341
347
342
- _outgoing . TryAdd ( new ClientMessage
343
- {
348
+ _outgoing . TryAdd ( new ClientMessage {
344
349
// No Id means it's a notification.
345
350
Method = method ,
346
351
Params = JToken . FromObject ( notification , Serializer . JsonSerializer )
@@ -376,17 +381,15 @@ public void SendNotification(string method, object notification)
376
381
string requestId = Interlocked . Increment ( ref _nextRequestId ) . ToString ( ) ;
377
382
378
383
var responseCompletion = new TaskCompletionSource < ServerMessage > ( state : requestId ) ;
379
- cancellationToken . Register ( ( ) =>
380
- {
384
+ cancellationToken . Register ( ( ) => {
381
385
responseCompletion . TrySetException (
382
386
new OperationCanceledException ( "The request was canceled via the supplied cancellation token." , cancellationToken )
383
387
) ;
384
388
385
389
// Send notification telling server to cancel the request, if possible.
386
390
if ( ! _outgoing . IsAddingCompleted )
387
391
{
388
- _outgoing . TryAdd ( new ClientMessage
389
- {
392
+ _outgoing . TryAdd ( new ClientMessage {
390
393
Method = JsonRpcNames . CancelRequest ,
391
394
Params = new JObject (
392
395
new JProperty ( "id" , requestId )
@@ -397,8 +400,7 @@ public void SendNotification(string method, object notification)
397
400
398
401
_responseCompletions . TryAdd ( requestId , responseCompletion ) ;
399
402
400
- _outgoing . TryAdd ( new ClientMessage
401
- {
403
+ _outgoing . TryAdd ( new ClientMessage {
402
404
Id = requestId ,
403
405
Method = method ,
404
406
Params = request != null ? JToken . FromObject ( request , Serializer . JsonSerializer ) : null
@@ -439,17 +441,15 @@ public void SendNotification(string method, object notification)
439
441
string requestId = Interlocked . Increment ( ref _nextRequestId ) . ToString ( ) ;
440
442
441
443
var responseCompletion = new TaskCompletionSource < ServerMessage > ( state : requestId ) ;
442
- cancellationToken . Register ( ( ) =>
443
- {
444
+ cancellationToken . Register ( ( ) => {
444
445
responseCompletion . TrySetException (
445
446
new OperationCanceledException ( "The request was canceled via the supplied cancellation token." , cancellationToken )
446
447
) ;
447
448
448
449
// Send notification telling server to cancel the request, if possible.
449
450
if ( ! _outgoing . IsAddingCompleted )
450
451
{
451
- _outgoing . TryAdd ( new ClientMessage
452
- {
452
+ _outgoing . TryAdd ( new ClientMessage {
453
453
Method = JsonRpcNames . CancelRequest ,
454
454
Params = new JObject (
455
455
new JProperty ( "id" , requestId )
@@ -460,8 +460,7 @@ public void SendNotification(string method, object notification)
460
460
461
461
_responseCompletions . TryAdd ( requestId , responseCompletion ) ;
462
462
463
- _outgoing . TryAdd ( new ClientMessage
464
- {
463
+ _outgoing . TryAdd ( new ClientMessage {
465
464
Id = requestId ,
466
465
Method = method ,
467
466
Params = request != null ? JToken . FromObject ( request , Serializer . JsonSerializer ) : null
@@ -871,8 +870,7 @@ private void DispatchRequest(ServerMessage requestMessage)
871
870
}
872
871
873
872
#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 ( _ => {
876
874
if ( handlerTask . IsCanceled )
877
875
Log . LogDebug ( "{RequestMethod} request {RequestId} canceled." , requestMessage . Method , requestId ) ;
878
876
else if ( handlerTask . IsFaulted )
@@ -893,8 +891,7 @@ private void DispatchRequest(ServerMessage requestMessage)
893
891
{
894
892
Log . LogDebug ( "{RequestMethod} request {RequestId} complete (Result = {@Result})." , requestMessage . Method , requestId , handlerTask . Result ) ;
895
893
896
- _outgoing . TryAdd ( new ClientMessage
897
- {
894
+ _outgoing . TryAdd ( new ClientMessage {
898
895
Id = requestMessage . Id ,
899
896
Method = requestMessage . Method ,
900
897
Result = handlerTask . Result != null ? JToken . FromObject ( handlerTask . Result , Serializer . JsonSerializer ) : null
@@ -962,8 +959,7 @@ void DispatchNotification(ServerMessage notificationMessage)
962
959
handlerTask = _dispatcher . TryHandleEmptyNotification ( notificationMessage . Method ) ;
963
960
964
961
#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 => {
967
963
if ( handlerTask . IsCanceled )
968
964
Log . LogDebug ( "{NotificationMethod} notification canceled." , notificationMessage . Method ) ;
969
965
else if ( handlerTask . IsFaulted )
@@ -1006,31 +1002,31 @@ static LspException CreateLspException(ServerMessage message)
1006
1002
switch ( message . Error . Code )
1007
1003
{
1008
1004
case LspErrorCodes . InvalidRequest :
1009
- {
1010
- return new LspInvalidRequestException ( requestId ) ;
1011
- }
1005
+ {
1006
+ return new LspInvalidRequestException ( requestId ) ;
1007
+ }
1012
1008
case LspErrorCodes . InvalidParameters :
1013
- {
1014
- return new LspInvalidParametersException ( requestId ) ;
1015
- }
1009
+ {
1010
+ return new LspInvalidParametersException ( requestId ) ;
1011
+ }
1016
1012
case LspErrorCodes . InternalError :
1017
- {
1018
- return new LspInternalErrorException ( requestId ) ;
1019
- }
1013
+ {
1014
+ return new LspInternalErrorException ( requestId ) ;
1015
+ }
1020
1016
case LspErrorCodes . MethodNotSupported :
1021
- {
1022
- return new LspMethodNotSupportedException ( requestId , message . Method ) ;
1023
- }
1017
+ {
1018
+ return new LspMethodNotSupportedException ( requestId , message . Method ) ;
1019
+ }
1024
1020
case LspErrorCodes . RequestCancelled :
1025
- {
1026
- return new LspRequestCancelledException ( requestId ) ;
1027
- }
1021
+ {
1022
+ return new LspRequestCancelledException ( requestId ) ;
1023
+ }
1028
1024
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 } ";
1031
1027
1032
- return new LspRequestException ( exceptionMessage , requestId , message . Error . Code ) ;
1033
- }
1028
+ return new LspRequestException ( exceptionMessage , requestId , message . Error . Code ) ;
1029
+ }
1034
1030
}
1035
1031
}
1036
1032
}
0 commit comments