Skip to content

Use CHM for invertedVisibilityEntities - #536

Merged
jpenilla merged 2 commits into
PaperMC:ver/26.3.xfrom
R00tB33rMan:use-chm-for-inverted-visibility
Nov 17, 2026
Merged

jpenilla merged 2 commits into
PaperMC:ver/26.3.xfrom
R00tB33rMan:use-chm-for-inverted-visibility

Conversation

@R00tB33rMan

@R00tB33rMan R00tB33rMan commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Right now every non-player entity that stops being tracked calls CraftPlayer#onEntityRemove on every online player (the loop under the TODO in ServerLevel), so each player's invertedVisibilityEntities gets removed from on whatever region that entity was on. At the same time, the player's own region is reading and writing that same map through hide/show and the entity tracker. It's still a plain HashMap, so this can corrupt it, and no plugin has to do anything weird for that to happen; just using the visibility API at all is enough.

I went through the other ways to fix this and none of them really hold up:

  • Scheduling the removal onto each player's region means one task per online player for every entity that unloads, which is a lot of scheduler spam on a busy server.
  • Only clearing it for players on the entity's region leaves stale entries for everyone else. They pile up, and an entity that comes back keeps its old hidden state.
  • Locking the map puts a lock on the tracker's canSee path, which runs constantly, on tick threads.

A CHM makes the map safe from any region, keeps canSee lock-free, and doesn't change any behavior. It's the same thing playersByUUID and playersByName already do.

The second commit covers the rest of what JP found. pluginWeakReferences and unlistedEntities become thread-safe. setVisibleByDefault resets every online player from the entity's region, and hidePlayer/showPlayer can target a player on another region, so both read a tracker or a viewer's position and sent chunks from a region that doesn't own them. The tracker is now only touched from the entity's region, and updatePlayer only for a viewer that region owns. Nothing is lost by skipping the rest, since the tracker updates every nearby player each tick and never pairs a viewer it doesn't own. visibleByDefault is volatile because canSee reads it for player targets from other regions.

Dueris fixed the map in Canvas back in January; this is that fix trimmed down. ./gradlew build passes and rebuilding the patches leaves no changes.

Closes: #448

@jpenilla

Copy link
Copy Markdown
Member

Thanks, the CHM change looks right. There are a few more places with the same kind of problem:

  • pluginWeakReferences is a static WeakHashMap, and getPluginWeakReference runs on each player's region during hide/show, so players in different regions access it concurrently.
  • unlistedEntities is a plain HashSet written on the player's region, but PlayerList#placeNewPlayer reads it through isListed for every online player from the joining player's region.
  • CraftEntity#setVisibleByDefault calls resetAndShowEntity/resetAndHideEntity on every online player from the entity's region, which modifies their visibility state and trackers off their own region.

@R00tB33rMan

Copy link
Copy Markdown
Contributor Author

Title might need a few changes but went ahead and resolved the remaining concerns elsewhere that we also didn't cover in our upstream! Let me know if there's any other concerns.

@R00tB33rMan
R00tB33rMan force-pushed the use-chm-for-inverted-visibility branch from a33ed99 to 3632a31 Compare November 17, 2026 20:12
R00tB33rMan added a commit to CraftCanvasMC/Canvas that referenced this pull request Nov 17, 2026
pluginWeakReferences is shared by every region through getPluginWeakReference, and isListed reads unlistedEntities from the joining player's region, so both become thread-safe. setVisibleByDefault resets each player through their own scheduler instead of from the entity's region.

Port of PaperMC/Folia#536
@jpenilla

Copy link
Copy Markdown
Member

The pluginWeakReferences and unlistedEntities changes look right. The setVisibleByDefault change has a problem though: for a player in another region, the task runs on that player's region, and resetAndShowEntity/resetAndHideEntity then call getHandle() on the entity, which belongs to a different region. That throws, and since raw EntityScheduler tasks aren't wrapped, the exception fails the region tick and shuts the server down. Before this change it was already failing in that case, but the exception went back to the calling plugin instead of the region tick.

@R00tB33rMan
R00tB33rMan force-pushed the use-chm-for-inverted-visibility branch from 3632a31 to 3df8bb6 Compare November 17, 2026 20:43
R00tB33rMan added a commit to CraftCanvasMC/Canvas that referenced this pull request Nov 17, 2026
pluginWeakReferences is shared by every region through getPluginWeakReference, and isListed reads unlistedEntities from the joining player's region, so both become thread-safe.

Port of PaperMC/Folia#536
@R00tB33rMan

R00tB33rMan commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor Author

Ah thank you for the catch. Didn't take that into consideration here; I was a bit skeptical of that code to begin with! (Going to make a few more edits)

@jpenilla

Copy link
Copy Markdown
Member

The setVisibleByDefault revert avoids the region crash. But setVisibleByDefault called from the entity's region still throws for any online player in a different region. The plugin did nothing wrong in that case, so the remaining failure is still a bug. We can leave that for a follow-up PR if you'd rather get the other fixes in first.

The putIfAbsent and remove(key, value) changes in addInvertedVisibility/removeInvertedVisibility look superfluous. Both methods only run on the player's own region, since hideEntity0/showEntity0 call getHandle() first. The only cross-region writer is onEntityRemove, and it only removes entries.

@R00tB33rMan
R00tB33rMan force-pushed the use-chm-for-inverted-visibility branch from 3df8bb6 to d3fae3a Compare November 17, 2026 21:02
@R00tB33rMan

Copy link
Copy Markdown
Contributor Author

I think it'd be worth addressing these issues fully so the topic is closed. Was this a bit more in-line with what you were looking for?

@jpenilla

Copy link
Copy Markdown
Member

addInvertedVisibility/removeInvertedVisibility don't need atomic updates. They only run on the player's own region, and the only cross-region writer, onEntityRemove, just removes entries. So the original code was already fine there and those can go back to how they were. The remaining issue is setVisibleByDefault, which this push doesn't touch. Was there a commit missing?

@R00tB33rMan

Copy link
Copy Markdown
Contributor Author

addInvertedVisibility/removeInvertedVisibility don't need atomic updates. They only run on the player's own region, and the only cross-region writer, onEntityRemove, just removes entries. So the original code was already fine there and those can go back to how they were. The remaining issue is setVisibleByDefault, which this push doesn't touch. Was there a commit missing?

Got it. Sorry, still working on this! I'll shoot you an @ when I'm all done.

@R00tB33rMan
R00tB33rMan force-pushed the use-chm-for-inverted-visibility branch from d3fae3a to 679ce22 Compare November 17, 2026 22:42
@R00tB33rMan

Copy link
Copy Markdown
Contributor Author

Alrighty, I went ahead and read each suggestion carefully and tested these changes. I couldn't find any new races that any of these adjustments caused. Let me know if everything is now in order, @jpenilla!

R00tB33rMan added a commit to MagnoliaLLC/Canvas that referenced this pull request Nov 17, 2026
setVisibleByDefault resets every online player from the entity's region, and hidePlayer/showPlayer can target a player on another region, so both read a tracker or a viewer's position and sent chunks from a region that doesn't own them. The tracker updates every nearby player each tick, so skipping those calls loses no pairing.

Port of PaperMC/Folia#536
Dueris and others added 2 commits November 17, 2026 16:01
ServerLevel calls CraftPlayer#onEntityRemove for every online player
when a non-player entity stops being tracked, on the region that owned
the entity, while each player's own region reads and writes the same
map through the visibility API and the entity tracker.
pluginWeakReferences is shared by every region through
getPluginWeakReference, and isListed reads unlistedEntities from the
joining player's region in PlayerList#placeNewPlayer, so both become
thread-safe.

setVisibleByDefault resets every online player from the entity's
region, and hidePlayer/showPlayer can target a player on another
region, so both read a tracker or a viewer's position and sent chunks
from a region that doesn't own them. The tracker is now only touched
from the entity's region, and updatePlayer only for a viewer that
region owns; the tracker updates every nearby player each tick, so no
pairing is lost. visibleByDefault is volatile because canSee reads it
for a player target from other regions.
@jpenilla
jpenilla force-pushed the use-chm-for-inverted-visibility branch from 679ce22 to 4903440 Compare November 17, 2026 23:02
@jpenilla
jpenilla merged commit 58176ea into PaperMC:ver/26.3.x Nov 17, 2026
@jpenilla

Copy link
Copy Markdown
Member

One more related issue that wasn't covered here: ServerPlayer#sentListPacket is a plain field written on the joining player's region in PlayerList#placeNewPlayer, but unregisterEntity and setPlayerProfile read it from other regions on player targets. So right after a join, a hide from another region can see it as false and skip the tab list remove packet.

@R00tB33rMan
R00tB33rMan deleted the use-chm-for-inverted-visibility branch November 17, 2026 23:21
@R00tB33rMan

Copy link
Copy Markdown
Contributor Author

One more related issue that wasn't covered here: ServerPlayer#sentListPacket is a plain field written on the joining player's region in PlayerList#placeNewPlayer, but unregisterEntity and setPlayerProfile read it from other regions on player targets. So right after a join, a hide from another region can see it as false and skip the tab list remove packet.

I did actually catch this a bit further down the line, a bit funny how we both had the same catch! 😂

I'll go ahead and open up another PR addressing some other issues I found and what not soon! Just want to make sure I test my changes a bit more carefully

R00tB33rMan added a commit to CraftCanvasMC/Canvas that referenced this pull request Nov 18, 2026
setVisibleByDefault resets every online player from the entity's region, and hidePlayer/showPlayer can target a player on another region, so both read a tracker or a viewer's position and sent chunks from a region that doesn't own them. The tracker updates every nearby player each tick, so skipping those calls loses no pairing.

Port of PaperMC/Folia#536
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants