Repository navigation
Fix menu holders and update tasks leaking when a player quits while a menu opens - #325
Open
Robin-qwerty wants to merge 1 commit into
Open
Robin-qwerty wants to merge 1 commit into
Robin-qwerty wants to merge 1 commit into
Conversation
… menu opens Opening a menu builds the inventory async and opens it in a later tick. That sync step never checked whether the player was still there, so a player who quit in between got a MenuHolder registered and its repeating update/refresh tasks started, and nothing ever cancelled them. - Track the newest in-flight open per player (pendingOpens). Quitting cancels it, a newer open replaces it, and the sync step only opens if it is still the pending open and viewer.isConnected(). - Check openInventory's return value; don't register on a cancelled open. - MenuHolder gets a closed flag: closed holders never start tasks, and refreshMenu() can no longer restart the update task after close. - Repeating tasks cancel themselves if the holder is closed or the player disconnected, and drop an abandoned holder on the main thread. - refreshMenu() now closes on the main thread, and only its own menu. - menuHolders is a concurrent set (read off-thread by placeholders). - DeluxeMenusOpenMenuEvent fires only when the inventory actually opened. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
Summary
If a player disconnects in the short window between running a menu command and the menu actually opening, DeluxeMenus registers a
MenuHolderfor the offline player anyway and starts its repeating tasks. Nothing ever closes that holder, so it stays inMenu.menuHoldersand its async placeholder-update task runs everyupdate_intervalseconds until the server restarts.This was confirmed with heap dumps on a production server (1.14.1-Release and 1.14.2-DEV). After 38 h there were 11 stuck holders, all for players who disconnected in the same second they ran
/fight, each with a liveCraftAsyncTask→MenuHolder$2re-evaluating placeholders for an offline player. That is roughly one leak per ~1,900 opens.Root cause
Opening a menu is split across threads and at least one tick:
Menu.openMenu(main thread) checks requirements and starts an async build.openInventory, adds the holder tomenuHoldersand starts the refresh and update tasks, without checking whether the player is still there.If the player quits between 1 and 3,
PlayerListener.onLeavehas nothing to close, because the holder isn't registered yet. Step 3 then registers it for a player who is gone:sequenceDiagram autonumber participant P as Player participant M as Main thread participant A as Async thread participant S as Scheduler P->>M: /fight M->>M: openMenu(): new MenuHolder, check requirements M->>A: runTaskAsynchronously(build) P--xM: disconnects M->>M: PlayerQuitEvent → closeMenu() Note over M: holder not registered yet,<br/>nothing to close A->>A: build inventory + items A->>M: runTask(open step) M->>M: openInventory(offline player) M->>M: menuHolders.add(holder) ❌ M->>S: startUpdatePlaceholdersTask() ❌ loop every update_interval, until restart S->>A: MenuHolder$2.run() for an offline player endTwo smaller problems in the same lifecycle:
refreshMenu()'s sync stage restarts the update task whenupdateTask == null.closeMenusets it tonull, so a refresh that finishes just after the menu closed (for example on quit) starts a new update task on a closed holder. This affects menus withrefresh: true.refreshMenu()calledMenu.closeMenufrom the async thread. That writes to the non-thread-safeHashSet, and it closes whatever menu the player has open now, which may be a newer one.menuHoldersis also read off the main thread by%deluxemenus_is_in_menu%and%deluxemenus_opened_menu%, for example when TAB or a scoreboard evaluates placeholders async.The fix
1. The open step only goes ahead if this open is still wanted
A new
pendingOpensmap (UUID → MenuHolder, concurrent) records the newest open still being built for each player.openMenuputs the holder inpendingOpensonce requirements pass.PlayerQuitEventremoves it (Menu.cancelPendingOpen).viewer.isConnected()is still true.isConnected()checks this exactPlayerobject;isOnline()would also be true if the same player had already rejoined. The return value ofopenInventoryis now checked as well:nullmeans the open didn't happen.flowchart TD A["Sync open step<br/>(main thread)"] --> B{"pendingOpens.remove(uuid, holder)<br/>and viewer.isConnected()?"} B -- "no: player quit,<br/>or a newer open replaced this one" --> X1["holder.markClosed()<br/>nothing registered, no tasks"] B -- yes --> C["close the previous menu, if any"] C --> D{"openInventory(...) != null?"} D -- "no: open was cancelled" --> X2["holder.markClosed()"] D -- yes --> E["menuHolders.add(holder)"] E --> F["start refresh / update tasks"] F --> G["fire DeluxeMenusOpenMenuEvent"] style X1 fill:#fde2e1,stroke:#c0392b style X2 fill:#fde2e1,stroke:#c0392b style G fill:#e3f4e1,stroke:#27ae60Because both the quit and the open step run on the main thread, one always happens fully before the other. Either the menu is registered before the quit (and the quit closes it), or the quit removes the pending entry and the open step drops it.
2. Holders know when they are closed
MenuHoldergets avolatile boolean closedand amarkClosed()that sets it and stops both tasks.closeMenuandcloseMenuForShutdownuse it; the shutdown path now also stops the refresh task, which it didn't before.startRefreshTask()andstartUpdatePlaceholdersTask()do nothing on a closed holder.refreshMenu()bails out if the holder is closed, both before the async part and in its sync stage, so it can no longer restart the update task after close.3. Safety net: repeating tasks stop themselves
Both repeating tasks start with
stopIfAbandoned(this). If the holder is closed or its player is no longer connected, the task cancels itself. If the holder was never closed, it is also removed frommenuHolderson the main thread (Menu.discardHolder). Even if some other path ever leaks a holder, its tasks can no longer run until restart.4. Thread-safe
menuHoldersmenuHoldersis nowConcurrentHashMap.newKeySet(), a drop-in for theHashSetthat is safe for the existing off-thread reads.Behavior changes
openMenu()calls while the first is still buildingInventoryOpenEventcancelled (for example player sleeping)DeluxeMenusOpenMenuEventNot changed:
lastOpenedMenus(used by%deluxemenus_last_menu%) is still kept after quit. A periodic sweep (suggested in the report) was left out because the self-cancelling tasks already cover it.Related
This PR and the async
/dm reloadPR (claude/relaxed-fermat-fjsevw) both changeMenu.java, so whichever merges second will need a small conflict fix: that PR changesmenuHoldersto aMap<UUID, MenuHolder>, and this one makes it concurrent and addspendingOpens. The combined version has already been built and tested locally.🤖 Generated with Claude Code