简体   繁体   中英

Is protobuf-net thread safe?

I've noticed that when I use protobuf-net in a multi-threaded context it tends to fail intermittently with the following error:

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

However, if I lock access to the protobuf-net serializer the first time a particular type is serialized, it works without failing.

Is protobuf-net meant to be thread safe, or is this just a bug?

Protobuf's metadata inspection is not threadsafe. This error is "rare" but happens a lot in huge serializations that are done in parallel. I had this exact error in my project where I serialize approximately 70 million objects. You can fix it by generating the metadata AHEAD of serialization:

Serializer.PrepareSerializer<YourCustomType1>();
Serializer.PrepareSerializer<YourCustomType2>();

Do that code somewhere ahead of serialization, perhaps a static constructor, for each of your custom types that are serialized.

You can also try to increase Protobuf's metadata inspection timeout to try and aid you, but in the case of a true deadlock in the Protobuf code this really just delays your exception:

// Initialize Protobuf Serializer for 5 minutes of wait in the case of long-waiting locks
RuntimeTypeModel.Default.MetadataTimeoutMilliseconds = 300000;

While Haney's answer helped me when I first encountered thread-related issues with protobuf-net, I later noticed weird behavior when multiple threads were serializing our data structure. To be more precise, serialization still succeeded but it was not possible to correctly deserialize the ouput afterwards (which defies the purpose). The deserialized objects had all members at default/uninitialized value.

I ended up using SemaphoreSlim to make sure only 1 thread can access the serialization code at a time. With that fix, everything was behaving correctly (and I can live with the delay of waiting for the semaphore).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM