自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

記一次 .NET 某設(shè)備監(jiān)控系統(tǒng)死鎖分析

開發(fā) 前端
因為是窗體程序,所以看主線程的線程棧就好了,如果卡在 用戶態(tài)? 那這個問題相對容易解決,如果卡在 內(nèi)核態(tài) 這個問題就比較復(fù)雜了,需要開啟 WinDbg 的本機(jī)內(nèi)核調(diào)試或者雙機(jī)調(diào)試才能找到最終的問題。

?一:背景

1. 講故事

上周看了一位訓(xùn)練營朋友的dump,據(jù)朋友說他的程序卡死了,看完之后發(fā)現(xiàn)是一例經(jīng)典的死鎖問題,蠻有意思,這個案例算是學(xué)習(xí) .NET高級調(diào)試 入門級的案例,這里和大家分享一下。

二:WinDbg 分析

1. 程序為什么會卡死

因為是窗體程序,所以看主線程的線程棧就好了,如果卡在 用戶態(tài)? 那這個問題相對容易解決,如果卡在 內(nèi)核態(tài) 這個問題就比較復(fù)雜了,需要開啟 WinDbg 的本機(jī)內(nèi)核調(diào)試或者雙機(jī)調(diào)試才能找到最終的問題。

既然已經(jīng)說了是入門級,那肯定是卡在 用戶態(tài)? 層面啦,我們用 !clrstack 命令觀察下主線程的線程棧即可,輸出如下:

0:000:x86> !clrstack
OS Thread Id: 0x31d8 (0)
Child SP IP Call Site
00f9ec28 00e9e108 [GCFrame: 00f9ec28]
00f9ed08 00e9e108 [GCFrame: 00f9ed08]
00f9ed24 00e9e108 [HelperMethodFrame_1OBJ: 00f9ed24] System.Threading.Monitor.ReliableEnter(System.Object, Boolean ByRef)
00f9eda0 70c08468 System.Threading.Monitor.Enter(System.Object, Boolean ByRef) [f:\dd\ndp\clr\src\BCL\system\threading\monitor.cs @ 62]
00f9edb0 0ce916c7 xxxx.GetAlarmCount(xxx)
00f9ee28 0961f41f xxx.xxx()
00f9ef04 0961d60a xxxx.xxx(System.Object, System.EventArgs)
00f9ef50 6de03dc9 System.Windows.Forms.Timer.OnTick(System.EventArgs)
00f9ef58 6de053d9 System.Windows.Forms.Timer+TimerNativeWindow.WndProc(System.Windows.Forms.Message ByRef)
00f9ef64 6ddd38d0 System.Windows.Forms.NativeWindow.Callback(IntPtr, Int32, IntPtr, IntPtr)
00f9f1b0 0130d5d4 [InlinedCallFrame: 00f9f1b0]
00f9f1ac 6de375bd DomainBoundILStubClass.IL_STUB_PInvoke(MSG ByRef)
00f9f1b0 6dde44e3 [InlinedCallFrame: 00f9f1b0] System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG ByRef)
00f9f1e4 6dde44e3 System.Windows.Forms.Application+ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr, Int32, Int32)
00f9f1e8 6dde40d1 [InlinedCallFrame: 00f9f1e8]
00f9f270 6dde40d1 System.Windows.Forms.Application+ThreadContext.RunMessageLoopInner(Int32, System.Windows.Forms.ApplicationContext)
00f9f2c0 6dde3f23 System.Windows.Forms.Application+ThreadContext.RunMessageLoop(Int32, System.Windows.Forms.ApplicationContext)
00f9f2ec 6ddbc83d System.Windows.Forms.Application.Run(System.Windows.Forms.Form)
00f9f300 01350a6e CleanControl.Program.Main(System.String[])
00f9f4ec 71d00556 [GCFrame: 00f9f4ec]
...

從卦中看,主線程卡在 Monitor.Enter? 處,也就表明當(dāng)前線程在 GetAlarmCount() 方法的一個 lock 處等待。

2. 誰在持有鎖

要想找到誰在持有鎖,需要理解 lock? 的底層機(jī)制,它是建立在 AutoResetEvent + ObjectHeader? 基礎(chǔ)之上的一種鎖玩法,在 CLR 層面使用 SyncBlk 的 class 來承載的,參考如下代碼:

class SyncBlock
{
// ObjHeader creates our Mutex and Event
friend class ObjHeader;
friend class SyncBlockCache;
friend struct ThreadQueue;
#ifdef DACCESS_COMPILE
friend class ClrDataAccess;
#endif
friend class CheckAsmOffsets;
protected:
AwareLock m_Monitor; // the actual monitor
SLink m_Link;
DWORD m_dwHashCode;
WCHAR m_BSTRTrailByte;
}

要想觀察這些 SyncBlk? 信息,可以用 WinDbg 提供的快捷命令 !syncblk 來觀察。

