Fix item loss when player disconnects during trade#39
Open
GG-MD wants to merge 2 commits intoArtillex-Studios:masterfrom
Open
Fix item loss when player disconnects during trade#39GG-MD wants to merge 2 commits intoArtillex-Studios:masterfrom
GG-MD wants to merge 2 commits intoArtillex-Studios:masterfrom
Conversation
Author
Author
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem Description
Players were losing items when either trader disconnected during an active trade session. The items would disappear completely - neither player would receive them back. This was a critical bug affecting item safety.
Reproduction Steps
What I Found
After adding debug logging and testing with two players, I discovered the root cause was a race condition in the
abort()method:handleQuitTrade()callstrade.abort()abort()was callingend()first, which closed the GUI inventorieshandleClose()event handlerhandleClose()tried to callabort()again (recursion!)abort()setended=trueearly, so the second call would exit before returning itemsabort()call then tried to return items, but the GUI was already closed and clearedAdditionally, the old code used
ContainerUtils.INSTANCE.addOrDrop()which runs asynchronously. When a player disconnects, they're gone before the async operation completes, so items never get added to their inventory.Solution
I restructured the
abort()method to follow a safer transaction pattern:Changes in Trade.java
ended=trueimmediately at the start ofabort()- preventshandleClose()from callingabort()againplayer.getInventory().addItem()instead of async ContainerUtilsisOnline()before sending messages/sounds to prevent errorsChanges in TradeGui.java
Added
if (trade.isEnded()) return;check inhandleClose()to prevent recursive abort calls.Testing
Tested multiple scenarios with two test players:
The fix has been running on my server for several test sessions without any item loss or duplication.
Technical Details
Before:
After:
This ensures items are always returned before any GUI cleanup happens, and the synchronous approach guarantees items are added even if the player disconnects immediately after.
Additional Notes
I noticed the original code also lacked null checks and online status checks in some places. I added those where appropriate to prevent additional edge case errors.
Let me know if you need any clarification or have questions about the implementation!