Skip to content

fix: keep view Count consistent when a range Add or Remove throws - #148

Closed
futuredo wants to merge 1 commit into
Cysharp:masterfrom
futuredo:fix/view-addrange-throwing-selector-count
Closed

futuredo wants to merge 1 commit into
Cysharp:masterfrom
futuredo:fix/view-addrange-throwing-selector-count

Conversation

@futuredo

@futuredo futuredo commented Dec 3, 2026

Copy link
Copy Markdown

Fixes #144.

A throwing selector or filter during AddRange / InsertRange leaves ISynchronizedView.Count ahead of the view's actual contents. The report in #144 is exact: CreateView throws on 999, AddRange({2, 999}) throws, the source list contains 2, 999, and the view is empty while Count is 1.

The same increment-during-projection pattern exists on the range Remove path: Filter.IsMatch can throw after earlier matches have already decremented filteredCount, again leaving Count inconsistent with the items still in the view.

Cause

In ObservableList<T>.View.SourceCollectionChanged, the range Add handler increments filteredCount inside the selector / Filter.IsMatch loop and only then calls InsertRange. If a later item throws, earlier matches have already been counted but nothing has been inserted. Range Remove decrements filteredCount in the same way before RemoveRange.

Fix

Count matches into a local addedCount / removedCount while projecting the range. Mutate the view list, then apply filteredCount only after InsertRange / RemoveRange succeeds, and only then raise the view notifications.

Exceptions still propagate to the caller. The source list has already accepted the mutation when it notifies the view, so the view is intentionally left unchanged in that case — it just no longer reports a Count that does not match its contents.

A previous attempt at the Add path was opened and then self-closed in #147, unmerged. This change also covers range Remove.

The public API is unchanged.

Validation

New tests in ObservableListTest:

  • Throwing selector on AddRange of an empty list (the A throwing selector during a range insertion leaves a wrong Count on the view #144 repro): Count and UnfilteredCount stay 0, view is empty.
  • Throwing selector on AddRange when the view already has an item: existing contents and counts stay unchanged.
  • Throwing filter on AddRange.
  • Throwing filter on RemoveRange after a successful AddRange.
  • Successful AddRange still updates Count / contents.

On master (a78ba19), the first three tests failed as reported (Count was 1 or 2 with no corresponding inserted items).

  • Before: 3 failed, 0 passed (new Add-range tests only).
  • After: the 5 new tests passed. Full core suite 115 passed, 0 failed, 0 skipped on both net8.0 and net10.0.

Linux, SDK 10.0.404:

dotnet test tests/ObservableCollections.Tests/ObservableCollections.Tests.csproj -f net8.0
dotnet test tests/ObservableCollections.Tests/ObservableCollections.Tests.csproj -f net10.0

AI assistance

AI assistance was used to implement the fix, write tests, and run local validation.

Count matching items locally while projecting a range, then update
filteredCount only after InsertRange or RemoveRange succeeds. A throwing
selector or filter can no longer leave Count ahead of the view contents.

Fixes Cysharp#144
@neuecc neuecc closed this Dec 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A throwing selector during a range insertion leaves a wrong Count on the view

2 participants