0:000:x86> !syncblk
Index SyncBlock MonitorHeld Recursion Owning Thread Info SyncBlock Owner
180 0b86e8e8 3 1 01452a08 3728 24 039da140 System.Object
-----------------------------
Total 339
CCW 5
RCW 2
ComClassFactory 0
Free 4

從卦中看,當(dāng)前持有 lock 的線程是 24? 號,那這個線程為什么遲遲不退出鎖呢?這就需要到這個線程棧上找原因了, 使用命令 ~24s; !clrstack 即可。

0:004:x86> ~24s
ntdll_779a0000!NtWaitForMultipleObjects+0xc:
77a11b2c c21400 ret 14h
0:024:x86> !clrstack
OS Thread Id: 0x3728 (24)
Child SP IP Call Site
0e99e504 0000002b [HelperMethodFrame_1OBJ: 0e99e504] System.Threading.WaitHandle.WaitOneNative(System.Runtime.InteropServices.SafeHandle, UInt32, Boolean, Boolean)
0e99e5e8 70bdd952 System.Threading.WaitHandle.InternalWaitOne(System.Runtime.InteropServices.SafeHandle, Int64, Boolean, Boolean) [f:\dd\ndp\clr\src\BCL\system\threading\waithandle.cs @ 243]
0e99e600 70bdd919 System.Threading.WaitHandle.WaitOne(Int32, Boolean) [f:\dd\ndp\clr\src\BCL\system\threading\waithandle.cs @ 194]
0e99e614 6e4aa4a8 System.Windows.Forms.Control.WaitForWaitHandle(System.Threading.WaitHandle)
0e99e654 6e8585af System.Windows.Forms.Control.MarshaledInvoke(System.Windows.Forms.Control, System.Delegate, System.Object[], Boolean)
0e99e658 6e4acc4f [InlinedCallFrame: 0e99e658]
0e99e6e0 6e4acc4f System.Windows.Forms.Control.Invoke(System.Delegate, System.Object[])
...
0e99e83c 0f46512c xxx.AddAlarmQueue(xxx)
...
0e99ea84 0d3f2783 xxx.Func()
0e99ead8 70be2e01 System.Threading.ThreadHelper.ThreadStart_Context(System.Object) [f:\dd\ndp\clr\src\BCL\system\threading\thread.cs @ 74]
...

從卦中看,其中的 MarshaledInvoke 方法很刺眼,它表示工作線程通過 Invoke 向主線程的控件推送數(shù)據(jù),因為主線程遲遲沒有響應(yīng)它,導(dǎo)致它一直在等待,而恰恰它又持有了 lock 鎖,不趕巧主線程因為獲取lock在遲遲等待又無法響應(yīng)工作線程的 MarshaledInvoke 請求,導(dǎo)致一種死鎖狀態(tài),如果要畫個圖大概是這樣的。

圖片

3. 如何化解

尋得化解之法,需要看下程序中是怎么持有 lock 鎖的,仔細(xì)觀察代碼之后,終于找到了 lock 代碼處,截圖如下:

圖片

對代碼敏感得朋友相信一眼就能看出,這 lock 的粒度真tmd的大,只要 lock 中有一處調(diào)用了 Invoke,如果不湊巧主線程剛好在等待 lock ,那就死鎖了,正如本篇中的 死鎖。

三:總結(jié)

這次卡死事故,本質(zhì)上來說是程序員對鎖的使用沒有一個好的習(xí)慣,沒有遵循鎖的盡早釋放原則。

其實這一塊關(guān)系型數(shù)據(jù)庫做的特別好,鎖的粒度分的很細(xì),諸如:行鎖,RID鎖,Key鎖,頁鎖,表鎖,在必要的時候還會涉及到鎖的升級,將性能,鎖開銷,一致性 做到了極致,非常值得我們研究和學(xué)習(xí)。

責(zé)任編輯:武曉燕 來源: 一線碼農(nóng)聊技術(shù)
相關(guān)推薦

2024-03-15 15:15:53

.NETCPU系統(tǒng)

2023-09-27 07:23:10

.NET監(jiān)控軟件

2024-03-28 12:56:36

2024-06-13 17:09:55

2023-03-26 20:24:50

ERP網(wǎng)站系統(tǒng)

2024-03-26 00:44:53

.NETCIM系統(tǒng)

2024-07-01 13:00:24

.NET網(wǎng)絡(luò)邊緣計算

2022-01-17 21:28:36

管理系統(tǒng).NET

2023-06-29 17:55:00

.NET日志WinDbg

2024-07-09 11:51:20

Windows線程池源碼

2024-11-29 10:06:59

2021-11-02 07:54:41

內(nèi)存.NET 系統(tǒng)

2023-06-26 00:12:46

2024-12-27 13:31:18

.NETdump調(diào)試

2024-06-04 10:54:34

.NET代碼程序

2024-08-08 11:21:01

2024-05-20 09:39:02

.NETurl線程池

2022-10-13 18:40:05

.NETOA后端

2023-07-06 10:11:38

.NET模式dump

2024-07-12 11:20:34

.NET崩潰視覺程序
點贊
收藏

51CTO技術(shù)棧公眾號