Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BindableCollection races against any call which adds/removes elements before it #318

Open
canton7 opened this issue Dec 5, 2021 Discussed in #306 · 0 comments
Open

Comments

@canton7
Copy link
Owner

canton7 commented Dec 5, 2021

Discussed in #306

Originally posted by Yoooi0 November 21, 2021
There seems to be an issue with thread safety in BindableCollection.

When two threads modify the collection at the same time, one calling ClearItems, the other calling RemoveItem, it can cause OnCollectionChanging to throw an exception.

System.Reflection.TargetInvocationException: "An error occurred while dispatching a call to the UI Thread"

Inner Exception
ArgumentOutOfRangeException: "Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index')"

at Stylet.Execute.OnUIThreadSync(Action action)
at Stylet.BindableCollection`1.RemoveItem(Int32 index)
at System.Collections.ObjectModel.Collection`1.Remove(T item)

Example timeline:

  • Create BindableCollection with 1 item
  • Thread 1 calls collection.ClearItems()
  • Thread 1 enters Execute.OnUIThreadSync
  • Thread 2 calls collection.Remove(firstItem), this calls RemoveItem with index argument set to 0
  • Thread 2 waits for thread 1
  • Thread 1 clears the collection by calling base.ClearItems()
  • Thread 2 enters Execute.OnUIThreadSync
  • Thread 2 throws exception when calling OnCollectionChanging because the item at index 0 does not exist anymore
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

No branches or pull requests

1 participant