From 2f563a187269cd81d928887a40ad495ebd4adefc Mon Sep 17 00:00:00 2001 From: Karrz1 <127596890+Karrz1@users.noreply.github.com> Date: Tue, 20 Oct 2026 02:05:56 +0100 Subject: [PATCH] Pathfinding: no null read for a polygon without detail edges, and a caught fault releases the nav lock (#76) - dtNavMesh closest-point-on-detail-mesh: a polygon whose detail mesh has no qualifying edge (no triangles, or none with a boundary flag in the boundary-only pass) left pmin/pmax null and dtVlerp read through them. The position the caller already put into `closest` is kept instead. - PathGenerator holds a per-map mutex around the A*. MapUpdater catches a hardware fault in Map::Update and skips the tick, but that unwind does not run the destructor of the lock, so the map's lock stayed held for good, the next tick threw on the relock and the world thread hung. The lock guard now records the lock the thread holds, and MapUpdater releases it in the catch through ReleaseNavLockAfterFault(). A normal exit clears the record first, so the lock is never unlocked twice. --- .../Detour/Source/DetourNavMesh.cpp | 5 ++++ src/server/game/Maps/MapUpdater.cpp | 3 ++ src/server/game/Movement/PathGenerator.cpp | 28 ++++++++++++++++++- src/server/game/Movement/PathGenerator.h | 3 ++ 4 files changed, 38 insertions(+), 1 deletion(-) diff --git a/dep/recastnavigation/Detour/Source/DetourNavMesh.cpp b/dep/recastnavigation/Detour/Source/DetourNavMesh.cpp index a655d1d89a..d788bb41c9 100644 --- a/dep/recastnavigation/Detour/Source/DetourNavMesh.cpp +++ b/dep/recastnavigation/Detour/Source/DetourNavMesh.cpp @@ -670,6 +670,11 @@ namespace } } + // A polygon whose detail mesh has no qualifying edge (no triangles, or none with a boundary flag in the boundary-only pass) leaves + // pmin/pmax unset; the caller has already put the position into `closest`, so keep that instead of reading through null. + if (!pmin || !pmax) + return; + dtVlerp(closest, pmin, pmax, tmin); } } diff --git a/src/server/game/Maps/MapUpdater.cpp b/src/server/game/Maps/MapUpdater.cpp index d8e6578d98..c3b0ee23df 100644 --- a/src/server/game/Maps/MapUpdater.cpp +++ b/src/server/game/Maps/MapUpdater.cpp @@ -20,6 +20,7 @@ #include "Log.h" #include "Map.h" #include "Metric.h" +#include "PathGenerator.h" #include #include #include @@ -95,6 +96,8 @@ unsigned int SehSafeMapUpdate(Map* m, uint32 diff, SehFault* fault) noexcept // that pointed at session teardown instead of the aura bug.) __except (SehFilter(GetExceptionInformation(), fault)) { + // The unwind skips destructors below the fault: a nav lock held by the faulting pathfind would stay locked for good. + ReleaseNavLockAfterFault(); return fault->code; } } diff --git a/src/server/game/Movement/PathGenerator.cpp b/src/server/game/Movement/PathGenerator.cpp index d59239a3ff..5c601e6dea 100644 --- a/src/server/game/Movement/PathGenerator.cpp +++ b/src/server/game/Movement/PathGenerator.cpp @@ -56,6 +56,32 @@ namespace static std::array locks; return locks[mapId % kNavExecLockStripes]; } + + // The nav lock this thread holds, if any. MapUpdater catches a hardware fault (SEH) in Map::Update and skips the tick; that unwind + // does not run the destructor of a lock held below the fault (a null read inside the locked A* left the map's stripe held, the + // next tick threw on the relock, and the world thread hung until the freeze detector fired). The guard records the lock so the + // catch can release it (ReleaseNavLockAfterFault); a normal exit clears the record first, so the lock is never unlocked twice. + thread_local std::mutex* t_navLockHeld = nullptr; + + class NavExecGuard + { + public: + explicit NavExecGuard(std::mutex& m) : _m(m) { _m.lock(); t_navLockHeld = &_m; } + ~NavExecGuard() { t_navLockHeld = nullptr; _m.unlock(); } + NavExecGuard(NavExecGuard const&) = delete; + NavExecGuard& operator=(NavExecGuard const&) = delete; + private: + std::mutex& _m; + }; +} + +void ReleaseNavLockAfterFault() +{ + if (std::mutex* held = t_navLockHeld) + { + t_navLockHeld = nullptr; + held->unlock(); + } } ////////////////// PathGenerator ////////////////// @@ -144,7 +170,7 @@ bool PathGenerator::CalculatePathImpl(float srcX, float srcY, float srcZ, float // SHARED non-thread-safe dtNavMeshQuery — serialize the node-pool-mutating A* // per map (see NavExecLock above). Guards findPath + moveAlongSurface against // the cross-thread node-pool corruption that crashed the server 2026-06-27. - std::lock_guard navGuard(NavExecLock(_source->GetMapId())); + NavExecGuard navGuard(NavExecLock(_source->GetMapId())); BuildPolyPath(start, dest); } diff --git a/src/server/game/Movement/PathGenerator.h b/src/server/game/Movement/PathGenerator.h index 0b1b720354..877af02eb4 100644 --- a/src/server/game/Movement/PathGenerator.h +++ b/src/server/game/Movement/PathGenerator.h @@ -27,6 +27,9 @@ class WorldObject; +// Unlocks the nav lock the calling thread still holds after a caught hardware fault skipped its destructor (MapUpdater). No-op otherwise. +TC_GAME_API void ReleaseNavLockAfterFault(); + // 74*4.0f=296y number_of_points*interval = max_path_len // this is way more than actual evade range // I think we can safely cut those down even more