mirror of
https://github.com/tangerineturtle1/NX2navi.git
synced 2026-11-20 01:49:59 -04:00
End fatal errors and a lost device with TerminateProcess instead of _Exit, which ran every library's detach.
dispatch.c fatal_end and vk_device.c lost_wait_exit ended with _Exit, which in the C runtime is ExitProcess: it stops the other threads wherever they are and then runs every library's DLL_PROCESS_DETACH on the ending thread (the audit's probes show the exe's TLS callback getting it after _Exit and not after TerminateProcess). That is the detach plat_exit already avoids, after NGX's faulted once at the end of a clean run. After a FATAL such a fault would enter the crash handler and replace code 70 or 72 with a crash report and code 71; after a device loss a driver detach that hangs would hold the process for good, since ExitProcess had stopped the watchdog too. The new platform plat_terminate ends the process with the code and nothing else (no exit hook, no stream flush: the failing paths have written their lines and flushed). fatal_end calls it after its existing log sync and flushes. lost_wait_exit tells its watchdog (spawned with a non-NULL argument) from the threads that stop there: after LOST_EXIT_MS the watchdog alone writes "vk: the end of the run did not finish in 10 s; ending it" through rt_log_sync's bounded trylocks, and the others end 2 s later only should the watchdog not have started. Exit codes stay 70 and 72. Not reproduced before the change (no detach fault on a fatal path has been seen). tests/fatal_exit_check.py (new: dispatch.c and the platform layer with a TLS callback; rt_fatal ends with 70, its FATAL line and no detach call, which it showed with _Exit; and statically, no _Exit in runtime outside the platform layer, and both ends call plat_terminate) and the compile checks pass. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -80,7 +80,10 @@ static void fatal_end(int code, const char *fmt, va_list ap) {
|
||||
rtss_limit_restore();
|
||||
fflush(stdout);
|
||||
fflush(stderr);
|
||||
_Exit(code);
|
||||
// Not _Exit: in the C runtime it is ExitProcess, which stops the other threads wherever they are and then runs every
|
||||
// library's detach on this thread. A driver's detach that faults there turned a fatal error's end into a crash
|
||||
// report with another code (platform/win32.c plat_exit names one such detach), and one that hangs held the process.
|
||||
plat_terminate(code);
|
||||
}
|
||||
|
||||
void rt_fatal(const char *fmt, ...) {
|
||||
|
||||
@@ -1927,6 +1927,8 @@ void plat_exit(int code) {
|
||||
_exit(code);
|
||||
}
|
||||
|
||||
void plat_terminate(int code) { _exit(code); }
|
||||
|
||||
int plat_setenv(const char *name, const char *value) {
|
||||
return (value ? setenv(name, value, 1) : unsetenv(name)) == 0;
|
||||
}
|
||||
|
||||
@@ -331,6 +331,10 @@ void plat_aligned_free(void *p);
|
||||
// ---- process
|
||||
_Noreturn void plat_exit(int code); // ends the process at once: other threads stop, no atexit handlers run
|
||||
extern void (*plat_exit_hook)(void); // called first by plat_exit when set (main.c: the queued log lines)
|
||||
// Ends the process at once with code, and nothing else: no exit hook, no stream flush, and no library's detach call
|
||||
// (ExitProcess and _Exit run every one). For the ends of a failed run (dispatch.c fatal_end, vk_device.c
|
||||
// lost_wait_exit), which have written their lines themselves and must not wait on a lock another thread holds.
|
||||
_Noreturn void plat_terminate(int code);
|
||||
int plat_setenv(const char *name, const char *value); // value NULL removes it; 1 on success
|
||||
uint32_t plat_process_id(void);
|
||||
// CPU time of the process since it started (user and kernel), and wall time since it started, in ns; 0 when unknown.
|
||||
|
||||
@@ -1380,6 +1380,11 @@ void plat_exit(int code) {
|
||||
ExitProcess((UINT)code); // not reached
|
||||
}
|
||||
|
||||
void plat_terminate(int code) {
|
||||
TerminateProcess(GetCurrentProcess(), (UINT)code);
|
||||
ExitProcess((UINT)code); // not reached
|
||||
}
|
||||
|
||||
// _putenv_s, not SetEnvironmentVariableA: getenv reads the C runtime's copy of the environment, which only the CRT's
|
||||
// own calls update. An empty value removes the variable.
|
||||
int plat_setenv(const char *name, const char *value) { return _putenv_s(name, value ? value : "") == 0; }
|
||||
|
||||
@@ -868,21 +868,28 @@ static void device_fault_report(void) {
|
||||
// the run, and nothing else touches the device meanwhile: the paths that would read vk_lost and return, and a thread
|
||||
// that gets the loss as a result of its own stops here for good.
|
||||
// LOST_EXIT_MS bounds the end. The first thread writes the report and waits for a save commit (fs_host.c
|
||||
// fs_save_crash_wait, 2 s at most); should it hang on a lock that a stopped thread holds, the watchdog ends the
|
||||
// process with the same code, and the lines already written are in the log (rt_fatal_code wrote them itself).
|
||||
// fs_save_crash_wait, 2 s at most); should it hang on a lock that a stopped thread holds, the watchdog (arg non-NULL)
|
||||
// says so in one line and ends the process with the same code, and the lines already written are in the log
|
||||
// (rt_fatal_code wrote them itself). The end is plat_terminate: ExitProcess would stop the watchdog too and then run
|
||||
// every library's detach, and a driver's detach that hangs on a lost device would hold the process for good. A thread
|
||||
// that stops here otherwise ends the process a little later, should the watchdog not have started.
|
||||
#define LOST_EXIT_MS 10000
|
||||
volatile int32_t vk_lost;
|
||||
|
||||
static void lost_wait_exit(void *arg) {
|
||||
(void)arg;
|
||||
for (int t = 0; t < LOST_EXIT_MS; t += 50) plat_sleep_ms(50);
|
||||
_Exit(RT_LOST_EXIT);
|
||||
int watchdog = arg != NULL;
|
||||
for (int t = 0; t < LOST_EXIT_MS + (watchdog ? 0 : 2000); t += 50) plat_sleep_ms(50);
|
||||
if (watchdog) {
|
||||
rt_log_sync(); // the queue's locks only tried (bounded): their holder may be the thread that hangs
|
||||
rt_log("vk: the end of the run did not finish in %d s; ending it", LOST_EXIT_MS / 1000);
|
||||
}
|
||||
plat_terminate(RT_LOST_EXIT);
|
||||
}
|
||||
|
||||
void vk_device_lost_check(VkResult r, const char *where) {
|
||||
if (r != VK_ERROR_DEVICE_LOST) return;
|
||||
if (plat_xchg32(&vk_lost, 1)) lost_wait_exit(NULL); // another thread is ending the run: this one stops here
|
||||
plat_thread_spawn(lost_wait_exit, NULL, "vk lost exit", 0);
|
||||
plat_thread_spawn(lost_wait_exit, (void *)1, "vk lost exit", 0);
|
||||
device_fault_report();
|
||||
rt_fatal_code(RT_LOST_EXIT, "vk: the GPU device was lost (seen at: %s). The run ends here; the lines above name what "
|
||||
"the GPU was doing", where);
|
||||
|
||||
Reference in New Issue
Block a user