55using System . Threading . Tasks ;
66using Newtonsoft . Json . Linq ;
77using Microsoft . Extensions . Logging ;
8- using Newtonsoft . Json ;
98using OmniSharp . Extensions . JsonRpc . Server ;
109using OmniSharp . Extensions . JsonRpc . Server . Messages ;
1110
@@ -72,7 +71,8 @@ private void ProcessInputStream()
7271 // content is encoded in UTF-8
7372 while ( true )
7473 {
75- try {
74+ try
75+ {
7676 if ( _inputThread == null ) return ;
7777
7878 var buffer = new byte [ 300 ] ;
@@ -182,8 +182,7 @@ private void HandleRequest(string request)
182182 _scheduler . Add (
183183 type ,
184184 item . Request . Method ,
185- async ( ) =>
186- {
185+ async ( ) => {
187186 try
188187 {
189188 var result = await _requestRouter . RouteRequest ( descriptor , item . Request , CancellationToken . None ) ;
@@ -202,26 +201,24 @@ private void HandleRequest(string request)
202201
203202 if ( item . IsNotification )
204203 {
204+
205205 var descriptor = _requestRouter . GetDescriptor ( item . Notification ) ;
206206 if ( descriptor is null ) continue ;
207+
208+ // We need to special case cancellation so that we can cancel any request that is currently in flight.
209+ if ( descriptor . Method == JsonRpcNames . CancelRequest )
210+ {
211+ var cancelParams = item . Notification . Params ? . ToObject < CancelParams > ( ) ;
212+ if ( cancelParams == null ) { continue ; }
213+ _requestRouter . CancelRequest ( cancelParams . Id ) ;
214+ continue ;
215+ }
216+
207217 var type = _requestProcessIdentifier . Identify ( descriptor ) ;
208218 _scheduler . Add (
209219 type ,
210220 item . Notification . Method ,
211- async ( ) =>
212- {
213- try
214- {
215- await _requestRouter . RouteNotification ( descriptor , item . Notification , CancellationToken . None ) ;
216- }
217- catch ( Exception e )
218- {
219- _logger . LogCritical ( Events . UnhandledNotification , e , "Unhandled exception executing notification {Method}" , item . Notification . Method ) ;
220- // TODO: Should we rethrow or swallow?
221- // If an exception happens... the whole system could be in a bad state, hence this throwing currently.
222- throw ;
223- }
224- }
221+ DoNotification ( descriptor , item . Notification )
225222 ) ;
226223 }
227224
@@ -231,6 +228,23 @@ private void HandleRequest(string request)
231228 _outputHandler . Send ( item . Error ) ;
232229 }
233230 }
231+
232+ Func < Task > DoNotification ( IHandlerDescriptor descriptor , Notification notification )
233+ {
234+ return async ( ) => {
235+ try
236+ {
237+ await _requestRouter . RouteNotification ( descriptor , notification , CancellationToken . None ) ;
238+ }
239+ catch ( Exception e )
240+ {
241+ _logger . LogCritical ( Events . UnhandledNotification , e , "Unhandled exception executing notification {Method}" , notification . Method ) ;
242+ // TODO: Should we rethrow or swallow?
243+ // If an exception happens... the whole system could be in a bad state, hence this throwing currently.
244+ throw ;
245+ }
246+ } ;
247+ }
234248 }
235249
236250
0 commit comments