From bbf8e7154bd124bc2383b6b94def4bb664da8acb Mon Sep 17 00:00:00 2001 From: BlankenshipG Date: Mon, 19 Oct 2026 20:31:37 -0400 Subject: [PATCH] fix(engine): plugin sections in pop-outs draw in the pop-out's ImGui context (2026.10.20.4) Managed plugins loaded a second cimgui.dll whose current context stayed the main one; a section drawn in a popped-out Mini Remote then faulted in igGetCursorPosY and the runtime aborted the client (RynthDreamWeave's server card, SK). Managed plugins now bind cimgui to the engine's module, and pop-out plugin calls set the pop-out context in any second cimgui too. Co-authored-by: Cursor --- ...plugin-imgui-context-crash-2026.10.20.4.md | 46 +++++++++++ .../Plugins/ManagedPlugins.cs | 9 +++ src/RynthCore.Engine/Plugins/PluginManager.cs | 5 +- .../Plugins/StrayImGuiContext.cs | 78 +++++++++++++++++++ 4 files changed, 137 insertions(+), 1 deletion(-) create mode 100644 Changelog/2026-10-20-popout-plugin-imgui-context-crash-2026.10.20.4.md create mode 100644 src/RynthCore.Engine/Plugins/StrayImGuiContext.cs diff --git a/Changelog/2026-10-20-popout-plugin-imgui-context-crash-2026.10.20.4.md b/Changelog/2026-10-20-popout-plugin-imgui-context-crash-2026.10.20.4.md new file mode 100644 index 0000000..165a3f9 --- /dev/null +++ b/Changelog/2026-10-20-popout-plugin-imgui-context-crash-2026.10.20.4.md @@ -0,0 +1,46 @@ +# Engine: a plugin section in a popped-out window no longer kills the client (2026-10-20) + +RynthCore engine 2026.10.20.4. Previous release: 2026.10.19.3 (the live engine). + +## The crash (SK on DreamWeave, 2026-10-19 20:15:45 and 20:19:17) + +Opening the "DreamWeave server (RynthDreamWeave)" entry in a popped-out Mini Remote froze the +client and closed it about 20 seconds later. The same thing happened on a second DreamWeave client +(pid 43516). The engine logged a main-thread hang; its CLR dump shows what actually happened: + +- AC's main thread (0x7894, the one that draws the pop-outs) holds a + `System.ExecutionEngineException`, the runtime's fatal error, in + `ServerPackPlugin.RenderEmbeddedCard` -> `ImGuiNative.igGetCursorPosY()`, called from the + popped-out Mini Remote's frame. The "hang" was the runtime dying: writing the dump, then + terminating the client. +- That RynthDreamWeave is rynth's managed build (API v88): it loads into its own + AssemblyLoadContext with its own copy of ImGui.NET. The engine's DLL resolver only covers its own + ImGui.NET, so the plugin's copy loaded a second `cimgui.dll`: a second ImGui with its own + "current context". The dump has both modules, `RynthCore.cimgui.dll` (the engine's) and + `Runtime\cimgui.dll`. +- A pop-out draws in a context of its own, made current only in the engine's cimgui. The plugin + asked its own copy for the current context, got the main one (not in a frame then), and its first + call ran against a window that wasn't there: an access violation in native code, which no + try/catch stops. + +The docked Mini Remote worked by luck: there the main context is the one in a frame. Every +managed plugin loaded the same way (RynthLua, RynthNav, RynthTracker, RynthVision, RynthInventory, +RynthSort, RynthNet, RynthThreads and the server plugins), so any of their sections in a pop-out +could do the same, on any client. + +### Changed + +- **Managed plugins share the engine's ImGui.** A managed plugin's load context resolves `cimgui` + / `cimgui.dll` to the engine's own module (`ManagedPlugins.PluginLoadContext.LoadUnmanagedDll`): + one ImGui, one current context, so a pop-out's context is the one the plugin draws in. +- **Pop-outs set the context in a second cimgui too** (`Plugins/StrayImGuiContext.cs`). When a + `cimgui.dll` that isn't the engine's is loaded (a NativeAOT plugin that doesn't bind its ImGui.NET + to the engine's module), a plugin call made inside a pop-out + (`PluginManager.RenderEmbed`, ":popout" surfaces: Mini Remote sections, Guardian, RynthAi panels) + first makes the pop-out's context current in that copy, and puts the copy's own context back + afterwards. Logged once: `a second cimgui.dll is loaded ...`. + +## Tests + +- Engine builds clean. The fix is native interop with the game's ImGui, so it was checked against + the crash dump rather than a unit test: the faulting frame is exactly the call path changed. diff --git a/src/RynthCore.Engine/Plugins/ManagedPlugins.cs b/src/RynthCore.Engine/Plugins/ManagedPlugins.cs index 7048f74..4bc379f 100644 --- a/src/RynthCore.Engine/Plugins/ManagedPlugins.cs +++ b/src/RynthCore.Engine/Plugins/ManagedPlugins.cs @@ -196,6 +196,15 @@ internal static class ManagedPlugins protected override IntPtr LoadUnmanagedDll(string unmanagedDllName) { + // ImGui.NET's "cimgui": the engine's own module, so the plugin draws in the engine's + // ImGui and its current context (a pop-out's included). Its own copy of ImGui.NET used + // to load a second cimgui.dll whose current context stayed the main one; drawing a + // section in a popped-out Mini Remote then ran against a context outside its frame and + // faulted in cimgui, killing the client (RynthDreamWeave's server card, 2026-10-19). + if (EntryPoint.ImGuiNativeHandle != IntPtr.Zero + && (unmanagedDllName.Equals("cimgui", StringComparison.OrdinalIgnoreCase) + || unmanagedDllName.Equals("cimgui.dll", StringComparison.OrdinalIgnoreCase))) + return EntryPoint.ImGuiNativeHandle; string candidate = Path.Combine(_dir, unmanagedDllName.EndsWith(".dll", StringComparison.OrdinalIgnoreCase) ? unmanagedDllName : unmanagedDllName + ".dll"); diff --git a/src/RynthCore.Engine/Plugins/PluginManager.cs b/src/RynthCore.Engine/Plugins/PluginManager.cs index 8e0a0f5..9bad25b 100644 --- a/src/RynthCore.Engine/Plugins/PluginManager.cs +++ b/src/RynthCore.Engine/Plugins/PluginManager.cs @@ -1354,7 +1354,10 @@ internal static partial class PluginManager surfaceUtf8[..nameLen].CopyTo(popOut); PopOutSurfaceSuffix.CopyTo(popOut[nameLen..]); popOut[^1] = 0; - return RenderEmbedCore(pluginName, popOut, size, out want); + // A plugin on a second cimgui copy must see the pop-out's context there too (StrayImGuiContext). + StrayImGuiContext.Scope stray = StrayImGuiContext.Enter(current); + try { return RenderEmbedCore(pluginName, popOut, size, out want); } + finally { StrayImGuiContext.Restore(stray); } } return RenderEmbedCore(pluginName, surfaceUtf8, size, out want); } diff --git a/src/RynthCore.Engine/Plugins/StrayImGuiContext.cs b/src/RynthCore.Engine/Plugins/StrayImGuiContext.cs new file mode 100644 index 0000000..d73730c --- /dev/null +++ b/src/RynthCore.Engine/Plugins/StrayImGuiContext.cs @@ -0,0 +1,78 @@ +// ============================================================================ +// RynthCore.Engine - Plugins/StrayImGuiContext.cs +// +// A plugin whose ImGui.NET isn't bound to the engine's cimgui (RynthCore.cimgui.dll) calls a +// second copy, cimgui.dll, with an ImGui of its own: its own "current context". Plugins set that +// to the engine's context before drawing and read it back with GetCurrentContext, which works in +// the main frame. A popped-out window draws in a context of its own (ImGuiPopOuts), and the +// engine makes it current only in ITS cimgui: the stray copy still names the main context, which +// is not in a frame then, and the plugin's first call (igGetCursorPosY) faulted on its missing +// window. That is an access violation in native code, which no try/catch stops: the runtime +// aborted the client (RynthDreamWeave's server card in a popped-out Mini Remote, 2026-10-19). +// +// So around a plugin call made inside a pop-out, the pop-out's context is made current in the +// stray copy too, and the copy's own current context is put back afterwards. Nothing is done +// when no second cimgui is loaded (every plugin bound to the engine's). Render thread only. +// ============================================================================ + +using System; +using System.Runtime.InteropServices; + +namespace RynthCore.Engine.Plugins; + +internal static unsafe class StrayImGuiContext +{ + [DllImport("kernel32.dll", CharSet = CharSet.Unicode, ExactSpelling = true, EntryPoint = "GetModuleHandleW")] + private static extern IntPtr GetModuleHandleW(string name); + + [DllImport("kernel32.dll", CharSet = CharSet.Ansi, ExactSpelling = true, EntryPoint = "GetProcAddress")] + private static extern IntPtr GetProcAddress(IntPtr module, string name); + + // The stray module these pointers belong to (the module can load after the first pop-out frame). + private static IntPtr _module; + private static delegate* unmanaged[Cdecl] _get; + private static delegate* unmanaged[Cdecl] _set; + + /// What changed, for . + public readonly struct Scope + { + public readonly bool Entered; + public readonly IntPtr Previous; + public Scope(bool entered, IntPtr previous) { Entered = entered; Previous = previous; } + } + + /// + /// Makes current in the stray cimgui, if one is loaded; the scope + /// says what to put back (nothing when there is no stray copy). + /// + public static Scope Enter(IntPtr context) + { + if (context == IntPtr.Zero || !Resolve()) return default; + IntPtr previous = _get(); + _set(context); + return new Scope(true, previous); + } + + /// Puts back the stray copy's own current context (zero included: a pop-out's must not linger there). + public static void Restore(Scope scope) + { + if (!scope.Entered || _set == null) return; + _set(scope.Previous); + } + + /// True with the stray copy's two calls ready: a cimgui.dll loaded that isn't the engine's. + private static bool Resolve() + { + IntPtr module = GetModuleHandleW("cimgui.dll"); + if (module == IntPtr.Zero || module == EntryPoint.ImGuiNativeHandle) return false; + if (module == _module && _get != null && _set != null) return true; + IntPtr get = GetProcAddress(module, "igGetCurrentContext"); + IntPtr set = GetProcAddress(module, "igSetCurrentContext"); + if (get == IntPtr.Zero || set == IntPtr.Zero) return false; + _get = (delegate* unmanaged[Cdecl])get; + _set = (delegate* unmanaged[Cdecl])set; + _module = module; + RynthLog.Plugin($"PluginManager: a second cimgui.dll is loaded (0x{module:X8}); popped-out plugin surfaces get the pop-out's context in it too."); + return true; + } +}