首页 > 其他分享 >记一次 .NET 某工控电池检测系统 卡死分析

记一次 .NET 某工控电池检测系统 卡死分析

时间:2023-11-13 15:44:23浏览次数:27  
标签:SQLite x86 Threading 工控 System instance NET Data 卡死

一:背景

1. 讲故事

前几天有位朋友找到我,说他的窗体程序有卡死现象,让我帮忙看下怎么回事,解决这种问题就需要在卡死的时候抓一个dump下来,拿到dump之后就可以分析了。

二:为什么会卡死

1. 观察主线程

窗体程序的卡死,需要观察主线程此时正在做什么,可以用 !clrstack 命令观察。


0:000:x86> !clrstack
OS Thread Id: 0x4a08 (0)
Child SP       IP Call Site
012fe784 0000002b [HelperMethodFrame_1OBJ: 012fe784] System.Threading.WaitHandle.WaitOneNative(System.Runtime.InteropServices.SafeHandle, UInt32, Boolean, Boolean)
012fe868 7115d952 System.Threading.WaitHandle.InternalWaitOne(System.Runtime.InteropServices.SafeHandle, Int64, Boolean, Boolean) [f:\dd\ndp\clr\src\BCL\system\threading\waithandle.cs @ 243]
012fe880 7115d919 System.Threading.WaitHandle.WaitOne(Int32, Boolean) [f:\dd\ndp\clr\src\BCL\system\threading\waithandle.cs @ 194]
012fe894 711e89bf System.Threading.WaitHandle.WaitOne(Int32) [f:\dd\ndp\clr\src\BCL\system\threading\waithandle.cs @ 220]
012fe89c 6fb186b8 System.Threading.ReaderWriterLockSlim.WaitOnEvent(System.Threading.EventWaitHandle, UInt32 ByRef, TimeoutTracker, EnterLockType)
012fe8e0 6fb17892 System.Threading.ReaderWriterLockSlim.TryEnterReadLockCore(TimeoutTracker)
012fe920 6fb17562 System.Threading.ReaderWriterLockSlim.TryEnterReadLock(TimeoutTracker)
012fe94c 0325f49f xxx.QuyitpjK0dXKR6IyqH(System.Object)
012fe964 0325ee8a xxx.RWAutoLock..ctor(System.Threading.ReaderWriterLockSlim, Boolean)
...

从卦中的线程栈数据来看,貌似是卡在一个读写锁TryEnterReadLock 上,根据读写锁的规则,必然有人执行了一个 WriteLock 并且出不来,接下来就是寻找持有这个 lock 的线程。

2. 到底谁在持有

如果是 lock ,相信很多朋友都知道用 !syncblk 命令,那读写锁用什么命令呢?说实话我也搞不清楚,只能先挖挖 ReaderWriterLockSlim 类本身,看看有没有什么新发现。


0:000:x86> !DumpObj /d 03526f38
Name:        System.Threading.ReaderWriterLockSlim
MethodTable: 6f947428
EEClass:     6f9a92dc
Size:        72(0x48) bytes
File:        C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Core\v4.0_4.0.0.0__b77a5c561934e089\System.Core.dll
Fields:
      MT    Field   Offset                 Type VT     Attr    Value Name
