jlamfers/RazorMachine

Adding MemoryContentProvider is not thread-safe

Opened this issue · 2 comments

The piece of code causing the issue at RazorMachine.cs

        protected MemoryContentProvider MemoryContentProvider {
            get {
                var provider = Context.TemplateFactory.ContentManager.TryGetContentProvider<MemoryContentProvider>();
                if (provider == null)
                    Context.TemplateFactory.ContentManager.AddContentProvider(provider = new MemoryContentProvider());
                return provider;
            }
        }

When two threads reach the getter at the same time (using one RazorMachine instance) it is possible that both of them add new MemoryContentProvider to the context and each next call to Context.TemplateFactory.ContentManager.TryGetContentProvider() will throw an exception (SingleOrDefault at CompositeContentProvider.TryGetContentProvider(Type type)).

This situation is possible only when calling the MemoryContentProvider getter first time, but this can leave the RazorMachine object in unusable state.

Is there already a solution for this problem?

I'm not using the latest version, but I don't think so.
You can add lock statement in the source code above and build the library on your own, but I've done this on higher level to make the change quicker.