一句Task.Result就死锁,这代码还怎么写?

开发 前端
大概就是说在 Winform 的主线程下执行 Task.Result 会造成死锁,我也看了图中的参考链接, Stephen 是绝对的大佬,不过这篇文章对死锁的成因主要还是大段的文字灌输,没有真的让你眼见为实,那这篇我就从 windbg 的角度来给它剖析下。

 [[398889]]

本文转载自微信公众号「一线码农聊技术」,作者一线码农聊技术。转载本文请联系一线码农聊技术公众号。

一、背景

1. 讲故事

前些天把 .NET 高级调试 方面的文章索引到 https://github.com/ctripxchuang/dotnetfly 的过程中,发现了一个有意思的评论,截图如下:

大概就是说在 Winform 的主线程下执行 Task.Result 会造成死锁,我也看了图中的参考链接, Stephen 是绝对的大佬,不过这篇文章对死锁的成因主要还是大段的文字灌输,没有真的让你眼见为实,那这篇我就从 windbg 的角度来给它剖析下。

二、windbg 分析

1. 真的会死锁吗?

看文章看截图貌似真的会死锁,当然我多年不玩 winform 了,也搞不清楚到底会不会,至少在 Console 中是不会的,得,先上一段测试代码。

  1. public partial class Form1 : Form 
  2.     { 
  3.         public Form1() 
  4.         { 
  5.             InitializeComponent(); 
  6.         } 
  7.  
  8.         private void button1_Click(object sender, EventArgs e) 
  9.         { 
  10.             var jsonTask = GetJsonAsync("http://cnblogs.com").Result; 
  11.             textBox1.Text = jsonTask; 
  12.         } 
  13.  
  14.         public async static Task<string> GetJsonAsync(string uri) 
  15.         { 
  16.             using (var client = new HttpClient()) 
  17.             { 
  18.                 var jsonString = await client.GetStringAsync(uri); 
  19.  
  20.                 return jsonString; 
  21.             } 
  22.         } 
  23.     } 

代码非常简单,把程序跑起来,点一下 click,果然界面卡住了,有点不可思议。

2. 寻找死锁原因

接下来赶紧祭出 windbg 附加到进程上一探究竟吧。

1) 查看主线程

