Try and fix distribution pipe hang #206
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This issue was very helpfully described by @Rishum-P here: #203 (comment)
I tried to reproduce it and I believe it's actually even easier to cause than described. You just need to have a distribution pipe with 0 values for all connected sides. Feeding items into that pipe is enough to cause an infinite loop and eventual crash (when running in singleplayer).
As I understand, this issue basically happens because toNextOpenSide() only tries all possible open sides once before giving up and returning. So if we are in a situation where no side is a valid destination for items, we will just get stuck in the while loop in splitStacks() forever and ever.
I added two different protections against this issue happening. First of all,
toNextOpenSide()
now returns false if it cannot find a valid side, and in this situation we just set no specific destination for the item stack. This fixes the crash, and causes buildcraft to route the item stack randomly.However, I wanted un-routable items to be dropped on the ground, so I added a new
sideCheck()
event handler which disallows items from going to any side that has a distribution amount of 0. This should only have one effect, which is to cause items to be dropped at least there is at least one connected side that has a distribution amount greater than 0.