Repository navigation
Closed
Description
Hi Guys,
Umbraco does not support parallel execution very well, while the ContentBlocksRenderer does exactly that during rendering. This is causing random failures in production for some of our customers. ViewComponents with dependencies for dictionary items for example will fail randomly when executed in parallel. Some of the exceptions that are thown at random in our environments are:
- System.ObjectDisposedException: Cannot access a disposed object.
- The Scope being disposed is not the Ambient Scope.This typically indicates that a child Scope was not disposed, or flowed to a child thread that was not awaited, or concurrent threads are accessing the same Scope(Ambient context) which is not supported.
- There is already an open DataReader associated with this Connection which must be closed first.
My fix to solve the issues:
I have not seen much performance drawbacks even with remote calls. Most requests perform similar and some are even faster.
/// <summary>
/// Override ContentBlocksRenderer so we can render components one after another as parrallel execution is not supported by Umbraco and can throw
/// System.ObjectDisposedException: Cannot access a disposed object.
/// The Scope being disposed is not the Ambient Scope.This typically indicates that a child Scope was not disposed, or flowed to a child thread that was not awaited, or concurrent threads are accessing the same Scope(Ambient context) which is not supported.
/// There is already an open DataReader associated with this Connection which must be closed first.
/// </summary>
public class ContentBlocksRenderer : IContentBlocksRenderer
{
private readonly IContentBlockDefinitionRepository _definitionRepository;
public ContentBlocksRenderer(IContentBlockDefinitionRepository definitionRepository)
{
_definitionRepository = definitionRepository;
}
/// <inheritdoc/>
public async Task<IHtmlContent> RenderAsync(IContentBlocks? contentBlocks, RenderViewComponentAsync renderViewComponentAsync, RenderPartialViewAsync renderPartialViewAsync, bool isBackOfficePreview = false)
{
if (contentBlocks is null)
{
return HtmlString.Empty;
}
var builder = new HtmlContentBuilder();
if (contentBlocks.Header is not null)
{
builder.AppendHtml(await RenderBlockAsync(contentBlocks.Header, renderViewComponentAsync, renderPartialViewAsync, isBackOfficePreview));
}
builder.AppendHtml(await RenderBlocksAsync(contentBlocks.Blocks, renderViewComponentAsync, renderPartialViewAsync, isBackOfficePreview));
return builder;
}
/// <inheritdoc/>
public async Task<IHtmlContent> RenderBlockAsync(IContentBlockViewModel? block, RenderViewComponentAsync renderViewComponentAsync, RenderPartialViewAsync renderPartialViewAsync, bool isBackOfficePreview = false)
{
if (block is null ||
_definitionRepository.GetById(block.DefinitionId) is not IContentBlockDefinition definition)
{
return HtmlString.Empty;
}
var blockHtml = await GetBlockHtml(block, definition, renderViewComponentAsync, renderPartialViewAsync);
if (isBackOfficePreview)
{
// Preview mode: add block id for scroll synchronisation before the block html
var blockIdAnchor = $"<a id=\"{block.Id}\" class=\"perplex-content-blocks-preview-anchor\"></a>";
return new HtmlContentBuilder()
.AppendHtml(blockIdAnchor)
.AppendHtml(blockHtml);
}
else
{
// No preview mode: block html only
return blockHtml;
}
}
/// <inheritdoc/>
public async Task<IHtmlContent> RenderBlocksAsync(IEnumerable<IContentBlockViewModel>? blocks, RenderViewComponentAsync renderViewComponentAsync, RenderPartialViewAsync renderPartialViewAsync, bool isBackOfficePreview = false)
{
if (blocks?.Any() != true)
{
return HtmlString.Empty;
}
var builder = new HtmlContentBuilder();
foreach (var block in blocks)
{
builder.AppendHtml(await RenderBlockAsync(block, renderViewComponentAsync, renderPartialViewAsync, isBackOfficePreview));
}
return builder;
}
private static async Task<IHtmlContent> GetBlockHtml(IContentBlockViewModel block, IContentBlockDefinition definition, RenderViewComponentAsync renderViewComponentAsync, RenderPartialViewAsync renderPartialViewAsync)
{
if (definition is IContentBlockDefinition<ViewComponent> componentDefinition)
{
Type viewComponentType = componentDefinition.GetType().GenericTypeArguments[0];
return await renderViewComponentAsync(viewComponentType, block);
}
else
{
var viewPath = definition.Layouts.FirstOrDefault(l => l.Id == block.LayoutId)?.ViewPath;
if (string.IsNullOrEmpty(viewPath))
{
return HtmlString.Empty;
}
return await renderPartialViewAsync(viewPath, block);
}
}
}
Metadata
Metadata
Assignees
Labels
No labels
Activity
PerplexDaniel commentedon Oct 13, 2025
Hi Ron,
Interesting, do you happen to have some code to reproduce this? That is, a view component with dependencies on dictionary items like you said? If I understand this correctly, just adding that block 10 or even 100 times to 1 page should probably trigger this then right? Since we indeed call all render tasks at the same time without
awaiting them individually. I am curious if there's anything in that code specifically because I have not heard of these issues internally and we use ContentBlocks a lot. We never use Umbraco's Dictionary feature though.I am not necessarily against just rendering every block 1 at a time,
awaiting each, but I wonder if you wouldn't have the same issues anyway if you have multiple requests to the same page simultaneously. Then renders will likely happen in parallel as well.Ron-Brouwer commentedon Oct 14, 2025
Hi Daniel,
Unfortunately not, in the end I got the error on a page pretty much every time but it had remote calls in the components as well (perhaps slowing thing down). Most of the time it's very random and It seems to be related to multiple threads creating/using scopes in parallel while that is not supported in Umbraco. The dictionary uses a scope for database access I guess. The SuppressFlow might be an option, but I'm not really familiar with that. I might me missing something obviously, but the provided code did solve my issue.
Exception:
The Scope being disposed is not the Ambient Scope. This typically indicates that a child Scope was not disposed, or flowed to a child thread that was not awaited, or concurrent threads are accessing the same Scope (Ambient context) which is not supported. If using Task.Run (or similar) as a fire and forget tasks or to run threads in parallel you must suppress execution context flow with ExecutionContext.SuppressFlow() and ExecutionContext.RestoreFlow().
Relevant stack:
.............
Umbraco.Cms.Web.Common.UmbracoHelper.GetDictionaryValueOrDefault(string key, CultureInfo specificCulture, string defaultValue)
.............
Perplex.ContentBlocks.Rendering.ContentBlocksRenderer.RenderAsync(IContentBlocks contentBlocks, RenderViewComponentAsync renderViewComponentAsync, RenderPartialViewAsync renderPartialViewAsync, bool isBackOfficePreview) in ContentBlocksRenderer.cs
.............
For additional info:
https://harrygordon.co.uk/blog/not-thread-safe-obviously/#:~:text=Investigating%20Umbraco%20scope
Umbraco docs:
The created scope becomes the ambient scope.
If an ambient scope already exists, it becomes the parent of the created scope.
When the created scope is disposed, the parent scope becomes the ambient scope again.
https://apidocs.umbraco.com/v10/csharp/api/Umbraco.Cms.Infrastructure.Scoping.IScopeProvider.html
namespace Umbraco.Cms.Infrastructure.Scoping
{
///
/// Implements .
///
/// Not thread-safe obviously.
internal class Scope : CoreScope, ICoreScope, IScope, Core.Scoping.IScope
Hope this helps!
PerplexDaniel commentedon Oct 14, 2025
Too bad there's no code to try and reproduce it, I may still try to reproduce this locally in the near future using the Dictionary feature combined with some remote calls. Good that you could replace the renderer with a version that fixes it for you so it is not blocking you at the moment.
Ron-Brouwer commentedon Oct 15, 2025
Hi Daniel,
Here is some code to show (and reproduce) at least one issue without all the noise. Just drop it somewhere and you'll get an exception. It simulates what you are doing and will fail for sure. Perhaps you can even fix it in Umbraco?
PerplexDaniel commentedon Oct 15, 2025
Thanks for that code, it indeed crashes reliably that way. There is a
Task.Runin there which is not needed (and our Renderer does not use it either) so I removed that in the version I used [1]. I also use 100ms instead of 1000ms so it's faster to test :-)Then I moved this code to a view component used by ContentBlocks in a project of ours but only execute it once instead of 10x [2] and added this block 4 times to 1 page. Loading that page then indeed randomly crashes about 50% of the time it's opened.
We have seen this "Not the ambient scope" exception in the past in background jobs etc but not yet in ContentBlocks but clearly it can occur and quite easily at that. I will look into releasing a patch (3.0.1) soon with a fix similar to the code you provided (without
Task.WhenAllin the Renderer). I'll update this issue when the new version is out, likely still this week.[1] Test code used on a random page
[2] View component used to test in ContentBlocks
Ron-Brouwer commentedon Oct 16, 2025
Sounds good to me!
Render blocks sequentially instead of concurrently with Task.WhenAll
PerplexDaniel commentedon Oct 16, 2025
@Ron-Brouwer Fixed in v3.0.1. Thank you for raising this issue and providing the code to reproduce it.
Ron-Brouwer commentedon Oct 16, 2025
Thanks Daniel, you're the best!