rickomax / psxprev

PSXPREV - Playstation (PSX) Files Previewer/Extractor
BSD 2-Clause "Simplified" License
193 stars 10 forks source link

Invalid operation from PreviewForm enumerating over modified All lists #58

Closed trigger-segfault closed 1 year ago

trigger-segfault commented 1 year ago

Describe the issue: The PreviewForm's refresh action includes updating the animations, models, and textures list. It does so by enumerating through the AllAnimations, AllEntities and AllTextures lists that are stored in Program. The problem is that these lists can be modified by the parallel scan threads while PreviewForm is enumerating through them, causing an InvalidOperationException because the list was modified, thus invalidating the enumeration process.

It looks like the code causing this is Parallel.ForEach used for ProcessFile. There's no other situation that could cause this issue, since ReloadItems is only called by the scan thread after an item is added. But if multiple scan threads are running then this can absolutely happen.

Callstack: UI Thread:

Scan Thread:

Solution: Locking can be used on the list object when adding to each individual list, and when updating/refreshing each individual list. That way only one thread will have access to the list until it's done using it.

Ideally, Add* functions should added to Program to automatically handle the locking.