Crafting critical section
Posted: Sat Jul 03, 2010 12:06 pm
I was just glancing through the crafting code, and I have some doubts about ingredients availability check.
It goes like this:
Now, for me it looks like a classical critical section, that is not thread-safe. It is theoretically possible for a player (i.e. another server thread) to remove the ingredients just after the "foreach" part, but before the "remove" part. Of course that it *is* hard, but it is *not* impossible.
I believe introducing synchronization to the critical section mentioned is required. Please correct me if I'm wrong, I'm definitely not an L2j guru.
It goes like this:
- The check is done with RecipeController$RecipeItemMaker.listItems(boolean remove) method, with the "remove" parameter indicating if ingredients shall be taken out of the inventory or not.
- First time, it's run in RecipeItemMaker constructor ("remove" parameter = false), as one of verification steps.
- Second time, it's run just before the success/fail check, in RecipeItemMaker.finishCrafting() method, this time with "remove" parameter = true.
Code: Select all
foreach (ingredient in ingredients) check if there is enough items in player's inventoryif (remove) remove items from player's inventory
I believe introducing synchronization to the critical section mentioned is required. Please correct me if I'm wrong, I'm definitely not an L2j guru.