ConcurrentModificationException using KRYO

Hi everyone!

I’m currently using Kryo for serialization in my project.
While serializing I got ConcurrentModificationException.

At the Kryo support forums I found that it could happen when Kryo serializes some collection that’s being modified by one of the threads of my application.

I tried almost everything except making all (possible) collections synchronized.
I’m also thinking about using standard serialization more and more.

Can anybody tell if you ran across the similar problem or simply knows the answer,
how to fix?

Any help is greatly appreciated.

Synchronizing the individual operations on a collection (which is what the synchronizedX() methodsin Collections does) does not help there, because what happens is that an iterator created by Kryo fails-fast as it detects the collection being modified while it iterates over it.
One way is to synchronize your modification threads with the Kryo serialization thread, possibly by doing synchronized(lock), both around the place where you trigger Kryo serialization as well as any place where you possibly modify the lists, to lock on the same object.

I’ve solved problem by overriding some methods in Kryo and writing a bunch of serializers for my classes.