From 047615dbbe40e0d82e202f8a9483471aa54c62cd Mon Sep 17 00:00:00 2001 From: MathiewMay Date: Wed, 11 Mar 2026 08:34:20 -0400 Subject: [PATCH] Fix entity ID TLS key recreating on every call of getSmallid() for linux, this caused a ID pool corruption. --- Minecraft.World/Entities/Entity.cpp | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/Minecraft.World/Entities/Entity.cpp b/Minecraft.World/Entities/Entity.cpp index 9c88a00e2..0a62c9b3e 100644 --- a/Minecraft.World/Entities/Entity.cpp +++ b/Minecraft.World/Entities/Entity.cpp @@ -24,7 +24,11 @@ int Entity::entityCounter = 2048; // 4J - changed initialiser to 2048, as we are using range 0 - 2047 as special unique smaller ids for things that need network tracked +#ifdef _WIN32 DWORD Entity::tlsIdx = TlsAlloc(); +#else +pthread_key_t Entity::tlsIdx = 0; +#endif // 4J - added getSmallId & freeSmallId methods unsigned int Entity::entityIdUsedFlags[2048/32] = {0}; @@ -47,7 +51,6 @@ int Entity::getSmallId() #ifdef _WIN32 if( ((size_t)TlsGetValue(tlsIdx) != 0 ) ) #else - pthread_key_create(&tlsIdx, nullptr); if ( ((size_t)pthread_getspecific(tlsIdx) != 0) ) #endif { @@ -151,7 +154,16 @@ void Entity::freeSmallId(int index) void Entity::useSmallIds() { - pthread_setspecific(tlsIdx,(LPVOID)1); +#ifdef _WIN32 + TlsSetValue(tlsIdx, (LPVOID)1); +#else + static bool keyCreated = false; + if (!keyCreated) { + pthread_key_create(&tlsIdx, nullptr); + keyCreated = true; + } + pthread_setspecific(tlsIdx,(void*)1); +#endif } // Things also added here to be able to manage the concept of a number of extra "wandering" entities - normally path finding entities aren't allowed to