ShopChest icon indicating copy to clipboard operation
ShopChest copied to clipboard

Error in task

Open stevmei opened this issue 7 years ago • 4 comments

See pastebin: https://pastebin.com/nFjGSaYX

stevmei avatar Sep 24 '17 12:09 stevmei

It is generated if a shop is removed by player (/shop remove) at the same time when updateNearestShops() is called async. The next() call of an iterator over all shopLocations does fail because the collection has been modified and in this case next() generate an exception.

There are two solutions:

  1. Lock the collection when iterating over it so that no other thread can modify it. This would need a lot of rewriting your code (every time you modify / read that collection).
  2. The simple but not professional solution: Catch this exception and break the updateNearestShops() for this time (that's the same behavior it actually does because of that exception).

stevmei avatar Oct 10 '17 15:10 stevmei

BTW: You could also iterate over a copy of that collection but in this case it is possible that items / holograms of removed shops are created and never got removed again (until server restart).

stevmei avatar Oct 10 '17 15:10 stevmei

Found another solution: When using a copy of that collection. you can check, if the original still contains an equal shop before re-showing holograms and items that are already removed and killed.

stevmei avatar Oct 10 '17 16:10 stevmei

But remember: creating a copy of that collection can also throw a CME. This can occure if a shop is removed concurrent to the new ArrayList<>(...) call. For this, you should consider to catch this and return an empty collection.

See the complete suggestion here: https://github.com/sigmaroot/ShopChest/commit/9cd7e0650eb776aff2e39681c981674412779d2e

If a merge request is wanted, just tell me here.

stevmei avatar Oct 10 '17 18:10 stevmei