70da878c  40004aa       38       System.Boolean  1 instance        0 _fIsReentrant
6f92fa28  40004ab       3c ...LockSlim+SpinLock  1 instance 03526f74 _spinLock
70dfba4c  40004ac       1c        System.UInt32  1 instance       20 _numWriteWaiters
70dfba4c  40004ad       20        System.UInt32  1 instance        1 _numReadWaiters
70dfba4c  40004ae       24        System.UInt32  1 instance        0 _numWriteUpgradeWaiters
70dfba4c  40004af       28        System.UInt32  1 instance        0 _numUpgradeWaiters
6f93d764  40004b0       39          System.Byte  1 instance        0 _waiterStates
70da42a8  40004b1       2c         System.Int32  1 instance       -1 _upgradeLockOwnerId
70da42a8  40004b2       30         System.Int32  1 instance       11 _writeLockOwnerId
70da6924  40004b3        c ...g.EventWaitHandle  0 instance 034844d0 _writeEvent
70da6924  40004b4       10 ...g.EventWaitHandle  0 instance 042a69c8 _readEvent
70da6924  40004b5       14 ...g.EventWaitHandle  0 instance 00000000 _upgradeEvent
70da6924  40004b6       18 ...g.EventWaitHandle  0 instance 00000000 _waitUpgradeEvent
70da150c  40004b8        4         System.Int64  1 instance 367 _lockID
70da878c  40004ba       3a       System.Boolean  1 instance        0 _fUpgradeThreadHoldingRead
70dfba4c  40004bc       34        System.UInt32  1 instance 3221225472 _owners
70da878c  40004c2       3b       System.Boolean  1 instance        0 _fDisposed
70da42a8  40004a9      4dc         System.Int32  1   static        4 ProcessorCount
70da150c  40004b7      4d4         System.Int64  1   static 1882 s_nextLockID
6f942b7c  40004b9        0 ...ReaderWriterCount  0 TLstatic  t_rwc

结合源码分析,发现上面的 _writeLockOwnerId=11 就是持有锁的线程ID,找到持有线程就好办了,把这个 managedid=11 转成 dbgid 再观察。


0:000:x86> !t

  13   11 47bc 0a0702c0   1029220 Preemptive  00000000:00000000 01425ed0 0     MTA (Threadpool Worker) 

