mixed-reality-extension-sdk icon indicating copy to clipboard operation
mixed-reality-extension-sdk copied to clipboard

[Unity] Simultaneously loaded asset copies break cache refcounting

Open stevenvergenz opened this issue 4 years ago • 0 comments

If you load the same asset an even number of times concurrently, the load will complete, but the asset will be garbage collected and released 30 seconds later.

Relevant Unity API: https://github.com/microsoft/mixed-reality-extension-unity/blob/master/MREUnityRuntime/MREUnityRuntimeLib/PluginInterfaces/IAssetCache.cs

Theory: Cache contents are checked before the load begins, then assigned after the load completes. This leaves a window for multiple loads to be kicked off before the first one completes. The first one to complete will write its result into the cache. The second to complete is interpreted as returning leased assets, and dereferences the first, leading to the garbage collection.

Option 1: Save the asset load task to the cache when a load starts. When reading from the cache (LeaseAssets, GetVersion), wait for this task to complete before returning. This means that cache loads are always asynchronous, so the synchronous API would need to be removed.

Option 2: Separate out the "return leased assets" and the "store freshly loaded assets" features of StoreAssets into two separate functions. Returning assets that aren't in the cache is a no-op, as is storing duplicate loaded assets.

stevenvergenz avatar Sep 03 '20 21:09 stevenvergenz