More Task and async gotchas! This code will dead lock.
Just a refresher, when you use a TaskCompletionSource, calling Set* or TrySet* will execute the continuation on that thread. Think of it like an event handler, you're triggering the event inline.
There's a new option that was introduced in .NET 4.6 and all of .NET Core that allows you to specify that the continuation should be dispatched to the thread pool.
When you use Task.WhenAll, it will continue execution based on the last task that completed. This means it can continue on some arbitrary thread.
In the above case, the second TaskCompletionSource didn't specify "RunContinuationsAsynchronously" and since it finishes last, WhenAll resumes when the second thread calls TrySetResult.
If you're calling TrySet* make sure you're either dispatching the calling code to the thread pool. If you're not doing this, make sure you know what the calling code can do and decide if that's ok. Ask yourself questions like "What happens if the caller blocks my thread?".

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with David Fowler 🇧🇧🇺🇸💉💉💉

David Fowler 🇧🇧🇺🇸💉💉💉 Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @davidfowl

Feb 12
I'm not sure about using redis as a queue, seems like blocking operations block the connection? Also, when using BROP to monitor multiple queues, you end up shuffling the queue names so the queue at the front of this list doesn't take priority (round robin?)
Or do you spin up multiple connections and balance the queues across them? It's a multiplexing problem (reminds me of HTTP/2).
I'm using System.Threading.Channels and Redis to expose a really nuget IAsyncEnumerable<T> implementation. Then using static interfaces to provide the RedisValue -> T.
Read 5 tweets
Feb 6
Lets talk about events. We want to write some code that reacts when something happens. This pattern exists in many domains from UI programming to IoT to serverless. What do the programming patterns for this look like today in #dotnet and #csharp?
We create a button (imagine this was referenced by a form or some UI component), then hook up an event handler and write to the console when a button click event happens. This is the one of the most popular and idiomatic event handling paradigms in .NET today.
It's existed for years and years in many different programming languages and domains. There may or may not be data associated with these events.
Read 18 tweets
Oct 16, 2021
Our hackathon project this year was working on making it easier to incrementally migrate large ASP.NET projects to https://t.co/4PmXMQN7SX Core. Here's the MVC music store app: #dotnet #aspnetcore Image
Most pages are running on .NET Framework but the shopping cart is managed by ASP.NET Core. This application is 10 years old and using both technologies at the same time, sharing source where possible. Image
This is made possible because both applications are running in the same IIS pipeline using different modules. It's sharing session and auth with the existing application without many changes to it. The ASP.NET Core application is part of the larger application.
Read 6 tweets
Oct 3, 2021
.NET has 4 built-in dictionary/map types:
- Hashtable
- Dictionary
- ConcurrentDictionary
- ImmutableDictionary

There’s no guidance on when to use what, mostly individual documentation on each implementation.

#dotnet
ConcurrentDictionary - Good read speed even in the face of concurrency, but it’s a heavyweight object to create and slower to update.

Dictionary with lock - Poor read speed lightweight to create and “medium” update speed.
Dictionary as immutable object - best read speed and lightweight to create but heavy update. Copy and modify on mutation e.g. new Dictionary(old).Add(key, value)
Read 7 tweets
Sep 30, 2021
Reflecting on my immigration journey this morning and for anyone else in the same situation it was:
F1/OPT 1 year (student visa) -> H1-B 5 years (work via) -> Green Card 7 years (permanent resident) -> Citizen yesterday. No more immigration anxiety 😅
Since being on a student visa I've always been worried about not being able to enter the US for some random reason or the other (plus being a black immigrant). Travelling with an excess amount of paperwork to prove you belong here.
When I joined Microsoft in 2008, there were huge layoffs and I was sure I was going to be in that set of people. Why not lay off the new employees (and immigrants at that)? It didn't happen and I was thankful. Microsoft sponsored my H1-B and Green card.
Read 4 tweets
Sep 27, 2021
If you are queuing background jobs/work items today in your .NET applications, how are you doing it and what are you using it for? #dotnet #aspnetcore
What about non-durable background work?
Is anyone using the DurableTasks for job scheduling? github.com/Azure/durablet…
Read 4 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us on Twitter!

:(