Run the window's close on a thread of its own and hide the window, so it keeps answering while the game saves.

gfx_window_win.c wnd_proc ran gfx_window_closed, which does not return, on the window thread for WM_CLOSE and
WM_ENDSESSION. While pipeline builds run the end takes seconds (vk_pipeline_save_exit waits up to 5 s), and the
window stopped pumping meanwhile: after 5 s Windows showed it as not responding, and an "End task" there killed
the process in the middle of its saves.

WM_CLOSE now starts an "exit" thread that runs gfx_window_closed (exit_start; the end runs on the window thread as
before should no thread be made) and returns 0, so the pump goes on. The window is hidden as the end starts, so a
window that still answers does not invite a second close; one would find the end under way in rt_exit_begin.
WM_ENDSESSION(TRUE) starts the same thread and waits for it on the window thread, since Windows ends the process
once that message returns; after a close had started the end it waits as well. The SDL window is unchanged.
ShutdownBlockReasonCreate was left out: called at WM_ENDSESSION it is too late to show a reason.

Not reproduced before the change (a play-test item: close on the boot screen, the window must not ghost).
tests/exit_order_check.py checks statically that WM_CLOSE and WM_ENDSESSION go through exit_start, which hides the
window, and that the session's end joins the thread; it and the compile checks pass.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
chrg6
2026-11-13 17:43:36 -05:00
parent f9033dce64
commit dc94771051
2 changed files with 42 additions and 5 deletions

View File

@@ -485,6 +485,27 @@ int gfx_window_monitor_size(int *w, int *h) {
return 1;
}
// The window's close runs on a thread of its own: gfx_window_closed saves the settings, the caches and the saves and
// ends the process, which takes seconds while pipeline builds run (vk_pipeline_save_exit waits up to 5 s for them). On
// the window thread the window stopped answering meanwhile; after 5 s Windows showed it as not responding, and an
// "End task" there killed the process in the middle of its saves. The window is hidden as the end starts, so a window
// that still answers does not invite a second close (one would find the end under way: gfx_present.c rt_exit_begin).
static volatile int32_t g_exit_started;
static void exit_main(void *arg) {
(void)arg;
gfx_window_closed();
}
// The end's thread, NULL when the end had started already or runs here (no thread could be made).
static PlatThread *exit_start(HWND h) {
if (plat_xchg32(&g_exit_started, 1)) return NULL;
ShowWindow(h, SW_HIDE);
PlatThread *t = plat_thread_new(exit_main, NULL, "exit");
if (!t) gfx_window_closed();
return t;
}
static LRESULT CALLBACK wnd_proc(HWND h, UINT m, WPARAM w, LPARAM l) {
LRESULT input_res;
if (input_window_msg(h, m, w, l, &input_res)) return input_res;
@@ -576,13 +597,18 @@ static LRESULT CALLBACK wnd_proc(HWND h, UINT m, WPARAM w, LPARAM l) {
return 0;
}
case WM_ENDSESSION:
// Windows is shutting down, restarting or logging off: it ends the process after this message without a
// WM_CLOSE, so the close path runs here (the settings menu's pending save, RTSS's profile, a save Commit in
// flight); gfx_window_closed does not return
if (w) gfx_window_closed();
// Windows is shutting down, restarting or logging off: it ends the process once this message returns, without a
// WM_CLOSE, so the close's end runs (the settings menu's pending save, RTSS's profile, a save Commit in flight)
// and this thread waits for it here; the end does not return
if (w) {
PlatThread *t = exit_start(h);
if (t) plat_thread_join(t);
for (;;) plat_sleep_ms(1000); // a close had started the end already
}
return 0;
case WM_CLOSE:
gfx_window_closed();
exit_start(h);
return 0;
case WM_ERASEBKGND:
return 1;
}

View File

@@ -161,6 +161,17 @@ def static_checks():
ok = None not in pos and pos == sorted(pos)
print(f'cheats.c rebuild_if_needed: {", ".join(want)} in that order: {"ok" if ok else "FAIL " + str(pos)}', flush=True)
bad += not ok
# the window's close runs the end on a thread of its own, and the session's end waits for it
win = strip_comments((RT / 'gfx_window_win.c').read_text(encoding='utf-8'))
proc = function(win, 'static LRESULT CALLBACK wnd_proc(')
close = proc[proc.index('case WM_CLOSE:'):]
close = close[:close.index('case WM_ERASEBKGND:')]
session = proc[proc.index('case WM_ENDSESSION:'):proc.index('case WM_CLOSE:')]
ok = ('exit_start(' in close and 'gfx_window_closed(' not in close and 'exit_start(' in session
and 'plat_thread_join(' in session and 'ShowWindow(h, SW_HIDE)' in function(win, 'static PlatThread *exit_start('))
print(f'gfx_window_win.c: WM_CLOSE and WM_ENDSESSION start the end on its own thread and hide the window: '
f'{"ok" if ok else "FAIL"}', flush=True)
bad += not ok
steps = function(gp, 'void rt_exit_steps(unsigned what)')
pos = calls_in_order(steps, ['rtss_limit_restore', 'vk_pipeline_save_exit'])
ok = None not in pos and pos[0] < pos[1]