UnitySerializationUtility.DeserializeUnityObject() is not thread-safe

Issue #504 new
任才 created an issue

What happened?

When we load asset bundles both sync and async, the game crash in the deserialization process of Odin.

How can we reproduce it?

Load different large asset bundles with SerializedMonoBehaviour on them, both sync and async. Ensure SerializedMonoBehaviour.OnAfterDeserialize() to be called both on the main thread and the UnityPreload thread roughly at the same time.

If screenshots would help explain or demonstrate your issue, please include these.

None.

What version of Unity are you using?

2017.4.20f1

What version of Odin are you using? (See "Tools > Odin Inspector > About")

2.0.13.0

Do you have Editor Only mode enabled?

No

What operating system are you on?

Windows 10 x64

How to solve this issue?

Since OnAfterDeserialize() may be called both from main thread and the UnityPreload thread, the crash suggest that there may be some race condition inside the Odin deserialization process. A critical section can be handy at this point.

This is what I did. I switched my Odin into source code mode, changed UnitySerializationUtility.cs:

Before:

public static class UnitySerializationUtility
{
    public static void DeserializeUnityObject(UnityEngine.Object unityObject, ref SerializationData data, DeserializationContext context = null)
    {
        DeserializeUnityObject( unityObject, ref data, context, isPrefabData: false, prefabInstanceUnityObjects: null);
    }
}

After:

public static class UnitySerializationUtility
{
    private static Mutex _deserializeMutex = new Mutex();

    public static void DeserializeUnityObject(UnityEngine.Object unityObject, ref SerializationData data, DeserializationContext context = null)
    {
        _deserializeMutex.WaitOne();
        DeserializeUnityObject( unityObject, ref data, context, isPrefabData: false, prefabInstanceUnityObjects: null);
        _deserializeMutex.ReleaseMutex();
    }
}

Comments (2)

  1. Log in to comment