From 77d82cd7e65959ee6486c3288a2beb8a090f6b5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Chalet?= Date: Sun, 4 Oct 2026 15:19:09 +0200 Subject: [PATCH] Update multiple code paths to rethrow OperationCanceledException --- ...enIddictClientSystemIntegrationHandlers.cs | 5 ++ ...penIddictClientSystemIntegrationHelpers.cs | 15 ++++++ ...dictClientSystemIntegrationHttpListener.cs | 7 +++ .../OpenIddictClientSystemNetHttpHandlers.cs | 10 ++++ .../OpenIddictClientDispatcher.cs | 12 ++++- .../OpenIddictClientHandlers.cs | 48 ++++++++++++++----- .../Managers/OpenIddictApplicationManager.cs | 9 +++- .../OpenIddictAuthorizationManager.cs | 5 ++ .../Managers/OpenIddictTokenManager.cs | 15 ++++++ .../OpenIddictServerDispatcher.cs | 12 ++++- .../OpenIddictServerHandlers.cs | 2 +- ...enIddictValidationSystemNetHttpHandlers.cs | 10 ++++ .../OpenIddictValidationDispatcher.cs | 12 ++++- .../OpenIddictValidationHandlers.cs | 8 +++- 14 files changed, 147 insertions(+), 23 deletions(-) diff --git a/src/OpenIddict.Client.SystemIntegration/OpenIddictClientSystemIntegrationHandlers.cs b/src/OpenIddict.Client.SystemIntegration/OpenIddictClientSystemIntegrationHandlers.cs index ac56bfa7..74156be4 100644 --- a/src/OpenIddict.Client.SystemIntegration/OpenIddictClientSystemIntegrationHandlers.cs +++ b/src/OpenIddict.Client.SystemIntegration/OpenIddictClientSystemIntegrationHandlers.cs @@ -1190,6 +1190,11 @@ public static partial class OpenIddictClientSystemIntegrationHandlers await _service.RedirectProtocolActivationAsync(activation, identifier, source.Token); } + catch (OperationCanceledException) when (context.CancellationToken.IsCancellationRequested) + { + throw; + } + catch (Exception exception) when (!OpenIddictHelpers.IsFatal(exception)) { context.Logger.LogWarning(6215, SR.GetResourceString(SR.ID6215), identifier); diff --git a/src/OpenIddict.Client.SystemIntegration/OpenIddictClientSystemIntegrationHelpers.cs b/src/OpenIddict.Client.SystemIntegration/OpenIddictClientSystemIntegrationHelpers.cs index 3214be16..df66de58 100644 --- a/src/OpenIddict.Client.SystemIntegration/OpenIddictClientSystemIntegrationHelpers.cs +++ b/src/OpenIddict.Client.SystemIntegration/OpenIddictClientSystemIntegrationHelpers.cs @@ -452,6 +452,11 @@ public static class OpenIddictClientSystemIntegrationHelpers return await UIApplication.SharedApplication.OpenUrlAsync(new NSUrl(uri.AbsoluteUri), new UIApplicationOpenUrlOptions()); } + catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested) + { + throw; + } + catch (Exception exception) when (!OpenIddictHelpers.IsFatal(exception)) { return false; @@ -487,6 +492,11 @@ public static class OpenIddictClientSystemIntegrationHelpers return true; } + catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested) + { + throw; + } + catch (Exception exception) when (!OpenIddictHelpers.IsFatal(exception)) { return false; @@ -521,6 +531,11 @@ public static class OpenIddictClientSystemIntegrationHelpers return true; } + catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested) + { + throw; + } + catch (Exception exception) when (!OpenIddictHelpers.IsFatal(exception)) { return false; diff --git a/src/OpenIddict.Client.SystemIntegration/OpenIddictClientSystemIntegrationHttpListener.cs b/src/OpenIddict.Client.SystemIntegration/OpenIddictClientSystemIntegrationHttpListener.cs index 8c623143..6a16c160 100644 --- a/src/OpenIddict.Client.SystemIntegration/OpenIddictClientSystemIntegrationHttpListener.cs +++ b/src/OpenIddict.Client.SystemIntegration/OpenIddictClientSystemIntegrationHttpListener.cs @@ -202,6 +202,13 @@ public sealed class OpenIddictClientSystemIntegrationHttpListener : BackgroundSe exceptions.Push(new InvalidOperationException(SR.FormatID0384(port), exception)); } + catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested) + { + listener.Close(); + + throw; + } + catch (Exception exception) when (!OpenIddictHelpers.IsFatal(exception)) { listener.Close(); diff --git a/src/OpenIddict.Client.SystemNetHttp/OpenIddictClientSystemNetHttpHandlers.cs b/src/OpenIddict.Client.SystemNetHttp/OpenIddictClientSystemNetHttpHandlers.cs index f47dc1b7..ce017923 100644 --- a/src/OpenIddict.Client.SystemNetHttp/OpenIddictClientSystemNetHttpHandlers.cs +++ b/src/OpenIddict.Client.SystemNetHttp/OpenIddictClientSystemNetHttpHandlers.cs @@ -477,6 +477,11 @@ public static partial class OpenIddictClientSystemNetHttpHandlers response = await client.SendAsync(request, HttpCompletionOption.ResponseContentRead, context.CancellationToken); } + catch (OperationCanceledException) when (context.CancellationToken.IsCancellationRequested) + { + throw; + } + // If an exception is thrown at this stage, this likely means a persistent network error occurred. // In this case, log the error details and return a generic error to stop processing the event. catch (Exception exception) when (!OpenIddictHelpers.IsFatal(exception)) @@ -723,6 +728,11 @@ public static partial class OpenIddictClientSystemNetHttpHandlers OpenIddictSerializer.Default.Response, context.CancellationToken); } + catch (OperationCanceledException) when (context.CancellationToken.IsCancellationRequested) + { + throw; + } + // If an exception is thrown at this stage, this likely means the returned response was not a valid // JSON response or was not correctly formatted as a JSON object. This typically happens when // a server error occurs while the JSON response is being generated and returned to the client. diff --git a/src/OpenIddict.Client/OpenIddictClientDispatcher.cs b/src/OpenIddict.Client/OpenIddictClientDispatcher.cs index 77a430c5..78c7f299 100644 --- a/src/OpenIddict.Client/OpenIddictClientDispatcher.cs +++ b/src/OpenIddict.Client/OpenIddictClientDispatcher.cs @@ -39,9 +39,17 @@ public sealed class OpenIddictClientDispatcher : IOpenIddictClientDispatcher await handler.HandleAsync(context); } - catch (Exception exception) when (!OpenIddictHelpers.IsFatal(exception) && context.Logger.IsEnabled(LogLevel.Debug)) + catch (OperationCanceledException) when (context.CancellationToken.IsCancellationRequested) { - context.Logger.LogDebug(6132, exception, SR.GetResourceString(SR.ID6132), handler.GetType().FullName, typeof(TContext).FullName); + throw; + } + + catch (Exception exception) when (!OpenIddictHelpers.IsFatal(exception)) + { + if (context.Logger.IsEnabled(LogLevel.Debug)) + { + context.Logger.LogDebug(6132, exception, SR.GetResourceString(SR.ID6132), handler.GetType().FullName, typeof(TContext).FullName); + } throw; } diff --git a/src/OpenIddict.Client/OpenIddictClientHandlers.cs b/src/OpenIddict.Client/OpenIddictClientHandlers.cs index 409dc5a1..31ae4502 100644 --- a/src/OpenIddict.Client/OpenIddictClientHandlers.cs +++ b/src/OpenIddict.Client/OpenIddictClientHandlers.cs @@ -530,8 +530,12 @@ public static partial class OpenIddictClientHandlers ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0140)); } - catch (Exception exception) when (!OpenIddictHelpers.IsFatal(exception) && - exception is not OperationCanceledException) + catch (OperationCanceledException) when (context.CancellationToken.IsCancellationRequested) + { + throw; + } + + catch (Exception exception) when (!OpenIddictHelpers.IsFatal(exception)) { context.Logger.LogError(6219, exception, SR.GetResourceString(SR.ID6219)); @@ -1149,8 +1153,12 @@ public static partial class OpenIddictClientHandlers ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0140)); } - catch (Exception exception) when (!OpenIddictHelpers.IsFatal(exception) && - exception is not OperationCanceledException) + catch (OperationCanceledException) when (context.CancellationToken.IsCancellationRequested) + { + throw; + } + + catch (Exception exception) when (!OpenIddictHelpers.IsFatal(exception)) { context.Logger.LogError(6219, exception, SR.GetResourceString(SR.ID6219)); @@ -5030,8 +5038,12 @@ public static partial class OpenIddictClientHandlers ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0140)); } - catch (Exception exception) when (!OpenIddictHelpers.IsFatal(exception) && - exception is not OperationCanceledException) + catch (OperationCanceledException) when (context.CancellationToken.IsCancellationRequested) + { + throw; + } + + catch (Exception exception) when (!OpenIddictHelpers.IsFatal(exception)) { context.Logger.LogError(6219, exception, SR.GetResourceString(SR.ID6219)); @@ -7443,8 +7455,12 @@ public static partial class OpenIddictClientHandlers ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0140)); } - catch (Exception exception) when (!OpenIddictHelpers.IsFatal(exception) && - exception is not OperationCanceledException) + catch (OperationCanceledException) when (context.CancellationToken.IsCancellationRequested) + { + throw; + } + + catch (Exception exception) when (!OpenIddictHelpers.IsFatal(exception)) { context.Logger.LogError(6219, exception, SR.GetResourceString(SR.ID6219)); @@ -8248,8 +8264,12 @@ public static partial class OpenIddictClientHandlers ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0140)); } - catch (Exception exception) when (!OpenIddictHelpers.IsFatal(exception) && - exception is not OperationCanceledException) + catch (OperationCanceledException) when (context.CancellationToken.IsCancellationRequested) + { + throw; + } + + catch (Exception exception) when (!OpenIddictHelpers.IsFatal(exception)) { context.Logger.LogError(6219, exception, SR.GetResourceString(SR.ID6219)); @@ -9031,8 +9051,12 @@ public static partial class OpenIddictClientHandlers ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0140)); } - catch (Exception exception) when (!OpenIddictHelpers.IsFatal(exception) && - exception is not OperationCanceledException) + catch (OperationCanceledException) when (context.CancellationToken.IsCancellationRequested) + { + throw; + } + + catch (Exception exception) when (!OpenIddictHelpers.IsFatal(exception)) { context.Logger.LogError(6219, exception, SR.GetResourceString(SR.ID6219)); diff --git a/src/OpenIddict.Core/Managers/OpenIddictApplicationManager.cs b/src/OpenIddict.Core/Managers/OpenIddictApplicationManager.cs index 2df50355..cd45d59a 100644 --- a/src/OpenIddict.Core/Managers/OpenIddictApplicationManager.cs +++ b/src/OpenIddict.Core/Managers/OpenIddictApplicationManager.cs @@ -1407,6 +1407,11 @@ public class OpenIddictApplicationManager : IOpenIddictApplication await UpdateAsync(application, secret, cancellationToken); } + catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested) + { + throw; + } + catch (Exception exception) when (!OpenIddictHelpers.IsFatal(exception)) { // If a non-fatal exception is thrown, ignore it: the client secret will be updated the next time it is validated again. @@ -1569,7 +1574,7 @@ public class OpenIddictApplicationManager : IOpenIddictApplication return false; } - catch (CryptographicException exception) when (!OpenIddictHelpers.IsFatal(exception)) + catch (CryptographicException exception) { Logger.LogWarning(6288, exception, SR.GetResourceString(SR.ID6288)); @@ -1714,7 +1719,7 @@ public class OpenIddictApplicationManager : IOpenIddictApplication return true; } - catch (CryptographicException exception) when (!OpenIddictHelpers.IsFatal(exception)) + catch (CryptographicException exception) { Logger.LogWarning(6288, exception, SR.GetResourceString(SR.ID6288)); diff --git a/src/OpenIddict.Core/Managers/OpenIddictAuthorizationManager.cs b/src/OpenIddict.Core/Managers/OpenIddictAuthorizationManager.cs index 1af5512c..55aed99a 100644 --- a/src/OpenIddict.Core/Managers/OpenIddictAuthorizationManager.cs +++ b/src/OpenIddict.Core/Managers/OpenIddictAuthorizationManager.cs @@ -774,6 +774,11 @@ public class OpenIddictAuthorizationManager : IOpenIddictAuthori return true; } + catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested) + { + throw; + } + catch (ConcurrencyException exception) { Logger.LogDebug(6165, exception, SR.GetResourceString(SR.ID6165), await Store.GetIdAsync(authorization, cancellationToken)); diff --git a/src/OpenIddict.Core/Managers/OpenIddictTokenManager.cs b/src/OpenIddict.Core/Managers/OpenIddictTokenManager.cs index 1e181c51..8b40a33d 100644 --- a/src/OpenIddict.Core/Managers/OpenIddictTokenManager.cs +++ b/src/OpenIddict.Core/Managers/OpenIddictTokenManager.cs @@ -972,6 +972,11 @@ public class OpenIddictTokenManager : IOpenIddictTokenManager where TTok return true; } + catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested) + { + throw; + } + catch (ConcurrencyException exception) { Logger.LogDebug(6169, exception, SR.GetResourceString(SR.ID6169), await Store.GetIdAsync(token, cancellationToken)); @@ -1008,6 +1013,11 @@ public class OpenIddictTokenManager : IOpenIddictTokenManager where TTok return true; } + catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested) + { + throw; + } + catch (ConcurrencyException exception) { Logger.LogDebug(6172, exception, SR.GetResourceString(SR.ID6172), await Store.GetIdAsync(token, cancellationToken)); @@ -1044,6 +1054,11 @@ public class OpenIddictTokenManager : IOpenIddictTokenManager where TTok return true; } + catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested) + { + throw; + } + catch (ConcurrencyException exception) { Logger.LogDebug(6175, exception, SR.GetResourceString(SR.ID6175), await Store.GetIdAsync(token, cancellationToken)); diff --git a/src/OpenIddict.Server/OpenIddictServerDispatcher.cs b/src/OpenIddict.Server/OpenIddictServerDispatcher.cs index be8884a0..15fe8600 100644 --- a/src/OpenIddict.Server/OpenIddictServerDispatcher.cs +++ b/src/OpenIddict.Server/OpenIddictServerDispatcher.cs @@ -39,9 +39,17 @@ public sealed class OpenIddictServerDispatcher : IOpenIddictServerDispatcher await handler.HandleAsync(context); } - catch (Exception exception) when (!OpenIddictHelpers.IsFatal(exception) && context.Logger.IsEnabled(LogLevel.Debug)) + catch (OperationCanceledException) when (context.CancellationToken.IsCancellationRequested) { - context.Logger.LogDebug(6132, exception, SR.GetResourceString(SR.ID6132), handler.GetType().FullName, typeof(TContext).FullName); + throw; + } + + catch (Exception exception) when (!OpenIddictHelpers.IsFatal(exception)) + { + if (context.Logger.IsEnabled(LogLevel.Debug)) + { + context.Logger.LogDebug(6132, exception, SR.GetResourceString(SR.ID6132), handler.GetType().FullName, typeof(TContext).FullName); + } throw; } diff --git a/src/OpenIddict.Server/OpenIddictServerHandlers.cs b/src/OpenIddict.Server/OpenIddictServerHandlers.cs index afd7e2e8..33fa1e06 100644 --- a/src/OpenIddict.Server/OpenIddictServerHandlers.cs +++ b/src/OpenIddict.Server/OpenIddictServerHandlers.cs @@ -1289,7 +1289,7 @@ public static partial class OpenIddictServerHandlers } } - catch (CryptographicException exception) when (!OpenIddictHelpers.IsFatal(exception)) + catch (CryptographicException exception) { context.Logger.LogWarning(6288, exception, SR.GetResourceString(SR.ID6288)); diff --git a/src/OpenIddict.Validation.SystemNetHttp/OpenIddictValidationSystemNetHttpHandlers.cs b/src/OpenIddict.Validation.SystemNetHttp/OpenIddictValidationSystemNetHttpHandlers.cs index f3ee4a9f..bd5fe405 100644 --- a/src/OpenIddict.Validation.SystemNetHttp/OpenIddictValidationSystemNetHttpHandlers.cs +++ b/src/OpenIddict.Validation.SystemNetHttp/OpenIddictValidationSystemNetHttpHandlers.cs @@ -473,6 +473,11 @@ public static partial class OpenIddictValidationSystemNetHttpHandlers response = await client.SendAsync(request, HttpCompletionOption.ResponseContentRead, context.CancellationToken); } + catch (OperationCanceledException) when (context.CancellationToken.IsCancellationRequested) + { + throw; + } + // If an exception is thrown at this stage, this likely means a persistent network error occurred. // In this case, log the error details and return a generic error to stop processing the event. catch (Exception exception) when (!OpenIddictHelpers.IsFatal(exception)) @@ -719,6 +724,11 @@ public static partial class OpenIddictValidationSystemNetHttpHandlers OpenIddictSerializer.Default.Response, context.CancellationToken); } + catch (OperationCanceledException) when (context.CancellationToken.IsCancellationRequested) + { + throw; + } + // If an exception is thrown at this stage, this likely means the returned response was not a valid // JSON response or was not correctly formatted as a JSON object. This typically happens when // a server error occurs while the JSON response is being generated and returned to the client. diff --git a/src/OpenIddict.Validation/OpenIddictValidationDispatcher.cs b/src/OpenIddict.Validation/OpenIddictValidationDispatcher.cs index 3cc2012c..4b4b9bf1 100644 --- a/src/OpenIddict.Validation/OpenIddictValidationDispatcher.cs +++ b/src/OpenIddict.Validation/OpenIddictValidationDispatcher.cs @@ -39,9 +39,17 @@ public sealed class OpenIddictValidationDispatcher : IOpenIddictValidationDispat await handler.HandleAsync(context); } - catch (Exception exception) when (!OpenIddictHelpers.IsFatal(exception) && context.Logger.IsEnabled(LogLevel.Debug)) + catch (OperationCanceledException) when (context.CancellationToken.IsCancellationRequested) { - context.Logger.LogDebug(6132, exception, SR.GetResourceString(SR.ID6132), handler.GetType().FullName, typeof(TContext).FullName); + throw; + } + + catch (Exception exception) when (!OpenIddictHelpers.IsFatal(exception)) + { + if (context.Logger.IsEnabled(LogLevel.Debug)) + { + context.Logger.LogDebug(6132, exception, SR.GetResourceString(SR.ID6132), handler.GetType().FullName, typeof(TContext).FullName); + } throw; } diff --git a/src/OpenIddict.Validation/OpenIddictValidationHandlers.cs b/src/OpenIddict.Validation/OpenIddictValidationHandlers.cs index 5bb6c325..659dc3af 100644 --- a/src/OpenIddict.Validation/OpenIddictValidationHandlers.cs +++ b/src/OpenIddict.Validation/OpenIddictValidationHandlers.cs @@ -172,8 +172,12 @@ public static partial class OpenIddictValidationHandlers ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0140)); } - catch (Exception exception) when (!OpenIddictHelpers.IsFatal(exception) && - exception is not OperationCanceledException) + catch (OperationCanceledException) when (context.CancellationToken.IsCancellationRequested) + { + throw; + } + + catch (Exception exception) when (!OpenIddictHelpers.IsFatal(exception)) { context.Logger.LogError(6219, exception, SR.GetResourceString(SR.ID6219));