Repository navigation
Description
Description
Microsoft.Maui.Controls.Element tracks whether a bindable-property handler update is pending
using a per-instance HashSet<string> field, _pendingHandlerUpdatesFromBPSet. The Add →
base call → Remove bracket is only safe under the assumption that SetValue on a given
Element is always called from a single thread:
src/Controls/src/Core/Element/Element.cs (MAUI main @ 013e8bfb1d):
HashSet<string> _pendingHandlerUpdatesFromBPSet = new HashSet<string>(); // :701
private protected override void OnBindablePropertySet(BindableProperty property, object original,
object value, bool changed, bool willFirePropertyChanged)
{
if (willFirePropertyChanged)
_pendingHandlerUpdatesFromBPSet.Add(property.PropertyName); // :706
base.OnBindablePropertySet(property, original, value, changed, willFirePropertyChanged);
_pendingHandlerUpdatesFromBPSet.Remove(property.PropertyName); // :710
UpdateHandlerValue(property.PropertyName, changed);
}
protected override void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
bool waitForHandlerUpdateToFireFromBP =
_pendingHandlerUpdatesFromBPSet.Contains(propertyName); // :730
...
}That assumption doesn't hold: nothing prevents two threads from calling SetValue on the same
Element concurrently (a view-model updating a property from a background thread while an
animation or another property write happens on the UI thread is an ordinary pattern, not a
documented misuse). When that happens, HashSet<string> — which is not thread-safe — is mutated
concurrently and its internal state is corrupted. The corruption surfaces later, from inside the
HashSet's own code, as one of two exceptions that name neither the property being written nor
the thread that wrote it:
System.IndexOutOfRangeExceptionfromHashSet.AddIfNotPresentSystem.InvalidOperationException: "Operations that change non-concurrent collections must have exclusive access. A concurrent update was performed on this collection and corrupted its state..."
A single-threaded HashSet essentially never throws either of these, which is what makes both
signatures diagnostic of concurrent corruption rather than an app-level logic error. We hit both
signatures in production (trimmed + AOT Release build) with no informative stack beyond
Element.OnBindablePropertySet, and built the attached repro to isolate the mechanism.
Suggested direction
The intended lifetime of this state is one synchronous call on one thread, so the natural fix is
to stop storing it per-instance. Thread-local storage removes the race by construction — no lock,
no contention — and also removes one HashSet<string> allocation per Element, which is already
scoped as DS06 in the memory-allocation issue (#34149).
One interaction is worth surfacing rather than glossing over, because it shapes what the right
storage actually is: the Remove at Element.cs:710 is unconditional. It runs even when
willFirePropertyChanged was false and nothing was added, so the marker is already unbalanced
with respect to re-entrancy — a nested SetValue for the same property name clears the outer
call's marker today, single-threaded, per element. A bare [ThreadStatic] static HashSet<string>
would fix the race and the allocation but widen that existing aliasing from the same element to
any element on that thread. Storing a depth count, or keying on the (element, property) pair,
would avoid the race without widening it.
Two sibling collections on BindableObject appear to follow the same unsynchronized-per-instance
pattern, but nothing in this repro reproduces against them and this issue makes no claim about
them.
Does the bug reproduce also in a native app?
N/A — the defect is in Microsoft.Maui.Controls managed code (Element.cs), not in either
platform's native UI toolkit. The two platform-native reference points (Android's
ViewRootImpl.checkThread, iOS's UIKitThreadAccessException) are shown above specifically
because they demonstrate how a platform-mapped property can mask this bug rather than exhibit
it, which is why scenario A deliberately avoids any mapped property.
Steps to Reproduce
- Clone the linked repro project and deploy
BindablePropertyConcurrencyReproto a physical
Android device (or an iOS device/simulator). - Press A, then B. Each button runs a bounded 30 s stress loop against a single shared
Labeland reports CLEAN or THREW, with the caught exception's fullToString()
rendered below the status line.
| Button | What it does |
|---|---|
| A — Isolated mechanism | Two Task.Run loops writing two different custom bindable properties on the same element. Neither property is in any platform property mapper, so no platform view is touched on any thread — this isolates the HashSet race from any platform thread-affinity check. |
| B — Unmarshalled framework path | One background loop calling VisualStateManager.GoToState, racing a fade animation driven by the framework's own ticker on the UI thread. The app makes exactly one off-thread call — GoToState is not documented as UI-thread-only, takes no dispatcher, does not marshal, and driving it from view-model state is an ordinary MAUI pattern. |
A is the isolation; B is the complaint. A deliberately has the app write two bindable
properties concurrently — that's the app's mistake, and its only job is to show the mechanism
with nothing else in the frame. B is the case that does not require the app to do anything
unusual: one off-thread call to a documented, non-UI-thread-only API, racing the framework's own
UI-thread animation. There is no documented rule the app broke in B.
Expected: an off-thread bindable-property write may lose an update or produce a platform
thread-affinity error, but must not corrupt framework-internal state or throw from a HashSet
operation unrelated to the property being written.
Actual: IndexOutOfRangeException / InvalidOperationException thrown from inside
HashSet<string> on the Element.OnBindablePropertySet path, naming neither the property nor
the thread involved.
Link to public reproduction project repository
https://github.com/brunck/reproductions/tree/master/BindablePropertyConcurrencyRepro
Version with bug
10.0.90
Is this a regression from previous behavior?
Not sure, did not test other versions
Last version that worked well
Unknown/Other
Affected platforms
Android, iOS
Affected platform versions
- Android 16 (Pixel 8a, physical device)
- iOS (iPhone 11, physical device)
Did you find any workaround?
No. The only mitigation is avoiding concurrent bindable-property writes to the same Element
entirely — including the framework's own VisualStateManager.GoToState racing an animation,
which is not something app code visibly controls.
Relevant log output
Pixel 8a, Android 16, 9 cores, Release build (trimmed + AOT, matching the configuration our
production crash occurred in), Microsoft.Maui.Controls 10.0.90+8e2547a4707f745a27a7791495b240e756926980.
| Scenario | Result | Time to failure | Exception |
|---|---|---|---|
| A | THREW, 3/3 consecutive runs from a fresh process | 0.02 s, 0.03 s, 0.02 s | IndexOutOfRangeException and InvalidOperationException, both in HashSet.AddIfNotPresent |
| B | THREW, 3/3 consecutive runs from a fresh process | 0.68 s, 0.56 s, 0.67 s | InvalidOperationException ×2 and IndexOutOfRangeException ×1, all in HashSet.AddIfNotPresent; thrown on the background thread each time |
Both production signatures appear across three runs of B alone, and again across three runs
of A, all from the same unchanged binary — one root cause surfaces as two unrelated-looking
exceptions, neither of which names the property, the thread, or the corrupted collection. B's
three runs were each thrown on the background GoToState thread while the animation ran on the
UI thread.
iOS (single run per scenario, physical device, Microsoft.Maui.Controls 10.0.90):
| Scenario | Result | Time to failure | Exception |
|---|---|---|---|
| A | THREW | 0.01 s | InvalidOperationException — one of the same two production signatures as Android |
| B | THREW, caught | 0.00 s | UIKit.UIKitThreadAccessException, on the background GoToState thread |
| B | THREW, caught, ×25 consecutive presses from a fresh launch — no termination | 0.00 s each | UIKit.UIKitThreadAccessException every time; B alone does not terminate the process |
| A/B alternating, 5 presses each | PROCESS TERMINATED, uncaught | — | IndexOutOfRangeException in HashSet.AddIfNotPresent, on thread 1, in the layout pass — see follow-up comment |
| B (earlier harness build) | PROCESS TERMINATED, uncaught | — | InvalidOperationException in HashSet.AddIfNotPresent, on thread 1, in the animation ticker |
A corroborates cross-platform: with no platform view in the frame, iOS produces
InvalidOperationException from the same HashSet corruption as Android, confirming the defect
lives in platform-agnostic Controls code, not in either platform's binding to it.
Corrected — see the comment below. This paragraph originally said B does not corroborate on iOS. That was wrong: B does corroborate, and the iOS failure is the most severe one in this repro.
B corroborates on iOS, but asymmetrically. GoToState's state transition applies/unapplies Setters that write TextColor, which is platform-mapped; on iOS that reaches UILabel, so UIKit's own thread-affinity check fires on the background thread as UIKitThreadAccessException and the scenario catches it at 0.00 s. What that check does not do is prevent the corruption — the off-thread SetValue still mutates _pendingHandlerUpdatesFromBPSet before UIKit stops it, concurrently with the UI thread's animation writing Opacity to the same element. The UI thread then trips over the corrupted set inside MAUI's animation ticker, with no app frame on the stack, and the process terminates. The platform check only changes which exception the app sees first; the real corruption lands later, elsewhere, fatally. Full trace in the comment below.
This is still useful context in one respect: UIKit's check is a named, documented exception that identifies the actual violation, whereas MAUI's own Controls layer has no equivalent for _pendingHandlerUpdatesFromBPSet — the failure mode there is an unrelated HashSet exception that names neither the property, nor the thread, nor the corrupted collection.
We also expected the corruption might present as SIGSEGV/SIGABRT with no managed stack under
full AOT; instead every iOS failure — including the uncaught one that terminates the process — produced a complete managed stack with a normal ToString(), no different in kind from Android.
Android — A, IndexOutOfRangeException
System.IndexOutOfRangeException: Arg_IndexOutOfRangeException
at System.Collections.Generic.HashSet`1[[System.String, ...]].AddIfNotPresent(String , Int32& )
at System.Collections.Generic.HashSet`1[[System.String, ...]].Add(String )
at Microsoft.Maui.Controls.Element.OnBindablePropertySet(BindableProperty property, Object original, Object value, Boolean changed, Boolean willFirePropertyChanged)
at Microsoft.Maui.Controls.BindableObject.SetValueActual(BindableProperty property, BindablePropertyContext context, Object value, Boolean currentlyApplying, SetValueFlags attributes, SetterSpecificity specificity, Boolean silent)
at Microsoft.Maui.Controls.BindableObject.SetValueCore(BindableProperty property, Object value, SetValueFlags attributes, SetValuePrivateFlags privateAttributes, SetterSpecificity specificity)
at Microsoft.Maui.Controls.BindableObject.SetValue(BindableProperty property, Object value)
at BindablePropertyConcurrencyRepro.RaceLabel.set_RaceBeta(Int32 value)Android — A, InvalidOperationException, same binary, same button
System.InvalidOperationException: InvalidOperation_ConcurrentOperationsNotSupported
at System.Collections.Generic.HashSet`1[[System.String, ...]].AddIfNotPresent(String , Int32& )
at System.Collections.Generic.HashSet`1[[System.String, ...]].Add(String )
at Microsoft.Maui.Controls.Element.OnBindablePropertySet(BindableProperty property, Object original, Object value, Boolean changed, Boolean willFirePropertyChanged)
at Microsoft.Maui.Controls.BindableObject.SetValueActual(...)
at Microsoft.Maui.Controls.BindableObject.SetValueCore(...)
at Microsoft.Maui.Controls.BindableObject.SetValue(BindableProperty property, Object value)
at BindablePropertyConcurrencyRepro.RaceLabel.set_RaceBeta(Int32 value)Android — B, via VisualStateManager.GoToState, one off-thread call
System.IndexOutOfRangeException: Arg_IndexOutOfRangeException
at System.Collections.Generic.HashSet`1[[System.String, ...]].AddIfNotPresent(String , Int32& )
at System.Collections.Generic.HashSet`1[[System.String, ...]].Add(String )
at Microsoft.Maui.Controls.Element.OnBindablePropertySet(BindableProperty property, Object original, Object value, Boolean changed, Boolean willFirePropertyChanged)
at Microsoft.Maui.Controls.BindableObject.ClearValueCore(BindableProperty property, SetterSpecificity specificity)
at Microsoft.Maui.Controls.BindableObject.ClearValue(BindableProperty property, SetterSpecificity specificity)
at Microsoft.Maui.Controls.Setter.UnApply(BindableObject target, SetterSpecificity specificity)
at Microsoft.Maui.Controls.VisualStateManager.GoToState(VisualElement visualElement, String name)iOS — A, InvalidOperationException
System.InvalidOperationException: Operations that change non-concurrent collections must have exclusive access. A concurrent update was performed on this collection and corrupted its state. The collection's state is no longer correct.
at System.Collections.Generic.HashSet`1[[System.String, System.Private.CoreLib, Version=10.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].AddIfNotPresent(String value, Int32& location)
at Microsoft.Maui.Controls.Element.OnBindablePropertySet(BindableProperty property, Object original, Object value, Boolean changed, Boolean willFirePropertyChanged)
at Microsoft.Maui.Controls.BindableObject.SetValueActual(BindableProperty property, BindablePropertyContext context, Object value, Boolean currentlyApplying, SetValueFlags attributes, SetterSpecificity specificity, Boolean silent)
at Microsoft.Maui.Controls.BindableObject.SetValueCore(BindableProperty property, Object value, SetValueFlags attributes, SetValuePrivateFlags privateAttributes, SetterSpecificity specificity)
at Microsoft.Maui.Controls.BindableObject.SetValue(BindableProperty property, Object value)
at BindablePropertyConcurrencyRepro.RaceLabel.set_RaceAlpha(Int32 value) in RaceLabel.cs:line 28
at BindablePropertyConcurrencyRepro.MainPage.<>c__DisplayClass4_0.<RunScenarioA>b__0(CancellationToken token) in MainPage.xaml.cs:line 47
at BindablePropertyConcurrencyRepro.RunContext.<>c__DisplayClass20_0.<Background>b__0() in RunContext.cs:line 43(Remaining frames scrolled off-screen in the on-device capture; the frames above are the
diagnostic ones — same HashSet.AddIfNotPresent → Element.OnBindablePropertySet chain as
Android.)
iOS — B, masked by UIKitThreadAccessException (never reaches the HashSet)
UIKit.UIKitThreadAccessException: UIKit Consistency error: you are calling a UIKit method that can only be invoked from the UI thread.
at UIKit.UIApplication.EnsureUIThread() in /Users/cloudtest/vss/_work/1/s/macios/src/UIKit/UIApplication.cs:line 133
at UIKit.UILabel.set_TextColor(UIColor value) in /Users/cloudtest/vss/_work/1/s/macios/src/build/dotnet/ios/generated-sources/UIKit/UILabel.g.cs:line 812
at Microsoft.Maui.Platform.LabelExtensions.UpdateTextColor(UILabel platformLabel, ITextStyle textStyle, UIColor defaultColor)
at Microsoft.Maui.Handlers.LabelHandler.MapTextColor(ILabelHandler handler, ILabel label)
at Microsoft.Maui.PropertyMapper`2[...].<Add>b__0(IElementHandler h, IElement v)
at Microsoft.Maui.Controls.Label.MapTextColor(ILabelHandler handler, Label label, Action`2 baseMethod)
at Microsoft.Maui.PropertyMapperExtensions.<>c__DisplayClass1_0`2[...].<ModifyMapping>g__newMethod|0(IElementHandler handler, IElement view)
at Microsoft.Maui.PropertyMapper`2[...].<Add>b__0(IElementHandler h, IElement v)
at Microsoft.Maui.PropertyMapper.TryUpdatePropertyCore(String key, IElementHandler viewHandler, IElement virtualView)
at Microsoft.Maui.PropertyMapper.UpdateProperty(IElementHandler viewHandler, IElement virtualView, String property)
at Microsoft.Maui.Handlers.ElementHandler.UpdateValue(String property)
at Microsoft.Maui.Controls.BindableObject.SetValueActual(BindableProperty property, BindablePropertyContext context, Object value, Boolean currentlyApplying, SetValueFlags attributes, SetterSpecificity specificity, Boolean silent)
at Microsoft.Maui.Controls.BindableObject.SetValueCore(BindableProperty property, Object value, SetValueFlags attributes, SetValuePrivateFlags privateAttributes, SetterSpecificity specificity)
at Microsoft.Maui.Controls.BindableObject.SetValue(BindableProperty property, Object value, SetterSpecificity specificity)
at Microsoft.Maui.Controls.Setter.Apply(BindableObject target, SetterSpecificity specificity)(A few generic-type-argument frames collapsed to [...] for readability; nothing diagnostic was
in them. This is the concrete illustration of "B is masked on iOS": the call never gets past
UILabel.set_TextColor to reach Element.OnBindablePropertySet/the HashSet at all, unlike
Android where the race wins and the real corruption surfaces instead.)
Activity
brunck commentedon Aug 14, 2026
May be the same as this issue.
brunck commentedon Aug 14, 2026
Correction: scenario B does reproduce on iOS, and the iOS failure is worse than what I originally filed
Two statements in the original post are wrong, and both understate this. I've corrected the repro README and I'm recording the correction here rather than silently editing.
What I got wrong. I saw
UIKitThreadAccessExceptionthrown on the background thread at 0.00 s and concluded that UIKit's thread-affinity check was stopping the write before it could corrupt the set. It isn't. The check stops that call on that thread. It does not prevent the corruption and it does not protect the collection — it only changes which exception the app happens to see first.What actually happens. Scenario B makes one off-thread
VisualStateManager.GoToStatecall while the UI thread runs aFadeToon the same element. The off-threadSetValuestill mutates_pendingHandlerUpdatesFromBPSetbefore UIKit's check fires. Run B repeatedly and the UI thread is the one that trips over the corrupted set — inside MAUI's own animation ticker. The process terminates.Three things about that stack:
Program.Mainand the throw there is not one line of application code.ViewExtensions.FadeToAsync→Animation→Tweener→AnimationManager.OnFire→PlatformTicker→NSActionDispatcheris framework code start to finish.UIApplication.Main. There is nothing for an app to catch. On Android the equivalent throw can at least be swallowed via theHandledflag onAndroidEnvironment.UnhandledExceptionRaiser; iOS has no equivalent, sinceObjCRuntime.Runtime.MarshalManagedExceptioncan change how the exception crosses into native code but cannot suppress it.So the severity is higher than what I filed. A single off-thread
GoToState— a method that is not documented as UI-thread-only, takes no dispatcher, and does not marshal — can terminate an iOS app from inside MAUI's animation ticker, with no app frame on the stack and no exception the app is able to catch.Caveat, stated plainly: unlike the Android numbers in the original post, I don't have a clean run count for this one. It was reproduced on a physical device by pressing B repeatedly across several consecutive runs, not deterministically on a first press. Happy to quantify it if that's useful for triage.
Finally, correcting the AOT note in the original post: the expectation that this might present as
SIGSEGV/SIGABRTwith no managed stack under full AOT still did not hold — even the uncaught, process-terminating failure produced a complete managed stack.brunck commentedon Aug 14, 2026
Follow-up on iOS: correcting the reproduction recipe, and a second — worse — failure path. Both from a current build of the repro.
1. The recipe. My comment above says "run B repeatedly." That's wrong. Twenty-five consecutive B presses from a fresh launch never terminate the process — every one produces only the caught
UIKitThreadAccessExceptionon the background thread. What does terminate it is alternating A and B, five presses each.The asymmetry is consistent with the mechanism: under UIKit's thread check, B's background loop dies on its first iteration, so each B press buys a single off-thread write, whereas A's two loops run unchecked and generate the sustained concurrent traffic that actually corrupts the set.
2. The UI thread doesn't only die in the animation ticker. On the current build, the thread-1 termination came through the layout pass:
Note the overload:
SetValue(BindablePropertyKey, Object).Widthis a read-only bindable property — an application cannot write it, only the framework can. The UI-thread writer here is MAUI's own layout system, arranging the element because UIKit calledLayoutSubviews().That is harder to characterise as application misuse than the ticker stack was. Starting an animation is something an app chose to do; being arranged is not. Every element that appears on screen goes through
ArrangeOverride.The off-thread side of the same race is the next entry in the report, same second:
Summary of where this leaves iOS. The thread-1 termination has now been observed on two independent UI-thread paths — layout (
set_Width) and animation (set_Opacity) — carrying both production signatures (IndexOutOfRangeExceptionhere,InvalidOperationExceptionin the earlier stack). The corruption isn't tied to a framework subsystem. It's tied to the element: whatever the UI thread touches that element with next is what fails.TamilarasanSF4853 commentedon Aug 17, 2026
This issue has been validated in Visual Studio Code 1.133.0 with .NET MAUI versions 10.0.0 and 10.0.90. It can be reproduced on both Android and iOS platforms.
Screen.Recording.2026-08-17.at.11.12.04.AM.mov
shnaz commentedon Sep 22, 2026
Hello, we are also experiencing this issue