Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 706
jmcg Avatar asked Jun 13 '13 20:06

jmcg


1 Answers

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;
like image 121
Haney Avatar answered Oct 03 '22 07:10

Haney