Leave a line in the log when a module ends the process with ExitProcess, and write the gathered lines at a focus loss.

Two runs of 2026-11-13 (the 0.10.0 fresh install's nx2navi.1.log, 7,974,927 bytes, and the 0.9.2 test's
nx2navi.3.log) ended seconds after "window lost the focus" with exit code 0, no WER event and no line of their own:
an ExitProcess or exit() called by a module in the process and a kill from outside could not be told apart, and
both lose the up to 250 ms of lines the log thread was gathering. A direct ExitProcess skips perf.c's atexit sync.

(a) ExitProcess stops every other thread and then calls each module's detach, the executable's TLS callbacks
included; TerminateProcess calls none. win32.c exit_note_tls, in the .CRT$XLB list beside _tls_used, writes on
DLL_PROCESS_DETACH what the log queue holds (logq.c logq_peek: the bytes as they stand, no lock) and one line,
"exit: the process is ending through ExitProcess, not one of the game's own ends", with the return addresses that
led there. It runs under the loader lock with the other threads gone, so it uses no C runtime (hex written by hand)
and does not ask the loader: the addresses are named from a module list noted at start and at each focus loss
(plat_exit_note_modules: EnumProcessModules, two tables and a writers' lock the callback never takes), and
RtlCaptureStackBackTrace and WriteFile take no lock of the process. plat_exit, plat_terminate and plat_reexec set
the orderly flag (plat_exit_orderly), and main does once main_run has returned, so a tool mode's exit() leaves no
line; a library's exit() during a game run does, naming ucrtbase and its caller. main.c log_open sets it up for a
game run that writes a log file. Linux has empty stand-ins.

(b) gfx_window_focus_changed calls the new main.c rt_log_focus_lost at a focus loss: logq_nudge makes the log thread
write what it gathers at once (a flag log_main's gathering loop checks) and the module list is noted again.

Not reproduced before the change (the two ends are unexplained). The new tests/exit_tls_check.py links win32.c and
logq.c with a queued line still being gathered: ExitProcess from the program and exit() write the line and the note
naming exit_tls_check.exe; TerminateProcess, plat_exit and a return after plat_exit_orderly write no note.
tests/logq_check.c gains a nudge case, which fails with the flag left unset (252 ms) and passes with it.
thread_end_check, fatal_exit_check, crash_stack_check, exit_order_check, save_share_check and the compile checks of
every runtime file pass.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
chrg6
2026-11-13 17:53:30 -05:00
parent dd78132e6d
commit b23acff301
11 changed files with 296 additions and 3 deletions

View File

@@ -403,6 +403,16 @@ int plat_file_private(const char *path); // only this account m
typedef struct PlatAppend PlatAppend;
PlatAppend *plat_append_open(const char *path); // creates or truncates path; NULL on failure
int plat_append_write(PlatAppend *f, const void *data, size_t n); // 1 when all n bytes landed
// An end the game did not ask for: ExitProcess (or exit()) called by a library, or by the runtime outside plat_exit and
// plat_terminate. Windows calls the executable's TLS callback for it, with every other thread already stopped, and the
// callback then writes what the log queue held (queued(&p, &n): the bytes as they stand, no lock) and one line with
// the return addresses that led to the end into log. Nothing for plat_exit, plat_terminate, TerminateProcess from
// outside, or after plat_exit_orderly (main returning from a tool mode). plat_exit_note_modules notes the loaded
// modules, whose names the line gives the addresses (the callback may not ask the loader): at start and at a focus
// loss. Windows only; elsewhere they do nothing.
void plat_exit_note_setup(PlatAppend *log, void (*queued)(const char **p, size_t *n));
void plat_exit_note_modules(void);
void plat_exit_orderly(void);
// Calls cb(name, ctx) for each file in dir whose name ends in ext (".in"); name has no directory part. cb returns 0 to
// stop. Returns 0 when dir cannot be listed.
int plat_file_list(const char *dir, const char *ext, int (*cb)(const char *name, void *ctx), void *ctx);