Skip to content

Prevent invalid concurrent batches #12917

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/Components/Components/src/Rendering/Renderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,12 @@ protected virtual void ProcessPendingRender()
private void ProcessRenderQueue()
{
EnsureSynchronizationContext();

if (_isBatchInProgress)
{
throw new InvalidOperationException("Cannot start a batch when one is already in progress.");
}

_isBatchInProgress = true;
var updateDisplayTask = Task.CompletedTask;

Expand Down
40 changes: 40 additions & 0 deletions src/Components/Components/test/RendererTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3395,6 +3395,27 @@ public void EventFieldInfoWorksWhenEventHandlerIdWasSuperseded()
}
}

[Fact]
public void CannotStartOverlappingBatches()
{
// Arrange
var renderer = new InvalidRecursiveRenderer();
var component = new CallbackOnRenderComponent(() =>
{
// The renderer disallows one batch to be started inside another, because that
// would violate all kinds of state tracking invariants. It's not something that
// would ever happen except if you subclass the renderer and do something unsupported
// that commences batches from inside each other.
renderer.ProcessPendingRender();
});
var componentId = renderer.AssignRootComponentId(component);

// Act/Assert
var ex = Assert.Throws<InvalidOperationException>(
() => renderer.RenderRootComponent(componentId));
Assert.Contains("Cannot start a batch when one is already in progress.", ex.Message);
}

private class NoOpRenderer : Renderer
{
public NoOpRenderer() : base(new TestServiceProvider(), NullLoggerFactory.Instance)
Expand Down Expand Up @@ -4109,5 +4130,24 @@ protected override void BuildRenderTree(RenderTreeBuilder builder)
private class DerivedEventArgs : EventArgs
{
}

class CallbackOnRenderComponent : AutoRenderComponent
{
private readonly Action _callback;

public CallbackOnRenderComponent(Action callback)
{
_callback = callback;
}

protected override void BuildRenderTree(RenderTreeBuilder builder)
=> _callback();
}

class InvalidRecursiveRenderer : TestRenderer
{
public new void ProcessPendingRender()
=> base.ProcessPendingRender();
}
}
}