diff --git a/runtime/gfx_window_win.c b/runtime/gfx_window_win.c index 618e949a..155ebf31 100644 --- a/runtime/gfx_window_win.c +++ b/runtime/gfx_window_win.c @@ -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; } diff --git a/tests/exit_order_check.py b/tests/exit_order_check.py index 53cda22b..32bee6a3 100644 --- a/tests/exit_order_check.py +++ b/tests/exit_order_check.py @@ -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]