Tag: deadlock

Protobuf.netexception – 检查元数据时超时

尝试使用protobuf.net反序列化对象时, 有时会收到以下exception。 我很惊讶,因为我从来没有多个线程同时反序列化同一个对象,并且protobuf.net源似乎没有使用任何静态对象进行反序列化。 exception确实提出了一个解决方案,但我不确定如何实现,所以欢迎一个例子。 Base Exception Type: System.TimeoutException: Timeout while inspecting metadata; this may indicate a deadlock. This can often be avoided by preparing necessary serializers during application initialization, rather than allowing multiple threads to perform the initial metadata inspection at ProtoBuf.Meta.RuntimeTypeModel.TakeLock(Boolean& lockTaken) at ProtoBuf.Meta.RuntimeTypeModel.FindOrAddAuto(Type type, Boolean demand, Boolean addWithContractOnly, Boolean addEvenIfAutoDisabled) at ProtoBuf.Meta.RuntimeTypeModel.GetKey(Type type, […]

exception抛出后,Boost共享互斥锁未释放

我在预先存在的.NET(C#,3.5)应用程序中遇到了一个奇怪的Boost(v1.38)互斥锁死锁,该应用程序调用C ++库。 在获得读取锁定之后的某个时刻[正确]抛出一个exception,并且该exception一直未处理回托管.NET代码(处理它的位置)。 尝试使用setter方法的c ++库的下一次调用会无限期地挂起唯一的锁定获取(可能是未释放读取锁定): ntdll.dll!NtWaitForSingleObject() + 0x15 bytes kernel32.dll!WaitForSingleObjectEx() + 0x43 bytes kernel32.dll!WaitForSingleObject() + 0x12 bytes OurCPPLib.dll!boost::shared_mutex::unlock_upgrade_and_lock() Line 478 + 0x11 bytes C++ OurCPPLib.dll!boost::unique_lock::unique_lock(boost::detail::thread_move_t<boost::upgrade_lock > other) Line 788 C++ OurCPPLib.dll!boost::upgrade_to_unique_lock::upgrade_to_unique_lock(boost::upgrade_lock & m_) Line 802 + 0x98 bytes C++ OurCPPLib.dll!OurClass::SetSomething(double something) Line 95 C++ 该类定义了许多Get和Set方法(读者/编写者)并像这样实现它们: boost::shared_mutex _calcSharedMutex; RETURNCODE GetSomething(double& something) { boost::shared_lock lock(_calcSharedMutex); return _anotherObject->GetSomething(something); […]

同步到异步调度:如何避免死锁?

我正在尝试创建一个具有同步方法的类,并调用其他一些异步的库方法。 出于这个原因,我使用Task.Result等待异步操作完成。 我的方法由WPF app以同步方式调用。 这导致僵局。 我知道最好的方法是使我的所有方法都异步,但我的情况要求它们是同步的。 另一方面,他们使用异步的其他库。 我的问题是:如何在这种情况下避免陷入僵局? 重现步骤: 用户点击应用中的按钮(方法Button1_OnClick ) 此方法创建IPlugin的实例,然后调用其方法RequestSomething() 然后,此方法以这种方式调用异步库: asyncTarget.MethodFromAsyncLibrary(“HelloFromPlugin”).Result 该库调用它的方法NotifyNewValueProgressAsync() NotifyNewValueProgressAsync()将调用委托给WPF应用程序 由于UI上下文被此行阻止了asyncTarget.MethodFromAsyncLibrary(“HelloFromPlugin”).Result步骤5中的回调导致死锁。 请参阅下面的代码示例和相关注释: public class SyncAdapterPlugin : IPlugin, IProgressAsyncHandler { //Constructor and fields are omitted here //This method is called from UI context by WPF application and it delegates synchronous call to asynchronous method string IPlugin.RequestSomething() { //In order to […]

使用nUnit测试死锁

我是unit testing和nUnit(2.48)的新手。 我想写一个测试方法,其中失败的情况是它死锁。 这可能吗? 显然,nUnit默认不知道该方法应该执行多长时间,因此我必须编写代码来在单独的线程上完成工作然后中止它并抛出exception(如果花费的时间超过我定义的时间)? 有一个更好的方法吗? 谢谢

即使在Asp.Net流中使用ConfigureAwait(false)后死锁也是如此

即使在使用ConfigureAwait(false)后我也遇到了死锁,下面是示例代码。 根据示例http://blog.stephencleary.com/2012/02/async-and-await.html(#Avoding Context),这不应该达到死锁。 这是我的class级 : public class ProjectsRetriever { public string GetProjects() { … var projects = this.GetProjects(uri).Result; … … } private async Task<IEnumerable> GetProjects(Uri uri) { return await this.projectSystem.GetProjects(uri, Constants.UserName).ConfigureAwait(false); } } 该类来自共享库: public class ProjectSystem { public async Task<IEnumerable> GetProjects(Uri uri, string userName) { var projectClient = this.GetHttpClient(uri); var projects = await projectClient.GetProjects(); […]

为什么不调用Task .Result死锁?

几个月前阅读这篇文章之后,我变得很擅长获取Task的Result ,并且通过ConfigureAwait(false)或Task.Run不断地将所有调用包裹Task.Run 。 但是,由于某种原因,以下代码成功完成: public static void Main(string[] args) { var arrays = DownloadMany(); foreach (var array in arrays); } IEnumerable DownloadMany() { string[] links = { “http://google.com”, “http://microsoft.com”, “http://apple.com” }; using (var client = new HttpClient()) { foreach (var uri in links) { Debug.WriteLine(“Still here!”); yield return client.GetByteArrayAsync(uri).Result; // Why doesn’t this deadlock? } […]

C#中的线程死锁示例

有人能给出一个如何在C#语言中引起线程死锁的例子吗?