fix(engine): destroy panel windows on their owner thread; hold login pop-out until game window is hooked
LayeredWindow.Dispose now destroys inline when the caller owns the HWND, else posts WM_RYNTH_SELF_DESTROY to the panel itself. Floating-panel restore at login waits for Win32Backend.CanRunOnGameThread so the HWND is created on AC's game thread. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
parent
c6e44ee348
commit
6908c64807
4 changed files with 165 additions and 87 deletions
|
|
@ -1487,7 +1487,53 @@ internal class RynthOverlayWindow : Window
|
|||
}
|
||||
}
|
||||
|
||||
private void RestoreFloatingPanels(List<string> titles)
|
||||
// How often, and for how long, the login-time floating-panel restore waits
|
||||
// for Win32Backend to be able to create windows on AC's game thread.
|
||||
private const int FloatingRestorePollMs = 200;
|
||||
private const long FloatingRestoreMaxWaitMs = 60_000;
|
||||
|
||||
/// <summary>
|
||||
/// Restores saved floating panels, but only once
|
||||
/// <see cref="ImGuiBackend.Win32Backend.CanRunOnGameThread"/> is true, so
|
||||
/// each panel's LayeredWindow is created on AC's game thread like every
|
||||
/// other pop-out. LoginComplete can fire before the engine has found and
|
||||
/// hooked the game window; popping out then makes FloatingPanelHost take
|
||||
/// its legacy path and create the HWND on the Avalonia UI thread instead
|
||||
/// (Win11 WS_EX_NOACTIVATE focus race, cross-thread destroy on redock).
|
||||
/// Polls on the UI thread; after <see cref="FloatingRestoreMaxWaitMs"/> it
|
||||
/// restores anyway (logged) so a never-hooked client doesn't lose the panels.
|
||||
/// </summary>
|
||||
private void RestoreFloatingPanels(List<string> titles) =>
|
||||
RestoreFloatingPanelsWhenGameThreadReady(titles, Environment.TickCount64 + FloatingRestoreMaxWaitMs, loggedWait: false);
|
||||
|
||||
private void RestoreFloatingPanelsWhenGameThreadReady(List<string> titles, long deadline, bool loggedWait)
|
||||
{
|
||||
if (!ImGuiBackend.Win32Backend.CanRunOnGameThread)
|
||||
{
|
||||
if (Environment.TickCount64 < deadline)
|
||||
{
|
||||
if (!loggedWait)
|
||||
{
|
||||
try { RynthLog.UI($"AvaloniaOverlay: holding {titles.Count} floating panel(s) until the game window is hooked (GameHwnd=0x{ImGuiBackend.Win32Backend.GameHwnd.ToInt64():X}): [{string.Join(", ", titles)}]"); } catch { }
|
||||
}
|
||||
DispatcherTimer.RunOnce(
|
||||
() => RestoreFloatingPanelsWhenGameThreadReady(titles, deadline, loggedWait: true),
|
||||
TimeSpan.FromMilliseconds(FloatingRestorePollMs),
|
||||
DispatcherPriority.Background);
|
||||
return;
|
||||
}
|
||||
|
||||
try { RynthLog.UI($"AvaloniaOverlay: game window still not hooked after {FloatingRestoreMaxWaitMs / 1000}s — restoring floating panels on the legacy (UI-thread) path."); } catch { }
|
||||
}
|
||||
else if (loggedWait)
|
||||
{
|
||||
try { RynthLog.UI("AvaloniaOverlay: game window hooked — restoring held floating panels on the game thread."); } catch { }
|
||||
}
|
||||
|
||||
RestoreFloatingPanelsNow(titles);
|
||||
}
|
||||
|
||||
private void RestoreFloatingPanelsNow(List<string> titles)
|
||||
{
|
||||
try
|
||||
{
|
||||
|
|
|
|||
|
|
@ -67,6 +67,10 @@ internal sealed unsafe class LayeredWindow : IDisposable
|
|||
// because LayeredWindow lives in UI/ and avoids depending on the ImGui
|
||||
// subsystem.
|
||||
private const uint WM_RYNTH_RESTORE_FOCUS = 0x8001;
|
||||
// Posted by DestroyOnOwnerThread to the panel HWND itself, so DestroyWindow
|
||||
// runs in StaticWndProc on whichever thread owns it. Must not collide with
|
||||
// Win32Backend's game-window messages (0x8001-0x8004).
|
||||
private const uint WM_RYNTH_SELF_DESTROY = 0x8005;
|
||||
private const uint WM_ENTERSIZEMOVE = 0x0231;
|
||||
private const uint WM_EXITSIZEMOVE = 0x0232;
|
||||
private const uint WM_NCHITTEST = 0x0084;
|
||||
|
|
@ -199,7 +203,7 @@ internal sealed unsafe class LayeredWindow : IDisposable
|
|||
[DllImport("user32.dll")] private static extern bool BringWindowToTop(IntPtr hWnd);
|
||||
private const uint GW_OWNER = 4;
|
||||
[DllImport("user32.dll")] private static extern bool ReleaseCapture();
|
||||
[DllImport("user32.dll")] private static extern bool PostMessage(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam);
|
||||
[DllImport("user32.dll", SetLastError = true)] private static extern bool PostMessage(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam);
|
||||
[DllImport("user32.dll")] private static extern bool AttachThreadInput(uint idAttach, uint idAttachTo, bool fAttach);
|
||||
[DllImport("user32.dll")] private static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
|
||||
[DllImport("user32.dll")] private static extern bool GetCursorPos(out POINT lpPoint);
|
||||
|
|
@ -264,6 +268,16 @@ internal sealed unsafe class LayeredWindow : IDisposable
|
|||
[UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvStdcall) })]
|
||||
private static IntPtr StaticWndProc(IntPtr hwnd, uint msg, IntPtr wParam, IntPtr lParam)
|
||||
{
|
||||
// Handled before the instance lookup: Dispose has already removed the
|
||||
// instance by the time this posted message is dispatched.
|
||||
if (msg == WM_RYNTH_SELF_DESTROY)
|
||||
{
|
||||
bool destroyed = DestroyWindow(hwnd);
|
||||
int err = destroyed ? 0 : Marshal.GetLastWin32Error();
|
||||
RynthCore.Engine.RynthLog.Info($"LayeredWindow: self-destroy DestroyWindow(0x{hwnd.ToInt64():X}) = {destroyed} err={err} on thread=0x{GetCurrentThreadId():X}.");
|
||||
return IntPtr.Zero;
|
||||
}
|
||||
|
||||
LayeredWindow? self;
|
||||
lock (_instancesLock)
|
||||
_instances.TryGetValue(hwnd, out self);
|
||||
|
|
@ -294,12 +308,11 @@ internal sealed unsafe class LayeredWindow : IDisposable
|
|||
/// FloatingPanelHost's RunOnGameThread) and DestroyWindow only works on
|
||||
/// the owning thread.
|
||||
///
|
||||
/// Dispose only posts the destroy, and Win32Backend.Shutdown unhooks the
|
||||
/// game WndProc ~1 ms after the overlay stops — usually before the game
|
||||
/// thread gets to the post. AC's own WndProc then drops it, and because
|
||||
/// the engine module is never unloaded the panel lives on: frozen on its
|
||||
/// last frame, deaf to input, and hidden under the next generation's copy
|
||||
/// of it until that one is moved ("two RynthAi windows").
|
||||
/// Synchronous backstop for panels that were never disposed: because the
|
||||
/// engine module is never unloaded, such a panel lives on after shutdown,
|
||||
/// frozen on its last frame, deaf to input, and hidden under the next
|
||||
/// generation's copy of it until that one is moved ("two RynthAi windows").
|
||||
/// (Disposed panels destroy themselves via WM_RYNTH_SELF_DESTROY.)
|
||||
///
|
||||
/// <paramref name="includeThisGeneration"/>=false spares this engine
|
||||
/// load's own windows (class names carry a per-load Guid) and removes only
|
||||
|
|
@ -1180,34 +1193,51 @@ internal sealed unsafe class LayeredWindow : IDisposable
|
|||
lock (_instancesLock)
|
||||
_instances.Remove(hwnd);
|
||||
|
||||
// DestroyWindow MUST run on the thread that owns the HWND — Win32
|
||||
// silently no-ops it otherwise. The HWND was created on the game
|
||||
// thread (see FloatingPanelHost ctor's RunOnGameThread wrap), so
|
||||
// marshal the teardown there too. Without this, redocking left
|
||||
// the original floating window visible but orphaned ("frozen
|
||||
// duplicate").
|
||||
//
|
||||
// UI deep-dive finding P0-1 (2026-07-02): this used to block via
|
||||
// RunOnGameThread (a synchronous SendMessage) — Dispose is often
|
||||
// called from the Avalonia UI thread (e.g. redocking a panel),
|
||||
// and blocking it on the game thread's WndProc is part of the
|
||||
// same AB-BA deadlock class the OnInput fix closes. Dispose
|
||||
// doesn't need to observe the destroy completing, so post it
|
||||
// instead — fire-and-forget, never blocks the caller.
|
||||
try
|
||||
{
|
||||
RynthCore.Engine.ImGuiBackend.Win32Backend.PostDestroyWindow(hwnd);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
RynthCore.Engine.RynthLog.Info($"LayeredWindow.Dispose: PostDestroyWindow threw {ex.GetType().Name}: {ex.Message}. Falling back to inline call on thread=0x{GetCurrentThreadId():X}.");
|
||||
// Best-effort fallback if Win32Backend isn't available
|
||||
// (e.g. early shutdown after it's already torn down).
|
||||
bool destroyed = DestroyWindow(hwnd);
|
||||
RynthCore.Engine.RynthLog.Info($"LayeredWindow.Dispose: fallback DestroyWindow(0x{hwnd.ToInt64():X}) = {destroyed}.");
|
||||
}
|
||||
// DestroyWindow MUST run on the thread that owns the HWND (Win32
|
||||
// fails it with ERROR_ACCESS_DENIED otherwise); a missed destroy
|
||||
// leaves the floating window visible but orphaned ("frozen duplicate").
|
||||
DestroyOnOwnerThread(hwnd);
|
||||
}
|
||||
|
||||
DisposeDib();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Destroys <paramref name="hwnd"/> on the thread that actually owns it, never
|
||||
/// blocking the caller. Inline when the caller is the owner; otherwise posts
|
||||
/// WM_RYNTH_SELF_DESTROY to the window itself so its own WndProc destroys it.
|
||||
/// Posting to the panel (not the game window) works for panels created on
|
||||
/// any thread, and still works after Win32Backend has unhooked the game
|
||||
/// WndProc at shutdown — a post to the game window would be dropped by AC.
|
||||
/// </summary>
|
||||
private static void DestroyOnOwnerThread(IntPtr hwnd)
|
||||
{
|
||||
uint ownerThread = GetWindowThreadProcessId(hwnd, out _);
|
||||
uint currentThread = GetCurrentThreadId();
|
||||
|
||||
if (ownerThread == 0)
|
||||
{
|
||||
// Window already gone (e.g. its owning thread exited, which destroys it).
|
||||
RynthCore.Engine.RynthLog.Info($"LayeredWindow.Dispose: HWND=0x{hwnd.ToInt64():X} no longer exists — nothing to destroy.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (ownerThread == currentThread)
|
||||
{
|
||||
bool destroyed = DestroyWindow(hwnd);
|
||||
int err = destroyed ? 0 : Marshal.GetLastWin32Error();
|
||||
RynthCore.Engine.RynthLog.Info($"LayeredWindow.Dispose: DestroyWindow(0x{hwnd.ToInt64():X}) inline on owner thread=0x{currentThread:X} = {destroyed} err={err}.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (PostMessage(hwnd, WM_RYNTH_SELF_DESTROY, IntPtr.Zero, IntPtr.Zero))
|
||||
{
|
||||
RynthCore.Engine.RynthLog.Info($"LayeredWindow.Dispose: posted self-destroy to HWND=0x{hwnd.ToInt64():X} (owner thread=0x{ownerThread:X}, caller thread=0x{currentThread:X}).");
|
||||
return;
|
||||
}
|
||||
|
||||
// Post failed (queue full or HWND destroyed in the meantime); a cross-thread
|
||||
// DestroyWindow would only fail, so report it rather than attempt it.
|
||||
RynthCore.Engine.RynthLog.Error($"LayeredWindow.Dispose: PostMessage(self-destroy) to HWND=0x{hwnd.ToInt64():X} failed err={Marshal.GetLastWin32Error()} (owner thread=0x{ownerThread:X}).");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue