vchelaru / FlatRedBall

Cross-platform 2D game engine focused on ultimate productivity built in .NET
http://flatredball.com
MIT License
408 stars 65 forks source link

Adding collision relationships while game is running can cause tasks to freeze #1501

Closed vchelaru closed 1 month ago

vchelaru commented 1 month ago

Not sure exactly how to repro it but I got it by running in edit mode and creating a collision relationship between PlayerList and CoinList. This feels like a nasty bug. The deadlock is on 2 threads both wanting to perform primary thread calls.

image

image

To reproduce this, just try creating tons of relationships between player and entity. Note that Coin and CoinList are both dynamic (created while game was running) if it matters...

vchelaru commented 1 month ago

After thinking about this I have a few ideas on how to solve this. This is ultimately happening because 2 tasks are both awaiting their completion:

  1. Task1 begins and awaits its completion. It is index[0] in the list of tasks
  2. Task2 begins and awaits its completion. Task2 begins in response to Task[1].
  3. Task1 cannot complete because Task2 must finish before Task1 is done.
  4. Task2 cannot complete because it is [1] on the task list

I believe this is the case. To solve this, only index [0] should be able to await its completion. However, how can we tell that is the case... Actually why would task 2 ever even get put on the task list?