Consider a type that will print out a message when it’s finalized, and that has a Dispose method which will suppress finalization:

class DisplayOnFinalize : IDisposable {     public void Dispose() { GC.SuppressFinalize(this); }     ~DisplayOnFinalize() { Console.WriteLine(“Finalized”); } }

Now consider a simple usage of this class:

void Foo() {     var tcs = new TaskCompletionSource<bool>();     using(new DisplayOnFinalize())     {         tcs.Task.Wait();     } }

This method instantiates an instance of the finalizable class, and then blocks waiting for a task to be completed prior to disposing of the DisplayOnFinalize instance.  The task on which this code waits will never complete, and thus the calling thread will remain blocked at this location.  That thread’s stack will maintain a reference to the DisplayOnFinalize class, since if the wait were to complete the thread would need to invoke that instance’s Dispose method.  And as such, the message will never be printed out.

Now, consider a small variation:

async void Foo() {     var tcs = new TaskCompletionSource<bool>();     using(new DisplayOnFinalize())     {         await tcs.Task;     } }

The only differences here are that I’ve added the async keyword to the signature of my method, and I’m now asynchronously waiting for the task to complete (via the await keyword) rather than synchronously waiting for it to complete (via the Wait method).  However, this has a significant effect on behavior.  Of course, there’s the important difference that you’d expect, that we’re asynchronously waiting for the task and thus we’re not blocking the calling thread while waiting (forever) for the task to complete.  However, there’s a more subtle but nevertheless significant difference here… if you were to call this method repeatedly, you’d start to see “Finalized” getting printed out as the DisplayOnFinalize instances got garbage collected and finalized.

The async/await keywords tell the C#/Visual Basic compiler to rewrite your async method into a state machine, where the code that comes after the await is logically part of a continuation hooked up to the task (or, in general, the awaitable) being awaited.  The following isn’t exactly how the transformation happens, but you can think of the previous example as logically translating into something like the following (for the sake of this post, I’m leaving out lots of otherwise important details):

void Foo() {     var tcs = new TaskCompletionSource<bool>();     var d = new DisplayOnFinalize();     tcs.Task.ContinueWith(delegate     {         d.Dispose();     }); }

The code that comes after the await is in effect hooked up as a continuation, and in this case that code is the code to dispose of the DisplayOnFinalize instance.  Now, when the call to Foo returns, there’s no more reference via the thread’s stack to ‘d’.  The only reference to ‘d’ is in the closure/delegate hooked up to the task as a continuation.  If that task were rooted, that would be enough to keep the DisplayOnFinalize instance alive, but the task isn’t rooted.  The task is referred to by the TaskCompletionSource<bool> instance ‘tcs’ on the thread’s stack, but when the call to Foo goes away, so too does that reference to ‘tcs’.  And thus, all of these instances become available for garbage collection.

All of this serves to highlight an important fact: when you await something, it’s that something which needs to keep the async method’s execution alive via a reference to the continuation object that’s provided by ‘await’ to the awaited awaitable’s awaiter (try saying that ten times fast).  When you await Task.Run(…), the task you’re awaiting is rooted in the ThreadPool’s queues (or if the task is already executing, it’s rooted by the stack processing the task).  When you await Task.Delay(…), the task you’re awaiting is rooted by the underlying timer’s internal data structures.  When you await a task for an async I/O operation, the task is typically rooted by some data structure held by the I/O completion port that will be signaled when the async operation completes.  And so on.

This all makes logical sense: in order to complete the task when the async operation completes, the thing that will be completing it must have a reference to it.  But it’s still something good to keep in mind… if you ever find that your async methods aren’t running to completion, consider whether awaitables you’re awaiting might be getting garbage collected before they complete.  In my previous post, I referred to a real bug that was discovered to be caused by a task never completing.  The way we happened across that bug was because a finalizable class similar in nature to the one shown previously was actually in use, and its finalizer was firing.  This led us to realize that it was invoked because the awaited task was getting garbage collected before it was completed, which was because of a a queue of work referencing completion sources, and that queue that was being cleared without canceling those associated tasks.

