我注意到,当我在多线程上下文中使用protobuf-net时,它往往会间歇性地失败,并出现以下错误:
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但是,如果我在第一次序列化特定类型时锁定对protobuf-net序列化程序的访问,它将正常工作而不会失败。
protobuf-net是否意味着线程安全,或者这只是一个bug?
发布于 2013-06-14 04:30:13
Protobuf的元数据检查不是threadsafe。这种错误是“罕见的”,但在并行完成的大型序列化中经常发生。我在我的项目中遇到了这个错误,我序列化了大约7000万个对象。您可以通过在序列化之前生成元数据来修复它:
Serializer.PrepareSerializer<YourCustomType1>();
Serializer.PrepareSerializer<YourCustomType2>();在序列化之前的某个地方编写代码,可能是一个静态构造函数,用于序列化的每个自定义类型。
您也可以尝试增加Protobuf的元数据检查超时来帮助您,但在Protobuf代码中出现真正的死锁的情况下,这实际上只会延迟您的异常:
// Initialize Protobuf Serializer for 5 minutes of wait in the case of long-waiting locks
RuntimeTypeModel.Default.MetadataTimeoutMilliseconds = 300000;https://stackoverflow.com/questions/17096359
复制相似问题