0:013:x86> !clrstack
OS Thread Id: 0x47bc (13)
Child SP       IP Call Site
07e4f1ac 0000002b [InlinedCallFrame: 07e4f1ac] 
07e4f1a4 09e38597 DomainBoundILStubClass.IL_STUB_PInvoke(IntPtr)
07e4f1ac 09e38334 [InlinedCallFrame: 07e4f1ac] System.Data.SQLite.UnsafeNativeMethods.sqlite3_step(IntPtr)
07e4f1dc 09e38334 System.Data.SQLite.SQLite3.Step(System.Data.SQLite.SQLiteStatement)
07e4f228 09e36fe8 System.Data.SQLite.SQLiteDataReader.NextResult()
07e4f250 09e36ceb System.Data.SQLite.SQLiteDataReader..ctor(System.Data.SQLite.SQLiteCommand, System.Data.CommandBehavior)
07e4f270 09e367ce System.Data.SQLite.SQLiteCommand.ExecuteReader(System.Data.CommandBehavior)
07e4f284 09e36732 System.Data.SQLite.SQLiteCommand.ExecuteNonQuery(System.Data.CommandBehavior)
07e4f2b0 09e366e6 System.Data.SQLite.SQLiteCommand.ExecuteNonQuery()
07e4f2bc 09e350dc SqlSugar.AdoProvider.ExecuteCommand(System.String, SqlSugar.SugarParameter[])
07e4f388 13189518 SqlSugar.InsertableProvider`1[[System.__Canon, mscorlib]].ExecuteCommand()
07e4f420 0181ac4a xxx.OperateLog+d__8.MoveNext()
...

0:013:x86> k
CvRegToMachine(x86) conversion failure for 0x14f
X86MachineInfo::SetVal: unknown register 0 requested
 # ChildEBP RetAddr      
00 07e4ede0 76c9ad10     ntdll_76ed0000!NtFlushBuffersFile+0xc
01 07e4ede0 6b27af8c     KERNELBASE!FlushFileBuffers+0x30
WARNING: Stack unwind information not available. Following frames may be wrong.
02 07e4edf0 6b270256     SQLite_Interop!SI768767362ea03a94+0xf73c
03 07e4ee1c 6b267938     SQLite_Interop!SI768767362ea03a94+0x4a06
04 07e4ee38 6b2599e1     SQLite_Interop!SI83d1cf4976f57337+0x84c8
05 07e4ee80 6b25902b     SQLite_Interop!SIa3401e98cbad673e+0x3201
06 07e4ee98 6b25258c     SQLite_Interop!SIa3401e98cbad673e+0x284b
07 07e4f168 6b255a05     SQLite_Interop!SI327cfc7a6b1fd1fb+0x633c
08 07e4f19c 09e38597     SQLite_Interop!SI9c6d7cd7b7d38055+0x255

结合卦中的读写信息,大概知道了原来是用写锁来写sqlite,后者卡在缓冲区刷新函数 NtFlushBuffersFile 上,方法签名如下:


NTSTATUS NtFlushBuffersFile(
  HANDLE  FileHandle,
  IO_STATUS_BLOCK *IoStatusBlock
);

有些朋友可能想看一下到底怎么写的,那就简单的反编译一下代码:

到这里基本就搞清楚了,由于 13号 线程持有了 写锁,导致主线程要用读锁操作 sqlite 时进行了长时间等待。

解决办法就比较简单了,主线程尽可能的只做UI更新的操作,不要让他触发各类锁,否则就有等锁的概率发生。

3. NtFlushBuffersFile 怎么了

有些朋友可能要问为什么 NtFlushBuffersFile 函数会卡死不返回,要想找到这个答案,需要看下反汇编。


0:013:x86> uf ntdll_76ed0000!NtFlushBuffersFile
ntdll_76ed0000!NtFlushBuffersFile:
76f41ad0 b84b000000      mov     eax,4Bh
76f41ad5 ba7071f576      mov     edx,offset ntdll_76ed0000!Wow64SystemServiceCall (76f57170)
76f41ada ffd2            call    edx
76f41adc c20800          ret     8

0:013:x86> u 76F57170h
ntdll_76ed0000!Wow64SystemServiceCall:
76f57170 ff252892ff76    jmp     dword ptr [ntdll_76ed0000!Wow64Transition (76ff9228)]

0:013:x86> u 76ec7000
wow64cpu!KiFastSystemCall:
76ec7000 ea0970ec763300  jmp     0033:76EC7009
76ec7007 0000            add     byte ptr [eax],al
76ec7009 41              inc     ecx
76ec700a ffa7f8000000    jmp     dword ptr [edi+0F8h]

从汇编代码看,NtFlushBuffersFile 通过 KiFastSystemCall 进入内核态了,用户态dump是没法看内核态的,所以也无法继续追究下去。

不过也可以看下这个线程过往的 GetLastError() 值,可能有些收获,使用 !gle 命令。


0:013:x86> !gle
LastErrorValue: (Win32) 0x26 (38) - <Unable to get error code text>
LastStatusValue: (NTSTATUS) 0xc0000008 - <Unable to get error code text>

根据上面的状态码,去msdn上搜一下具体信息。

从错误说明看,可能是这个sqlite文件有什么问题,又是句柄无效,又是读到头了,怀疑是操作sqlite 的时候出现了文件损坏

现在回头看看,如果想对 Sqlite 进行并发读写,开启下 Write-Ahead Logging 模式应该就可以了,不需要在程序里面进行读写控制。

所以最终的建议就是:

  • 开启WAL模式
  • 删掉读写控制

三:总结

这次卡死事故还是挺有意思的,熟悉了下 ReaderWriterLockSlim 又对 sqlite 有了一个新的认识。

图片名称

标签:SQLite,x86,Threading,工控,System,instance,NET,Data,卡死
From: https://www.cnblogs.com/huangxincheng/p/17829307.html

相关文章

  • 服务器Windows Server发布.NET Core项目出现HTTP错误500.19 - Internal Server Error[
    服务器WindowsServer发布.NETCore项目出现HTTP错误500.19-InternalServerError[错误代码:0x8007000d]经检查,发现是因为缺少【ASPNETCoreModuleV2】解决方案:到微软官方下载相应.net版本的HostingBundle  https://dotnet.microsoft.com/en-us/download/dotnet  下......
  • Netty(二)文件编程
    Netty(二)文件编程1FileChannel不能够直接打开FileChannel,只能够通过FileInputStream、FIleOutPutStream和RandomAccessFile的getChannel()方法来获取FileChannelFileInputStream获得的channel只能读FIleOutPutStream获得的channel只能写RandomAccessFile是否能读写需要根据......
  • Netty(四)NIO多线程优化
    Netty(四)NIO多线程优化​ 前面的代码都只有一个选择器,没有充分利用多核CPU,因此可以分两组选择器boss:单线程配一个选择器,专门处理accept事件,不负责数据的读写worker:创建CPU核心数的线程,每个线程配一个选择器,轮流处理read事件1多线程问题分析关键是这一部分的代码,需要保......
  • Netty(三)网络编程
    Netty(三)网络编程1阻塞和非阻塞堵塞:在没有数据可读的时候,包括数据复制的过程,线程必须堵塞等待,不会占用CPU但是线程相当于闲置在单线程下,两个堵塞的方法会相互影响,必须使用多线程,32位JVM一个线程320K,64位JVM一个线程1024K,为了减少线程数,需要采用线程池技术但是即便使用了线......
  • 每个.NET开发都应掌握的C#多线程知识点
    上篇文章讲述了C#特性(Attribute)知识点,本文将介绍多线程的知识点。多线程编程是现代软件开发中的重要组成部分,它能够充分利用多核处理器,提高应用程序的性能和响应性。C#作为.NET开发的主要语言,提供了强大的多线程支持。本文将介绍C#多线程知识点,帮助.NET开发者更好地应对多线程编程......
  • Kubernetes 各个组件介绍
    集群物理结构从角色角度K8s集群分为:master:所有请求的入口。node:所有应用的载体。部署方式:单Master节点:一个Master节点的K8s集群,不适用于生产环境。多Master节点:多个Master节点的K8s集群,具有高可用性。Pod创建时各个组件的功能当一个客户想要创建一个nginx的时候,各组件的请求流程。......
  • .net6.0及以上WPF中使用GDI+的demo
    usingSystem;usingSystem.Drawing;usingSystem.Runtime.InteropServices;usingSystem.Windows;usingSystem.Windows.Interop;usingSystem.Windows.Media.Imaging;namespaceTryDemo{///<summary>///InteractionlogicforMainWindow.xaml......
  • Delphi TNetHTTPClient使用递归方式取所有分页数据
    DelphiTNetHTTPClient使用递归方式取所有分页数据   业务系统提供的一个查询数据接口,可以通过分页方式取得数据,如果一次性取得所有数据,将页大小增大即可,但如果数据太多怕会造成内存溢出。   综合考虑每次只取一个分页,分页数据不要太大,用递归方式来获取是比较合理的解......
  • netsh、ssf、proxychains、netsh端口转发
    端口转发1本地端口转发(正向代理)实验kali:192.168.9.16ubuntu:192.168.9.15ubuntu2:192.168.9.10首先就是主机安装ssh工具,用ssh做端口转发kali开启ssh服务,需要下载openssh-serverapt-getinstallopenssh-server然后开启ssh服务servicesshstart然后配置可使用密......
  • 传递参数或查询字符串给_Layout.cshtml中的顶部导航在ASP.NET中的实现方式
    在ASP.NET中,你可以通过多种方式将参数或查询字符串传递给_Layout.cshtml中的顶部导航。以下是两种常见的方法:1.使用ViewBag:ViewBag是ASP.NETMVC框架中的一个特性,它允许你在视图和控制器之间传递数据。你可以在控制器中设置ViewBag的值,然后在_Layout.cshtml中使用这些值。示例代码......