Keeping Async Methods Alive的更多相关文章

  1. 使用Async方法 Using Async Methods 精通ASP-NET-MVC-5-弗瑞曼 Listing 4-32.

  2. Async/Await FAQ

    From time to time, I receive questions from developers which highlight either a need for more inform ...

  3. 编程概念--使用async和await的异步编程

    Asynchronous Programming with Async and Await You can avoid performance bottlenecks and enhance the ...

  4. Async Performance: Understanding the Costs of Async and Await

    Stephen Toub Download the Code Sample Asynchronous programming has long been the realm of only the m ...

  5. await and async

    Most people have already heard about the new “async” and “await” functionality coming in Visual Stud ...

  6. C# Async, Await and using statements

    Async, Await 是基于 .NEt 4.5架构的, 用于处理异步,防止死锁的方法的开始和结束, 提高程序的响应能力.比如: Application area           Support ...

  7. (译)关于async与await的FAQ

    传送门:异步编程系列目录…… 环境:VS2012(尽管System.Threading.Tasks在.net4.0就引入,在.net4.5中为其增加了更丰富的API及性能提升,另外关键字”async” ...

  8. Async and Await

    http://blog.stephencleary.com/2012/02/async-and-await.html Most people have already heard about the ...

  9. Don't Block on Async Code【转】

    http://blog.stephencleary.com/2012/07/dont-block-on-async-code.html This is a problem that is brough ...

随机推荐

  1. Pow 算法

    #include <iostream> using namespace std; template<class T, class Int> T Pow(T x, Int n) ...

  2. JQuery源码解析--callbacks

    (function (global, factory) { factory(global); })(this, function (window, noGlobal) { var rootjQuery ...

  3. SVN服务器与测试服务器代码同步

    在本地做测试项目的时候,想svn提交和服务器上的代码一步到位,不想再手动更新一次了,所以就研究了下同步, 要实现svn提交后自动更新到测试服务器,在你的版本库下的hooks文件夹下添加post-com ...

  4. Scala中Iterator允许执行一次

    背景 使用spark执行mapPartitionsWithIndex((index,iterator)=>{....}),在执行体中将iterator进行一次迭代后,再次根据iterator执行 ...

  5. 九月二十八JS验证

    js表单验证 js可用发来在数据被送往服务器前对HTML表单中的这些输入数据进行验证 被js验证的这些典型的表单数据有: >用户是否已填写表单中的必填项目: >用户输入的邮件地址是否是合法 ...

  6. ORACLE RAC 11G 更改 /etc/hosts文件

    来自官方文档:()Can I change the public hostname in my Oracle Database 10g Cluster using Oracle Clusterware ...

  7. mysql分页原理和高效率的mysql分页查询语句

    该博来自网络转载!!!供自己学习使用!!! 以前我在mysql中分页都是用的 limit 100000,20这样的方式,我相信你也是吧,但是要提高效率,让分页的代码效率更高一些,更快一些,那我们又该怎 ...

  8. 解决Nginx不支持pathinfo的问题

    server { listen 80; server_name www.zq27.cc zq27.cc; root /data/wwwroot/www.zq27.cc/; access_log off ...

  9. 转载:JAVA中关于set()和get()方法的理解及使用

    对于JAVA初学者来说,set和get这两个方法似乎已经很熟悉了,这两个方法是JAVA变成中的基本用法,也是出现频率相当高的两个方法. 为了让JAVA初学者能更好的理解这两个方法的使用和意义,今天笔者 ...

  10. 【笔记】memorymanagement-whitepaper-150215

    3 GC概念 Gc的职责: 1)  分配内存 2)  保证被引用的对象驻留内存 3)  对象不可达后将其占用内存回收 被引用对象被称为 “存活对象”. 不再被引用的对象称为“垃圾对象”. 找到垃圾对象 ...