单线程且无递归的 C# 中的奇怪死锁
我的 C# 应用程序遇到了奇怪的行为。
我通过 VS2010 在调试模式下运行应用程序,几个小时后,应用程序只是等待锁释放,但唯一存活的线程是等待锁释放的线程,并且该函数中没有递归:
class ProductionNode {
private readonly object _lock = new object();
public bool Activate( long jobId )
{
lock(_lock) // Doesn't go through here
{
return DAL.InsertJobIfNotExists(jobId)>0; //SQL PLAIN INSERT IF NOT EXIST COMMAND
}
}
public void HasJobs()
{
lock(_lock)
{
return DAL.HasProductionJobs();
}
}
}
再次,当我使用 VS2010 暂停应用程序时,使用 ProductionNode 的唯一线程是等待 _lock 对象被释放的线程。
为了清楚起见,应用程序可能会在多个线程中激活生产节点,但在发生死锁的给定场景中,VS 仅显示使用 ProductionNode 对象的单个线程,其他线程使用其他对象类型。< /strong>
有什么想法吗?
干杯, 多伦
I am running into a strange behavior of my C# application.
I am running the application in debug mode via VS2010 and after several hours the application just waits for a lock release but the only thread which is alive is the one that waits upon the lock to be released and there is not recursion in that function:
class ProductionNode {
private readonly object _lock = new object();
public bool Activate( long jobId )
{
lock(_lock) // Doesn't go through here
{
return DAL.InsertJobIfNotExists(jobId)>0; //SQL PLAIN INSERT IF NOT EXIST COMMAND
}
}
public void HasJobs()
{
lock(_lock)
{
return DAL.HasProductionJobs();
}
}
}
Again, when I am pausing the application using the VS2010 the only thread that uses the ProductionNode is the one that waits for the _lock object to be released.
To make things clear, the application might activate the production node in several threads but the in the given scenario where the deadlock occur the VS only displays a single thread which uses the ProductionNode object, the other threads uses other object types.
Any ideas?
Cheers,
Doron
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议使用
windbg
(Windows 调试工具,包含在 Windows SDK 中)和 psscor2 更深入地调试您的问题。基本上,在
windbg
中尝试:1 .加载 psscor2:
2 . 查找哪些线程持有哪些锁
尝试使用
!syncblk
3 。查看正在运行的线程并与持有锁的线程进行比较
!threads
4 。在线程之间切换
~Xs *[其中 X 是您从 !threads 获得的线程 id,并且您对此感兴趣,因为它持有锁]*
!clrstack -a
编辑:
由于您可能使用的是 .net 4,因此您可能需要使用 SOSEX 而不是 psscor2。到目前为止我还没有真正使用过 SOSEX,但我想看看死锁命令:
!dlk
I'd recommend using
windbg
(Dbugging Tools for Windows, included in the Windows SDK) and psscor2 to debug into your problem a little deeper.Basically, in
windbg
try to:1 . Load psscor2:
2 . Try finding which threads are holding what locks using
!syncblk
3 . Look at what threads are running and compare to the threads that are holding locks
!threads
4 . Switch between threads
~Xs *[where X is the thread id you got from !threads and that you are interested in because it's holding a lock]*
!clrstack -a
EDIT:
Since you're presumably using .net 4, you might want to use SOSEX instead of psscor2. I haven't really used SOSEX so far, but I'd have a look at the deadlock command:
!dlk
一些可能有帮助的提示。我会执行以下步骤:
_lock
并找到所有使用 Visual Studio 的参考
上下文菜单。也许你正在使用
锁在其他地方(你在更新中做了)。
静态
并且
ProductionNode
类不是singleton
被困在里面
DAL.InsertJobIfNotExists
所以记录它的入口和出口
将有助于追踪问题根源。
Some hints that may help. I would go these steps:
_lock
and find allreferences using Visual Studio
context menu. Maybe you are using
the lock somewhere else (you did in your update).
static
and
ProductionNode
class is notsingleton
trapped inside
DAL.InsertJobIfNotExists
sologging it's entrance and exit
would help tracing the problem root.