Skip to content

ObservableVector<T>'s explicit IList.Add(object) calls itself and overflows the stack #25215

Closed

Description

@MartinZikmund

Current behavior 🐛

The explicit IList.Add(object) implementation on the internal ObservableVector<T> calls itself and never returns, so it kills the process with a stack overflow.

int IList.Add(object value)
{
    if (value is T typedValue)
    {
        var ret = ((IList)this).Add(typedValue); // typedValue is boxed back to object -> resolves to IList.Add(object) again
        RaiseVectorChanged(CollectionChange.ItemInserted, _list.Count - 1);
        return ret;
    }
    ...

Source:

int IList.Add(object value)
{
if (value is T typedValue)
{
var ret = ((IList)this).Add(typedValue);
RaiseVectorChanged(CollectionChange.ItemInserted, _list.Count - 1);
return ret;
}
else
{
throw new ArgumentException($"Cannot add an instance of type {value?.GetType()}");
}

Running ((IList)new ObservableVector<int>()).Add(1); in a console app referencing Uno.Foundation gives Stack overflow. Repeated 8032 times. (Runtime-backed on an earlier master build; the code is unchanged on the sha above, confirmed by inspection.)

Expected behavior 🎯

IList.Add(object) adds the item, raises VectorChanged once, and returns the new index.

How to reproduce it (as minimally and precisely as possible) 🔬

The type is internal, but instances are handed out through public properties typed as IObservableVector<T>, so app code can reach it with a cast. For example:

  1. Create any Uno app (any head).
  2. Run:
    var brush = new Microsoft.UI.Xaml.Media.RadialGradientBrush();
    var list = (System.Collections.IList)brush.GradientStops; // backed by ObservableVector<GradientStop>
    list.Add(new Microsoft.UI.Xaml.Media.GradientStop());     // StackOverflow, process dies
    The same applies to other public vectors backed by this type, e.g. ItemsControl.GroupStyle, MediaPlaybackList.Items, CommandBarFlyout.PrimaryCommands, and to any internal code path that treats one of these vectors as a non-generic IList.

Only the plain ObservableVector<int> console repro was actually run; the GradientStops snippet is source-backed (GradientStops is new ObservableVector<GradientStop>()).

Workaround 🛠️

Use the generic Add(T) (through IList<T> / IObservableVector<T>) instead of the non-generic IList.Add.

Works on UWP/WinUI

Not applicable: this is Uno's own internal collection type.

Affected platforms 📱💻🖥️

All (shared Uno.Foundation code).

Uno.Sdk version (and other relevant versions) 📦

master @ e98c0dc

IDE version 🧑‍💻

N/A

Anything else we need to know? 💬

Suggested fix: call the generic overload directly. Add(T) already raises VectorChanged, so the extra RaiseVectorChanged must go too, otherwise the event fires twice:

if (value is T typedValue)
{
    Add(typedValue);
    return _list.Count - 1;
}

A unit test in Uno.UI.UnitTests/Windows_Foundation_Collections/Given_ObservableVector.cs (it has InternalsVisibleTo) can cast to IList, call Add, and assert the item is present and VectorChanged fired once. Note that a stack overflow cannot be caught, so until fixed the test would take down the test host.

Activity

  1. added
    kind/bug
    good first issue
    triage/untriaged
    project/core-tools 🛠️
    platform/all
    difficulty/starter 🚀
    7.0
    on Oct 11, 2026
  2. removed
    triage/untriaged
    7.0
    on Oct 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    difficulty/starter 🚀good first issuekind/bugplatform/allproject/core-tools 🛠️

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions