|
11 | 11 | using System.IO; |
12 | 12 | using System.Linq; |
13 | 13 | using System.Reflection; |
| 14 | +using System.Runtime.CompilerServices; |
14 | 15 | using System.Text.Json; |
15 | 16 | using System.Threading; |
16 | 17 | using System.Threading.Tasks; |
@@ -41,6 +42,8 @@ internal sealed class ExtensionMessageHandlerService( |
41 | 42 | IExtensionMessageHandlerFactory customMessageHandlerFactory) |
42 | 43 | : IExtensionMessageHandlerService |
43 | 44 | { |
| 45 | + private static readonly ConditionalWeakTable<IExtensionMessageHandlerWrapper, IExtensionMessageHandlerWrapper> s_disabledExtensionHandlers = new(); |
| 46 | + |
44 | 47 | private readonly SolutionServices _solutionServices = solutionServices; |
45 | 48 | private readonly IExtensionMessageHandlerFactory _customMessageHandlerFactory = customMessageHandlerFactory; |
46 | 49 |
|
@@ -226,19 +229,28 @@ private async ValueTask<string> HandleExtensionMessageInCurrentProcessAsync<TArg |
226 | 229 | throw new InvalidOperationException($"Multiple handlers found for message {messageName}."); |
227 | 230 |
|
228 | 231 | var handler = handlers[0]; |
| 232 | + if (s_disabledExtensionHandlers.TryGetValue(handler, out _)) |
| 233 | + throw new InvalidOperationException($"Handler was disabled due to previous exception."); |
229 | 234 |
|
230 | 235 | try |
231 | 236 | { |
232 | 237 | var message = JsonSerializer.Deserialize(jsonMessage, handler.MessageType); |
233 | 238 | var result = await handler.ExecuteAsync(message, executeArgument, cancellationToken).ConfigureAwait(false); |
234 | 239 | return JsonSerializer.Serialize(result, handler.ResponseType); |
235 | 240 | } |
236 | | - catch |
| 241 | + catch (Exception ex) when (DisableHandlerAndPropagate(ex)) |
237 | 242 | { |
238 | | - // Any exception thrown in this method is left to bubble up to the extension. |
239 | | - // But we unregister all handlers from that assembly to minimize the impact of a bad extension. |
240 | | - await UnregisterExtensionAsync(assemblyFilePath: handler.ExtensionIdentifier, cancellationToken).ConfigureAwait(false); |
241 | | - throw; |
| 243 | + throw ExceptionUtilities.Unreachable(); |
| 244 | + } |
| 245 | + |
| 246 | + bool DisableHandlerAndPropagate(Exception ex) |
| 247 | + { |
| 248 | + FatalError.ReportNonFatalError(ex, ErrorSeverity.Critical); |
| 249 | + |
| 250 | + // Any exception thrown in this method is left to bubble up to the extension. But we unregister this handler |
| 251 | + // from that assembly to minimize the impact. |
| 252 | + s_disabledExtensionHandlers.TryAdd(handler, handler); |
| 253 | + return false; |
242 | 254 | } |
243 | 255 | } |
244 | 256 |
|
|
0 commit comments