Base solution for your next web application
Open Closed

TaskCanceledException on JwtSecurityTokenHandler #9027


User avatar
0
Ricavir created

Hi,

I'm trying to clean some issues since updating my app to ASPNET CORE 3.1 I'm getting following exception when switching to another user account (or on user impersonation) : System.Threading.Tasks.TaskCanceledException : 'A task was canceled.'

Here is the full stack trace :

System.Threading.Tasks.TaskCanceledException HResult=0x8013153B Message=A task was canceled. Source=System.Private.CoreLib Arborescence des appels de procédure : at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.<ExecuteReaderAsync>d__17.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.<ExecuteReaderAsync>d__17.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.<ExecuteReaderAsync>d__17.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable1.AsyncEnumerator.<InitializeReaderAsync>d__18.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.<ExecuteAsync>d__72.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable1.AsyncEnumerator.<MoveNextAsync>d__17.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.ValueTaskAwaiter1.GetResult() at Microsoft.EntityFrameworkCore.Query.ShapedQueryCompilingExpressionVisitor.<SingleOrDefaultAsync>d__221.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at Microsoft.EntityFrameworkCore.Query.ShapedQueryCompilingExpressionVisitor.<SingleOrDefaultAsync>d__221.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter1.GetResult() at Abp.EntityFrameworkCore.Repositories.EfCoreRepositoryBase3.<FirstOrDefaultAsync>d__23.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Abp.Domain.Uow.UnitOfWorkInterceptor.<InternalInterceptAsynchronous>d__51.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Abp.Domain.Uow.UnitOfWorkInterceptor.<InternalInterceptAsynchronous>d__51.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Abp.Domain.Uow.UnitOfWorkInterceptor.<InternalInterceptAsynchronous>d__51.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter1.GetResult() at MyCompany.MyApp.Authorization.Users.UserManager.<GetUserOrNullAsync>d__4.MoveNext() in C:\Users\User\source\repos\MyApp_AspnetZero\aspnet-core\src\MyCompany.MyApp.Core\Authorization\Users\UserManager.cs:line 84 at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Abp.Domain.Uow.UnitOfWorkInterceptor.<InternalInterceptAsynchronous>d__51.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter1.GetResult() at MyCompany.MyApp.Authorization.Users.UserManager.<GetUserAsync>d__6.MoveNext() in C:\Users\User\source\repos\MyApp_AspnetZero\aspnet-core\src\MyCompany.MyApp.Core\Authorization\Users\UserManager.cs:line 95 at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter1.GetResult() at Nito.AsyncEx.Synchronous.TaskExtensions.WaitAndUnwrapException[TResult](Task1 task) at Nito.AsyncEx.AsyncContext.<>c__DisplayClass16_01.<Run>b__0(Task1 t) at System.Threading.Tasks.ContinuationResultTaskFromResultTask2.InnerInvoke() at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread) at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter1.GetResult() at Nito.AsyncEx.Synchronous.TaskExtensions.WaitAndUnwrapException[TResult](Task1 task) at Nito.AsyncEx.AsyncContext.Run[TResult](Func1 action) at Abp.Threading.AsyncHelper.RunSync[TResult](Func1 func) at MyCompany.MyApp.Authorization.Users.UserManager.GetUser(UserIdentifier userIdentifier) in C:\Users\User\source\repos\MyApp_AspnetZero\aspnet-core\src\MyCompany.MyApp.Core\Authorization\Users\UserManager.cs:line 106 at MyCompany.MyApp.Web.Authentication.JwtBearer.JwtSecurityStampHandler.<ValidateSecurityStampFromDb>d__10.MoveNext() in C:\Users\User\source\repos\MyApp_AspnetZero\aspnet-core\src\MyCompany.MyApp.Web.Core\Authentication\JwtBearer\JwtSecurityStampHandler.cs:line 85 at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter1.GetResult() at MyCompany.MyApp.Web.Authentication.JwtBearer.JwtSecurityStampHandler.<Validate>d__5.MoveNext() in C:\Users\User\source\repos\MyApp_AspnetZero\aspnet-core\src\MyCompany.MyApp.Web.Core\Authentication\JwtBearer\JwtSecurityStampHandler.cs:line 51 at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter1.GetResult() at MyCompany.MyApp.Web.Authentication.JwtBearer.MyAppJwtSecurityTokenHandler.<ValidateSecurityStampAsync>d__12.MoveNext() in C:\Users\User\source\repos\MyApp_AspnetZero\aspnet-core\src\MyCompany.MyApp.Web.Core\Authentication\JwtBearer\MyAppJwtSecurityTokenHandler.cs:line 110 at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Nito.AsyncEx.Synchronous.TaskExtensions.WaitAndUnwrapException(Task task) at Nito.AsyncEx.AsyncContext.<>c__DisplayClass15_0.<Run>b__0(Task t) at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread) at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Nito.AsyncEx.Synchronous.TaskExtensions.WaitAndUnwrapException(Task task) at Nito.AsyncEx.AsyncContext.Run(Func1 action) at MyCompany.MyApp.Web.Authentication.JwtBearer.MyAppJwtSecurityTokenHandler.ValidateToken(String securityToken, TokenValidationParameters validationParameters, SecurityToken& validatedToken) in C:\Users\User\source\repos\MyApp_AspnetZero\aspnet-core\src\MyCompany.MyApp.Web.Core\Authentication\JwtBearer\MyAppJwtSecurityTokenHandler.cs:line 48 at Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler.<HandleAuthenticateAsync>d__6.MoveNext()

Cette exception a été levée à l'origine dans cette pile des appels : [Code externe] MyCompany.MyApp.Authorization.Users.UserManager.GetUserOrNullAsync(Abp.UserIdentifier) dans UserManager.cs [Code externe] MyCompany.MyApp.Authorization.Users.UserManager.GetUserAsync(Abp.UserIdentifier) dans UserManager.cs [Code externe] MyCompany.MyApp.Authorization.Users.UserManager.GetUser(Abp.UserIdentifier) dans UserManager.cs MyCompany.MyApp.Web.Authentication.JwtBearer.JwtSecurityStampHandler.ValidateSecurityStampFromDb(Abp.UserIdentifier, string) dans JwtSecurityStampHandler.cs [Code externe] MyCompany.MyApp.Web.Authentication.JwtBearer.JwtSecurityStampHandler.Validate(System.Security.Claims.ClaimsPrincipal) dans JwtSecurityStampHandler.cs

I know that TaskCanceledException can occur in the logs due to SignalR. Do you know this one ? Any fix possible ?


5 Answer(s)
  • User Avatar
    1
    alexanderpilhar created

    Have a look at this one (might be the same): [exception when impersonating tenant user #8611](https://support.aspnetzero.com/QA/Questions/8611)

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @ricavir

    Are you using latest version of ABP Nuget packages ?

  • User Avatar
    0
    Ricavir created

    @ismcagdas : yes, Abp 5.7 @alexanderpilhar : thanks, I could fix it by adding suggested code here : https://support.aspnetzero.com/QA/Questions/8611#answer-9291fbcd-73ae-e4e4-b75b-39f3b1da856d

  • User Avatar
    0
    alexanderpilhar created

    @ricavir

    I don't think this is the solution to the problem ... It just replaces the real CancellationTokenProvider with the NullCancellationTokenProvider (which does nothing). I think @maliming just wanted me to test something there.

    What I did as a work around to silence the exceptions during debugging is the following:

    #if DEBUG
                try
                {
                    AsyncHelper.RunSync(() => ValidateSecurityStampAsync(principal));
                }
                catch
                {
                    // ignore
                }
    #else
                AsyncHelper.RunSync(() => ValidateSecurityStampAsync(principal));
    #endif
    
  • User Avatar
    0
    Ricavir created

    You are right, I just replaced it by a try/catch block to avoid logging these exceptions.