Skip to main content

AsyncEnumerable in C#: The importance of EnumeratorCancellation attribute

Modern applications often need to process large datasets or streams of data asynchronously. When we need to iterate through such data without loading everything at once, we've traditionally used IEnumerable. But what if our data access is inherently asynchronous? Enter IAsyncEnumerable<T>, introduced in C# 8.0 and .NET Core 3.0, designed specifically for asynchronous streaming scenarios.

In this post, we'll explore IAsyncEnumerable<T> and why the EnumeratorCancellation attribute with a CancellationToken is crucial for writing robust, cancellable asynchronous code.

What is IAsyncEnumerable<T>?

IAsyncEnumerable<T> is an interface that represents a sequence of elements that can be asynchronously enumerated. It's the asynchronous counterpart to the familiar IEnumerable<T> interface.

The beauty of IAsyncEnumerable<T> is that it allows you to:

  • Perform asynchronous operations while iterating through a sequence
  • Yield results as they become available
  • Handle cancellation gracefully

The need for cancellation

Why do we need cancellation support in asynchronous operations? Consider these scenarios:

  1. A user navigates away from a web page that's fetching data
  2. A search operation is taking too long, and the user cancels it
  3. A service is shutting down while operations are in progress

Without proper cancellation support, resources can be wasted, and applications may become unresponsive or leak resources.

The EnumeratorCancellation attribute

The EnumeratorCancellation attribute is where the magic happens. When applied to a CancellationToken parameter in an async iterator method, it tells the compiler to pass this token to the GetAsyncEnumerator method when the iterator is being consumed with await foreach.

Let's look at a simple example:

Why is the EnumeratorCancellation attribute important?

Without the EnumeratorCancellation attribute, there's a disconnect between the cancellation token passed to the async iterator method and the one passed to GetAsyncEnumerator when consuming the enumerable. The EnumeratorCancellation attribute ensures that this token flows into your async iterator method, allowing you to react to cancellation requests from the consumer.

To help you avoid this mistake, the compiler issues a  CS8425 warning as a helpful reminder to properly implement cancellation in your async iterator methods.

The warning message typically looks like this:

warning CS8425: Async-iterator method 'MyMethod' has parameters that are declared with 'CancellationToken' but are not decorated with the 'EnumeratorCancellation' attribute, and will not be passed the cancellation token from 'WithCancellation' method calls

This warning occurs when you define an async iterator method that returns IAsyncEnumerable<T> and includes a CancellationToken parameter, but you haven't decorated that parameter with the [EnumeratorCancellation] attribute.

Consider this code that would trigger CS8425:

When you use this method with WithCancellation:

The cancellation token from WithCancellation won't be passed to your method's token parameter, making cancellation ineffective.

The solution is straightforward—add the [EnumeratorCancellation] attribute to your CancellationToken parameter:

Ignoring this warning can lead to subtle bugs where cancellation appears to be implemented but doesn't actually work. Your code will compile and run, but cancellation requests from consumers will be ignored, potentially leading to:

  • Resource leaks
  • Unresponsive applications
  • Operations that continue running long after the user has requested cancellation

Conclusion

IAsyncEnumerable<T> bridges an important gap in .NET's asynchronous programming model. The EnumeratorCancellation attribute is a critical component that ensures cancellation requests flow correctly through your code, allowing for responsive, resource-efficient applications.

When working with asynchronous streams, always remember to:

  • Use the EnumeratorCancellation attribute for your cancellation token parameters
  • Check for cancellation regularly
  • Propagate the token to inner operations
  • Pay attention to compiler warning CS8425 as it indicates a potential cancellation issue

By following these practices, you'll create asynchronous streaming code that's both powerful and robust.

Popular posts from this blog

Podman– Command execution failed with exit code 125

After updating WSL on one of the developer machines, Podman failed to work. When we took a look through Podman Desktop, we noticed that Podman had stopped running and returned the following error message: Error: Command execution failed with exit code 125 Here are the steps we tried to fix the issue: We started by running podman info to get some extra details on what could be wrong: >podman info OS: windows/amd64 provider: wsl version: 5.3.1 Cannot connect to Podman. Please verify your connection to the Linux system using `podman system connection list`, or try `podman machine init` and `podman machine start` to manage a new Linux VM Error: unable to connect to Podman socket: failed to connect: dial tcp 127.0.0.1:2655: connectex: No connection could be made because the target machine actively refused it. That makes sense as the podman VM was not running. Let’s check the VM: >podman machine list NAME         ...

Cache stampede: when our cache turned against us

While investigating some performance issues, we ran into an ASP.NET Core API that cached a fairly expensive aggregation query for 60 seconds. Under normal load, that was fine: one request rebuilds the cache, everyone else reads from it. Under peak load, dozens of requests would arrive in that same expiry window, all see a cache miss, and all fire the same expensive query in parallel. The database didn't like that. That was the moment when our caching layer stopped helping and started hurting. A burst of requests comes in at the same time, all miss the cache, and all go hammer the database or the downstream API at once. That's a cache stampede . The cache was supposed to protect our backend, and for a few hundred milliseconds it did the opposite. Why this happens IMemoryCache.GetOrCreate (and its async sibling) looks like it protects you, but it doesn't add any locking on its own. Look at the naive version: public async Task<Report> GetReportAsync(string key) ...

Azure DevOps/ GitHub emoji

I’m really bad at remembering emoji’s. So here is cheat sheet with all emoji’s that can be used in tools that support the github emoji markdown markup: All credits go to rcaviers who created this list.