Is protobuf-net thread safe?

I noticed that when I use protobuf-net in a multi-threaded context, it periodically breaks 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 block access to the protobuf-net serializer during the first serialization of a certain type, it works without crashing.

Is protobuf-net a reliable thread or is it just a bug?

+7
source share
1 answer

Protobuf metadata validation is not thread safe. This error is "rare", but a lot happens in huge serializations that run in parallel. I had this exact error in my project where I serialize about 70 million objects. You can fix this by creating AHEAD serialization metadata:

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

Make this code somewhere ahead of serialization, possibly a static constructor, for each of your custom types that are serialized.

You can also try increasing the Protobuf metadata validation timeout to try to help you, but in the event of a true deadlock in the Protobuf code, this simply delays your exception:

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

All Articles