Logo Questions Linux Laravel Mysql Ubuntu Git Menu

Under what circumstances should an Async method support cancellation

I'm currently building an async enabled version of an existing API and I'm struggling to find any guidance on when it is a good idea to support cancellation. Some Async methods in the BCL do not have an overload which accepts a CancellationToken and I found this MSDN article which states

it is not essential for all async methods to support cancellation

So, what conditions would make it worth supporting cancellation via a CancellationToken?

I'm leaning towards the following conditions:

  • Any awaited method called also supports cancellation
  • Any awaited method may take longer than n ms to execute
  • The method implementation has one or more logical exit points (e.g. no side effects of quitting early)

Are those reasonable conditions? there any others?

like image 951
Trevor Pilley Avatar asked Mar 19 '23 02:03

Trevor Pilley

1 Answers

This is just my opinion, but I'd say that if the async methods you're calling all support cancellation, then yours should. In a similar vein, if you're creating an awaitable API over a naturally-asynchronous operation, then do your best to support cancellation (e.g., via CancellationToken.Register).

I'd also say that any (synchronous) CPU-bound method that could take a "long time" should observe a cancellation token periodically (CancellationToken.ThrowIfCancellationRequested). "Long time" is relative, but as a rough guideline I'd say anything over half a second (on older hardware, not our 8-core dev machines ;).

In any other scenario, you're talking about a much less useful form of cancellation - specifically, the cancellation may take an arbitrary amount of time to take effect. E.g., if some async methods support it but others don't. I'm not sure how useful a cancellation token parameter would be in that case; you might want to put it in, but be sure to document its limitations.

like image 76
Stephen Cleary Avatar answered Mar 21 '23 07:03

Stephen Cleary