Skip to content
Closed
Changes from 2 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
10 changes: 5 additions & 5 deletions src/Controls/src/Core/BindableObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,10 @@ public static void SetInheritedBindingContext(BindableObject bindable, object va
}
else
{
bindable._inheritedContext = new WeakReference(value);
if (bindable._inheritedContext is not null)
bindable._inheritedContext.Target = value;
else
bindable._inheritedContext = new WeakReference(value);
bindable.ApplyBindings(fromBindingContextChanged: true);
bindable.OnBindingContextChanged();
}
Expand Down Expand Up @@ -692,11 +695,8 @@ private protected virtual void OnBindablePropertySet(BindableProperty property,

void ApplyBindings(bool fromBindingContextChanged)
{
var prop = _properties.Values.ToArray();

for (int i = 0, propLength = prop.Length; i < propLength; i++)
foreach (var context in _properties.Values)

Copilot AI Mar 11, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Iterating directly over _properties.Values here is not safe: ApplyBinding ultimately calls into BindingBase.Apply/BindingExpression.ApplyCore, which can invoke SetValueCore and property-changed callbacks. Those callbacks can add/remove bindable property contexts (modifying _properties) and will cause the dictionary enumerator to throw InvalidOperationException mid-iteration. The previous ToArray() snapshot avoided this; consider keeping a snapshot (potentially via pooled/rented array) or using a guarded pattern (e.g., retry or fallback snapshot when a modification is detected) so BindingContext propagation can’t crash on re-entrant property changes.

Suggested change
foreach (var context in _properties.Values)
var contexts = _properties.Values.ToArray();
foreach (var context in contexts)

Copilot uses AI. Check for mistakes.

Copilot AI Mar 11, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change alters ApplyBindings iteration semantics in a way that can be sensitive to re-entrant property changes. It would be helpful to add a unit test which triggers a binding application that, via a propertyChanged callback (or similar), adds/removes a different BindableProperty context while ApplyBindings is running, and asserts no InvalidOperationException is thrown.

Suggested change
foreach (var context in _properties.Values)
// Take a snapshot of the current property contexts so that re-entrant
// modifications to the _properties collection (e.g., via propertyChanged
// callbacks triggered during ApplyBinding) do not invalidate the enumerator.
var propertyContexts = _properties.Values.ToArray();
foreach (var context in propertyContexts)

Copilot uses AI. Check for mistakes.
{
BindablePropertyContext context = prop[i];
if (ReferenceEquals(context.Property, BindingContextProperty))
{
// BindingContextProperty Binding is handled separately within SetInheritedBindingContext
Expand Down
Loading