什么会导致/如何防止 ContextSwitchDeadlock? [英] What would cause/how can I prevent a ContextSwitchDeadlock?

查看:84
本文介绍了什么会导致/如何防止 ContextSwitchDeadlock?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Windows 服务中有一个运行时间相当长的进程,它会定期抛出ContextSwitchDeadlock"异常:

I have a rather long-running process in a Windows Service that periodically throws a "ContextSwitchDeadlock" exception:

我还操纵我的服务向自己发送电子邮件,其中包含有关遇到的异常的详细信息.我得到:

I have also rigged my service to send myself emails with details about encountered exceptions. I get:

Date: 05/25/2016 09:16:32:
Exception message: Timeout expired.  The timeout period elapsed prior to completion of the operation or the server is not responding.
Exception Source: .Net SqlClient Data Provider

...然后三秒后:

Date: 05/25/2016 09:16:35: 
Exception message: Cannot find table 0.
Exception Source: System.Data

顺便说一句:至少它是一致的:我遇到了三对这样的异常,每次第二个都在第一个之后正好三秒.

BTW: At least it's consistent: I have had three pairs of those exceptions, and each time the second one is datetimed exactly three seconds after the first.

我以前有过以这种方式或那种方式改变 SQLCommand 的 CommandTimeout 值(当前设置为 360)的经验,这似乎有点像蒙着眼睛扔飞镖甚至是黑魔法.有没有更好的方法来防止这种死锁?

I have had prior experience with jimmying the SQLCommand's CommandTimeout value (currently set at 360) this way or that, and it seems sort of like throwing darts blindfolded or even black magic. Is there a better way to prevent such deadlocks?

我有其他非常相似的方法不会导致这个问题;那些花更少的时间.该方法的持续时间(特别是运行查询所需的时间长度)似乎很麻烦".我无法改变这一点——它就是这样"——那我还能做什么?

I have other very similar methods that don't cause this problem; those take less time. The duration of the method (specifically, the length of time it takes the query to run) seems to be "the rub." I can't change that - "it is what it is" - so what else can I do?

自然地,在发布上述内容后,顽固的过程使我成为骗子",因为我又得到了两对异常,这次出现了第二对错误消息一个 在第一个之后的第二个而不是三个.如果我等待的时间足够长,也许第二个异常会在第一个异常之前发生.

Naturally, the recalcitrant process "made a liar out of me" right after posting the above, as I got two more pairs of the exceptions, this time with the second err msg of the pair occurring one second after the first instead of three. If I wait long enough, maybe the second exception will occur before the first one.

推荐答案

ContextSwitchDeadlock 是 Visual Studio 中的托管调试助手",是 Visual Studio 调试器在调试应用程序时为您提供额外帮助的众多工具之一.

The ContextSwitchDeadlock is a "Managed debugging assistant" in Visual Studio and is one of many tools that the Visual Studio debugger gives you additional aid when debugging an application.

我不是 100% 肯定它使用什么样的启发式来确定何时触发,但我知道一些,这也在工具窗口本身中描述:

I am not 100% positive what kind of heuristics it uses to figure out when to trigger but I know some, and this is also described in the tool window itself:

当前线程当前未运行代码或无法获取调用堆栈.

The current thread is not currently running code or the call stack could not be obtained.

这最后一部分通常会导致 Visual Studio 出错,这通常是在托管 (.NET) 程序调用 COM 或 P/Invoke 时发生的事情.完成此操作后,Visual Studio 不再能够查看该程序并弄清楚它在做什么,而当这恰好需要 60 秒以上时,它就会认为某些地方出了问题.

This last part is what usually trips up Visual Studio and this is usually something that happens when a managed (.NET) program calls into COM or P/Invoke. When this is done, Visual Studio is no longer able to look at the program and figure out what it is doing, and when this happens to take more than 60 seconds, it figures something has gone awry.

如果您的程序执行上述操作、调用 COM、或使用 P/Invoke 或涉及外部非托管 (.NET) 代码的内容,并且此外部代码执行时间超过 60 秒,则对话框基本上是误报.如果您知道这是良性的,例如是的,它调用外部代码,是的,这有时可能需要 60 秒以上",那么您可以删除该图像中下方的检查:

If your program is doing the above, calling into COM, or using P/Invoke, or something involving an external non-managed (.NET) code, and this external code takes more than 60 seconds to execute, then the dialog is basically a false positive. If you know that this is benign, as in, "yes, it calls external code, and yes, this may sometimes take more than 60 seconds" then you can remove the check lower in that image:

[ ] 抛出此异常类型时中断

[ ] Break when this exception type is thrown

请注意,它不一定例外,但这更多的是 Visual Studio 只是对可疑的(对其)行为发出警告.如果你知道这没问题,就告诉它停止升旗.

Note that it is not necessarily an exception though, this is more Visual Studio just raising a red flag on suspicious (to it) behavior. If you know this is OK, just tell it to stop raising that flag.

请注意,这与您在问题中提到的其他实际异常无关,只有大的 ContextSwitchDeadlock 对话框.

Note that this has nothing to do with the other real exceptions you mention in the question, only the big ContextSwitchDeadlock dialog.

这篇关于什么会导致/如何防止 ContextSwitchDeadlock?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