【问题标题】:Is protobuf-net thread safe?protobuf-net 线程安全吗?
【发布时间】:2013-06-10 09:17:19
【问题描述】:

我注意到,当我在多线程上下文中使用 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 是否意味着线程安全,或者这只是一个错误?

【问题讨论】:

  • 你描述的非常少见;如果你有一个可以重现这个的对象模型(即使只有 100 次),我很想看看它,看看什么是“up”

标签: c# protobuf-net


【解决方案1】:

Protobuf 的元数据检查不是线程安全的。这个错误是“罕见的”,但在并行完成的大量序列化中发生了很多。在我序列化大约 7000 万个对象的项目中,我遇到了这个确切的错误。您可以通过在序列化的 AHEAD 之前生成元数据来修复它:

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;

【讨论】:

    猜你喜欢
    • 2010-09-28
    • 2021-12-27
    • 2011-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-06
    相关资源
    最近更新 更多