界面无响应了,自然是主线程卡住了,所以急需看一下此时的主线程在干嘛?用命令 ~0s + !clrstack 即可。

  1. 0:000> !clrstack  
  2. OS Thread Id: 0x5a10 (0) 
  3.         Child SP               IP Call Site 
  4. 0000004d10dfde00 00007ffb889a10e4 [GCFrame: 0000004d10dfde00]  
  5. 0000004d10dfdf28 00007ffb889a10e4 [HelperMethodFrame_1OBJ: 0000004d10dfdf28] System.Threading.Monitor.ObjWait(Boolean, Int32, System.Object) 
  6. 0000004d10dfe040 00007ffb66920d64 System.Threading.ManualResetEventSlim.Wait(Int32, System.Threading.CancellationToken) 
  7. 0000004d10dfe0d0 00007ffb6691b4bb System.Threading.Tasks.Task.SpinThenBlockingWait(Int32, System.Threading.CancellationToken) 
  8. 0000004d10dfe140 00007ffb672601d1 System.Threading.Tasks.Task.InternalWait(Int32, System.Threading.CancellationToken) 
  9. 0000004d10dfe210 00007ffb6725cfa7 System.Threading.Tasks.Task`1[[System.__Canon, mscorlib]].GetResultCore(Boolean) 
  10. 0000004d10dfe250 00007ffb18172a1b WindowsFormsApp4.Form1.button1_Click(System.Object, System.EventArgs) [E:\net5\ConsoleApp1\WindowsFormsApp4\Form1.cs @ 26] 
  11. 0000004d10dfe2b0 00007ffb3a024747 System.Windows.Forms.Control.OnClick(System.EventArgs) 
  12. 0000004d10dfe2f0 00007ffb3a027b83 System.Windows.Forms.Button.OnClick(System.EventArgs) 
  13. 0000004d10dfe340 00007ffb3a837231 System.Windows.Forms.Button.OnMouseUp(System.Windows.Forms.MouseEventArgs) 
  14. 0000004d10dfe400 00007ffb3a7e097d System.Windows.Forms.Control.WmMouseUp(System.Windows.Forms.Message ByRef, System.Windows.Forms.MouseButtons, Int32) 
  15. 0000004d10dfe480 00007ffb3a0311cc System.Windows.Forms.Control.WndProc(System.Windows.Forms.Message ByRef) 
  16. 0000004d10dfe540 00007ffb3a0b0c97 System.Windows.Forms.ButtonBase.WndProc(System.Windows.Forms.Message ByRef) 
  17. 0000004d10dfe5c0 00007ffb3a0b0be5 System.Windows.Forms.Button.WndProc(System.Windows.Forms.Message ByRef) 
  18. 0000004d10dfe5f0 00007ffb3a030082 System.Windows.Forms.NativeWindow.Callback(IntPtr, Int32, IntPtr, IntPtr) 
  19. 0000004d10dfe690 00007ffb3a765a02 DomainBoundILStubClass.IL_STUB_ReversePInvoke(Int64, Int32, Int64, Int64) 
  20. 0000004d10dfe9d0 00007ffb776d221e [InlinedCallFrame: 0000004d10dfe9d0] System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG ByRef) 
  21. 0000004d10dfe9d0 00007ffb3a0b9489 [InlinedCallFrame: 0000004d10dfe9d0] System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG ByRef) 
  22. 0000004d10dfe9a0 00007ffb3a0b9489 DomainBoundILStubClass.IL_STUB_PInvoke(MSG ByRef) 
  23. 0000004d10dfea60 00007ffb3a046661 System.Windows.Forms.Application+ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr, Int32, Int32) 
  24. 0000004d10dfeb50 00007ffb3a045fc7 System.Windows.Forms.Application+ThreadContext.RunMessageLoopInner(Int32, System.Windows.Forms.ApplicationContext) 
  25. 0000004d10dfebf0 00007ffb3a045dc2 System.Windows.Forms.Application+ThreadContext.RunMessageLoop(Int32, System.Windows.Forms.ApplicationContext) 
  26. 0000004d10dfec50 00007ffb181708e2 WindowsFormsApp4.Program.Main() [E:\net5\ConsoleApp1\WindowsFormsApp4\Program.cs @ 19] 
  27. 0000004d10dfee78 00007ffb776d6923 [GCFrame: 0000004d10dfee78]  

从堆栈输出看,主线程最后是卡在 Task.Result 下的 Monitor.ObjWait 上,也就是说它还没有取到最后的 jsonString,这就很奇怪了,都好几分钟了,难道网络出问题啦 ? 我这网可是100M火力全开。。。??????

2) jsonString 哪去了?

判断是不是网络的问题,有一个好办法,那就是直接暴力搜索托管堆,如果在托管堆上发现了 jsonString,那就说明是程序上的某些地方让 Result 迟迟得不到结束,用命令 !dumpheap -type String -min 8500 + !do 000001f19002fcf0 查看即可,如下图所示:

从图中可以清晰的看出 html 回来了,既然都回来了,为啥还没让 Task.Result 结束呢?下一步就是看一看这个 html 被谁持有,使用 !gcroot 即可。

  1. 0:000> !gcroot 000001f19002fcf0 
  2. Thread 5a10: 
  3.     0000004d10dfe250 00007ffb18172a1b WindowsFormsApp4.Form1.button1_Click(System.Object, System.EventArgs) [E:\net5\ConsoleApp1\WindowsFormsApp4\Form1.cs @ 26] 
  4.         rbp+10: 0000004d10dfe2b0 
  5.             ->  000001f180007f78 WindowsFormsApp4.Form1 
  6.             ->  000001f180070d68 System.ComponentModel.EventHandlerList 
  7.             ->  000001f180071718 System.ComponentModel.EventHandlerList+ListEntry 
  8.             ->  000001f1800716d8 System.EventHandler 
  9.             ->  000001f1800716b0 System.Windows.Forms.ApplicationContext 
  10.             ->  000001f180071780 System.EventHandler 
  11.             ->  000001f18006ab38 System.Windows.Forms.Application+ThreadContext 
  12.             ->  000001f18006b140 System.Windows.Forms.Application+MarshalingControl 
  13.             ->  000001f18016c9c8 System.Collections.Queue 
  14.             ->  000001f18016ca00 System.Object[] 
  15.             ->  000001f18016c948 System.Windows.Forms.Control+ThreadMethodEntry 
  16.             ->  000001f18016c8b8 System.Object[] 
  17.             ->  000001f1800e6f80 System.Action 
  18.             ->  000001f1800e6f60 System.Runtime.CompilerServices.AsyncMethodBuilderCore+MoveNextRunner 
  19.             ->  000001f1800a77d0 WindowsFormsApp4.Form1+<GetJsonAsync>d__2 
  20.             ->  000001f1800b4e50 System.Threading.Tasks.Task`1[[System.String, mscorlib]] 
  21.             ->  000001f19002fcf0 System.String 
  22.  
  23. Found 1 unique roots (run '!GCRoot -all' to see all roots). 

从输出结果看,这个 System.String 最后被 5a10 线程的 WindowsFormsApp4.Form1 持有,可以用 !t 验证一下 5a10 到底是什么线程。

  1. 0:000> !t                                                                                                        Lock   
  2.        ID OSID ThreadOBJ           State GC Mode     GC Alloc Context                  Domain           Count Apt Exception 
  3.    0    1 5a10 000001f1f1b01200  2026020 Preemptive  000001F1800E70E8:000001F1800E7FD0 000001f1f1ad5b90 0     STA  
  4.    2    2 712c 000001f1f1b2a270    2b220 Preemptive  0000000000000000:0000000000000000 000001f1f1ad5b90 0     MTA (Finalizer)  

我去,5a10 竟然是主线程,真的有点混乱,主线程被卡死,string 又被主线程持有,完全是莫名其妙。

3) 寻找突破点

还是回过头下冷静思考下这条 引用链,我发现这里有一个 Queue:-> 000001f18016c9c8 System.Collections.Queue,有思路了,我可以在入 Queue 的地方下个 断点 来调试下源代码,工具用 DnSpy, 说干就干。

从图中可以看到,当前入Queue时,用的是线程 10,也就是说此时 string 还没被主线程持有,再仔细分析下这个调用栈,我想你应该就搞清楚了,反正我看完之后脑子中就有了这张图。

从图中可以发现,延续的 Task 最后被 WindowsFormsSynchronizationContext.Post 调度到了 Control 下的 Queue 中,而这 Queue 中的数据需要 UI线程 去执行,所以就有了下面的对话:

主线程: task小弟,你什么时候执行完呀,我在等你信号呢?

task: 老哥,我已在你家啦,你什么时候过来接我呀?

总而言之:task需要主线程来执行它,主线程却在傻傻的等待 task 的 complete 状态,所以延续的task永远得不到执行,这就出现了很尴尬的场面,不知道你明白了吗? ??????

三、破解之法

知道了前因后果,这破解之法就简单了,大体上分两种。

1. 禁止将 延续task 丢到 Queue 中

要切断这条路,言外之意就是让线程池自己结束这个 task,这样 UI线程 就能感知到这个task已完成,最终 UI线程 就能获取最后的 html,做法就是在 await 后加上 ConfigureAwait(false) , 参考如下:

2. 禁止阻塞主线程

如果不阻塞主线程,那么主线程就可以自由的在 Control.Queue 中获取需要执行的任务,改法也很简单,只需要在 GetJsonAsync 前加上 await 即可。

四、总结

结论就是多自己实操实操,理论知识是别人强制灌输给你的,到底对还是不对,其实你自己心里也没底,实操验证才是真正属于你的,而且也很难忘记,毕竟你曾今真的体验过,实操过,验证过。

责任编辑:武晓燕 来源: 一线码农聊技术
相关推荐

2011-03-24 10:24:45

批量数据绑定

2011-03-28 15:48:52

批量数据绑定

2012-02-09 09:41:22

2023-11-06 08:31:58

业务代码多线程

2019-11-15 18:00:18

MySQLSQL数据库

2013-03-22 10:53:42

PyConPython

2011-06-13 09:25:01

断号

2009-03-10 18:10:12

LinuxUbuntu技巧

2013-05-10 10:56:09

2023-07-12 08:01:28

FOADMROADMOXC

2023-09-05 23:34:52

Kubernetes云原生

2021-12-17 08:55:26

Python微博机器人

2015-08-03 10:21:04

设计模式表达

2020-11-27 09:57:11

Python代码PyPy

2022-08-01 10:01:11

JavaScript语言代码库

2016-09-12 15:26:06

戴尔

2017-09-25 09:00:06

IT负责人信息化CIO

2019-08-15 11:42:56

程序员电脑软件

2021-06-30 00:14:24

JS代码数组

2010-03-29 11:55:12

无线上网报错
点赞
收藏

51CTO技术栈公众号