question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

JwtSecurityTokenHandler: Deadlock (starvation) when used on single threaded TaskScheduler

See original GitHub issue

The 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:closed
  • Created 2 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
brentschmaltzcommented, Sep 13, 2021

@vuplea i believe we fixed this, please reopen if we missed some cases.

1reaction
vupleacommented, Aug 24, 2021

Yes, that’s what I meant to write😅. new Task(Action).Start(), I’ve edited it in the original message.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found