Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GraphicsDevice.EffectCache is not threadsafe #6837

Open
dmanning23 opened this issue Aug 3, 2019 · 0 comments

Comments

@dmanning23
Copy link
Contributor

commented Aug 3, 2019

I have a project where it is loading content in different threads. Very occasionally, it will crash when loading Effect objects:

System.ArgumentException
  HResult=0x80070057
  Message=An item with the same key has already been added.
  Source=mscorlib
  StackTrace:
   at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
   at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
   at Microsoft.Xna.Framework.Graphics.Effect..ctor(GraphicsDevice graphicsDevice, Byte[] effectCode, Int32 index, Int32 count)
   at Microsoft.Xna.Framework.Content.ContentTypeReader`1.Read(ContentReader input, Object existingInstance)
   at Microsoft.Xna.Framework.Content.ContentReader.InnerReadObject[T](T existingInstance)
   at Microsoft.Xna.Framework.Content.ContentReader.ReadObject[T]()
   at Microsoft.Xna.Framework.Content.ContentReader.ReadAsset[T]()
   at Microsoft.Xna.Framework.Content.ContentManager.ReadAsset[T](String assetName, Action`1 recordDisposableObject)
   at Microsoft.Xna.Framework.Content.ContentManager.Load[T](String assetName)
   at BloomBuddy.Bloom.LoadContent(SpriteBatch spriteBatch, ContentManager content, GraphicsDeviceManager device)
...

Chased it down to this line of code:

graphicsDevice.EffectCache.Add(effectKey, cloneSource);

Effects are being cached in the GraphicsDevice object, but there is no thread protection around that cache. There are two solutions I can see:

  1. Add a lock around access to the EffectCache
  2. Move the EffectCache into ContentManager. That would be the logical place to cache it.

Super appreciate all the work yall are putting into MonoGame.
Cheers!

What version of MonoGame does the bug occur on:

  • MonoGame 3.7

What operating system are you using:

  • Windows, Android, iOS

What MonoGame platform are you using:

  • WindowsDX, Android, iOS
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.