From ad8f4803d3f52fea9eb0f31daba9171e66cbd3e1 Mon Sep 17 00:00:00 2001 From: Spottedleaf Date: Mon, 9 Oct 2023 10:02:37 -0700 Subject: [PATCH] Make EntityLookup#get use read lock for entity maps On Folia, this map is modified by multiple threads and iterating it may throw errors, for example using the /kill command or any other command that uses an entity selector. --- patches/server/Rewrite-chunk-system.patch | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/patches/server/Rewrite-chunk-system.patch b/patches/server/Rewrite-chunk-system.patch index 258cfa9323..95e5bad95f 100644 --- a/patches/server/Rewrite-chunk-system.patch +++ b/patches/server/Rewrite-chunk-system.patch @@ -3781,7 +3781,15 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 + + @Override + public void get(final EntityTypeTest filter, final AbortableIterationConsumer action) { -+ for (final Entity entity : this.entityById.values()) { ++ final Int2ReferenceOpenHashMap entityCopy; ++ ++ this.entityByLock.readLock(); ++ try { ++ entityCopy = this.entityById.clone(); ++ } finally { ++ this.entityByLock.tryUnlockRead(); ++ } ++ for (final Entity entity : entityCopy.values()) { + final Visibility visibility = EntityLookup.getEntityStatus(entity); + if (!visibility.isAccessible()) { + continue;