JwtSecurityTokenHandler: Deadlock (starvation) when used on single threaded TaskScheduler
See original GitHub issueThe consumer loop EventBasedLRUCache.EventQueueTaskAction
is probably meant to be run as a separate blocking thread; however, the way is it started new Task(Action).Start()
does not do that, but uses TaskScheduler.Current
to run it.
Since the consumer loop is blocking, this means it stops a single threaded TaskScheduler
from all processing.
Perhaps the static Task.Run
, which would run the consumer on the thread pool, should be used?
Version: 6.12.2 Runtime: .NET 5 Repro:
static class Program
{
static void Main()
{
var scheduler = new ConcurrentExclusiveSchedulerPair().ExclusiveScheduler;
scheduler.Run(ValidateSomething);
Thread.Sleep(200);
scheduler.Run(() => { }).Wait();
}
private static Task Run(this TaskScheduler scheduler, Action action) =>
Task.Factory.StartNew(action, CancellationToken.None, TaskCreationOptions.DenyChildAttach, scheduler);
private static void ValidateSomething()
{
var handler = new JwtSecurityTokenHandler();
var tokenString = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c";
var securityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("AAAAAAAAAAAAAAAAAAAAAAAAAAA"));
var validationParameters = new TokenValidationParameters
{
RequireExpirationTime = false,
RequireSignedTokens = true,
ValidateAudience = false,
ValidateIssuer = false,
ValidateLifetime = false,
IssuerSigningKey = securityKey,
};
handler.ValidateToken(tokenString, validationParameters, out _);
}
}
Issue Analytics
- State:
- Created 2 years ago
- Comments:7 (5 by maintainers)
Top Results From Across the Web
Task.Run continues on the same thread causing deadlock
It means that is has AspNetSynchronizationContext as SyncronizationContext and will definitely deadlock. Here is debug output: *** (worked fine) ...
Read more >External tasks and grains - Orleans
Deadlocks. Since grains execute in a single-threaded fashion, it is possible to deadlock a grain by synchronously blocking in a way that would ......
Read more >A TaskScheduler that always run tasks in a specific thread
Here is an easy way to make all your tasks run exclusively on one thread for all tasks. And you can also run...
Read more >27. Task Execution and Scheduling
The interface has a single method execute(Runnable task) that accepts a task for ... This is typically used when you have a thread...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@vuplea i believe we fixed this, please reopen if we missed some cases.
Yes, that’s what I meant to write😅.
new Task(Action).Start()
, I’ve edited it in the original message.