-
Notifications
You must be signed in to change notification settings - Fork 3
Fix race condition in LocalAuctionClusterBridge lock implementation #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -9,21 +9,31 @@ | |||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| import java.util.UUID; | ||||||||||||||||||||||||||||||||||
| import java.util.concurrent.CompletableFuture; | ||||||||||||||||||||||||||||||||||
| import java.util.concurrent.ConcurrentHashMap; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| public class LocalAuctionClusterBridge implements AuctionClusterBridge { | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| private final ConcurrentHashMap<UUID, UUID> itemLocks = new ConcurrentHashMap<>(); | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| @Override | ||||||||||||||||||||||||||||||||||
| public CompletableFuture<Boolean> checkAvailability(Item item) { | ||||||||||||||||||||||||||||||||||
| return CompletableFuture.completedFuture(true); | ||||||||||||||||||||||||||||||||||
| return CompletableFuture.completedFuture(!itemLocks.containsKey(item.getId())); | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| @Override | ||||||||||||||||||||||||||||||||||
| public CompletableFuture<LockToken> lockItem(Item item, UUID buyerId, StorageType storageType) { | ||||||||||||||||||||||||||||||||||
| UUID existingLock = itemLocks.putIfAbsent(item.getId(), buyerId); | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| if (existingLock != null) { | ||||||||||||||||||||||||||||||||||
| return CompletableFuture.failedFuture(new IllegalStateException("Item already locked by another player")); | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| return CompletableFuture.completedFuture(LockToken.of(item)); | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| @Override | ||||||||||||||||||||||||||||||||||
| public CompletableFuture<Void> unlockItem(Item item, LockToken lockToken, StorageType storageType) { | ||||||||||||||||||||||||||||||||||
| itemLocks.remove(item.getId()); | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
| itemLocks.remove(item.getId()); | |
| UUID currentOwner = itemLocks.get(item.getId()); | |
| // If there is no current lock, consider it already unlocked. | |
| if (currentOwner == null) { | |
| return CompletableFuture.completedFuture(null); | |
| } | |
| // Verify that the lock token belongs to the current owner before unlocking. | |
| UUID tokenBuyerId = lockToken.getBuyerId(); | |
| if (!currentOwner.equals(tokenBuyerId)) { | |
| return CompletableFuture.failedFuture( | |
| new IllegalStateException("Lock token does not match the current lock owner")); | |
| } | |
| itemLocks.remove(item.getId(), currentOwner); |
Copilot
AI
Jan 8, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actual argument type 'Integer' is incompatible with expected argument type 'UUID'.
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -46,15 +46,16 @@ public CompletableFuture<Void> purchaseItem(Player player, Item item) { | |||||||||||||||||||||||||||||
| auctionManager.openMainAuction(player); | ||||||||||||||||||||||||||||||
| return CompletableFuture.completedFuture(null); | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| if (item.getStatus() != ItemStatus.IS_PURCHASE_CONFIRM) { | ||||||||||||||||||||||||||||||
| logger.info("Item not available"); | ||||||||||||||||||||||||||||||
| auctionManager.openMainAuction(player); | ||||||||||||||||||||||||||||||
| return CompletableFuture.completedFuture(null); | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| synchronized(item) { | ||||||||||||||||||||||||||||||
| if (item.getStatus() != ItemStatus.IS_PURCHASE_CONFIRM) { | ||||||||||||||||||||||||||||||
| logger.info("Item not available"); | ||||||||||||||||||||||||||||||
| auctionManager.openMainAuction(player); | ||||||||||||||||||||||||||||||
| return CompletableFuture.completedFuture(null); | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| item.setStatus(ItemStatus.IS_BEING_PURCHASED); | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
Comment on lines
+50
to
57
|
||||||||||||||||||||||||||||||
| synchronized(item) { | |
| if (item.getStatus() != ItemStatus.IS_PURCHASE_CONFIRM) { | |
| logger.info("Item not available"); | |
| auctionManager.openMainAuction(player); | |
| return CompletableFuture.completedFuture(null); | |
| } | |
| item.setStatus(ItemStatus.IS_BEING_PURCHASED); | |
| } | |
| if (item.getStatus() != ItemStatus.IS_PURCHASE_CONFIRM) { | |
| logger.info("Item not available"); | |
| auctionManager.openMainAuction(player); | |
| return CompletableFuture.completedFuture(null); | |
| } | |
| item.setStatus(ItemStatus.IS_BEING_PURCHASED); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The ConcurrentHashMap is using UUID as the key type, but item.getId() returns an int. This causes a type mismatch that will result in a compilation error. The map should be declared as ConcurrentHashMap<Integer, UUID> to match the item ID type.