TGViewer
Telegram github commits and releases Telegram github commits and releases @tgappsupdates · 4.55K subscribers
Post #20662 66
UnigramDev/Unigram/searchcollection-diff-snapshot • d6fac75 • 2 files, +28/-5
Snapshot the collections before diffing them off-thread

SearchCollection computes its diff on the thread pool:

await incremental.LoadMoreItemsAsync(0);
var diff = await Task.Run(() => DiffUtil.CalculateDiff(this, source, ...));

Both arguments are collections the UI thread owns and is still appending to,
and CalculateDiff starts by copying them. So the pool thread runs Array.Copy
over a list whose backing array and count are being replaced underneath it, and
the reported ArgumentException comes straight out of Array.CopyImpl.

The guard that was meant to prevent this is a plain bool. UpdateImpl sets
_loading before awaiting, and LoadMoreItemsAsync returns early while it is set
- but LoadMoreItemsAsync also clears it when it finishes, so one that was
already in flight when UpdateImpl set it hands the door back while the diff is
still running. The next scroll then appends to the very collection being
copied. That interleaving needs a load to be in flight at the moment an update
starts, which is why it is rare and why the report shows half a minute of
continuous scrolling behind it.

Snapshot both on the UI thread and diff the snapshots. The diff only ever
needed the sequences, the indices it produces stay valid because items are
appended rather than inserted, and nothing else has to be synchronised.

The same lines were copied into ChatTextBox's autocomplete, so all four sites
are changed; only the first is evidenced by a crash.

Reported by crash telemetry on 12.9.1.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

#unigram
More from @tgappsupdates
  1. Oct 12, 2026TGX-Android/Telegram-X/main • fd1f52d • 5 files, +16/-16 Remove rudimentary `this` referen…
  2. Oct 12, 2026TGX-Android/Telegram-X/main • 1128e91 • 3 files, +69/-0 Optimize APK size for `latest` fla…
  3. Oct 12, 2026Ajaxy/telegram-tt/master • d2229ce • 1 files, +2/-2 Attachment Modal Item: Fix duration po…
  4. Oct 12, 2026Ajaxy/telegram-tt/master • 60ea88a • 2 files, +18/-13 Payment Modal: Fix styles (#7375) Aj…
  5. Oct 12, 2026Ajaxy/telegram-tt/master • b1bf304 • 3 files, +4/-4 12.0.46 Ajaxy/telegram-tt/master • 5ef…
  6. Oct 12, 2026morethanwords/tweb/master • a10152a • 37 files, +102/-47 Update the icon font: sixteen ico…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →