Stop HD rumble at a fatal error and a crash too, on a thread of its own the end waits for 300 ms at most.

input_rumble_hd_stop ran only in the orderly ends, so a FATAL or a crash while a controller vibrated on HD rumble
left it buzzing: nothing renews or stops the value after the process is gone (input.c says so). It also took
hd_lock with a blocking lock while it only tried the other two.

dispatch.c rt_rumble_stop_bounded runs input_rumble_hd_stop (weak in dispatch.c, as fs_save_crash_wait is, for
builds without input.c) on a new "rumble stop" thread and waits at most 300 ms for it, never joining it: the stop
goes through SDL, which takes its own joystick lock, and a crash on SDL's thread while it held that lock would
otherwise keep the ending thread there and the process would never end. fatal_end and main.c crash_handler call
it right after rtss_limit_restore. input.c tries hd_lock 50 times 1 ms apart, as it does `lock` and sdl_lock, and
skips the stop when it is not taken.

Not reproduced before the change. tests/fatal_exit_check.py (now linking dispatch.c as its own unit: the stop runs
at a fatal error; a stop that hangs leaves the end at code 70 after 0.3 s; statically both failed ends call the
bounded stop after the RTSS restore), tests/rumble_hd_check.py 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:19:38 -05:00
parent 722a5dd1b0
commit 87fd385b7b
6 changed files with 68 additions and 9 deletions

View File

@@ -59,6 +59,25 @@ void rt_fatal_notes(void) {
__attribute__((weak)) void fs_save_crash_wait(void) {} // fs_host.c; builds without it (the unit checks) have no saves
// HD rumble's neutral value before a failed run ends (input.c input_rumble_hd_stop); builds without input.c (the unit
// checks) have no controller.
__attribute__((weak)) void input_rumble_hd_stop(void) {}
// The stop goes through SDL, which takes its own joystick lock: a crash on SDL's thread while it held that lock would
// hold the ending thread there for good, and the process would never end. So it runs on a thread of its own, and the
// ending thread waits for it at most 300 ms and never joins it. A controller left buzzing is the lesser harm.
static volatile int32_t rumble_stopped;
static void rumble_stop_main(void *arg) {
(void)arg;
input_rumble_hd_stop();
__atomic_store_n(&rumble_stopped, 1, __ATOMIC_RELEASE);
}
void rt_rumble_stop_bounded(void) {
if (!plat_thread_spawn(rumble_stop_main, NULL, "rumble stop", 0)) return;
for (int i = 0; i < 30 && !__atomic_load_n(&rumble_stopped, __ATOMIC_ACQUIRE); i++) plat_sleep_ms(10);
}
// One end for a failed run. The platform layer calls the crash handler on every thread that faults, without
// waiting for the others (platform/win32.c crash_veh, crash_filter), and a fatal error can come beside a crash from
// the same bad state (an allocation that failed here, a null pointer read there). The later end cut the first report
@@ -96,6 +115,7 @@ static void fatal_end(int code, const char *fmt, va_list ap) {
// RivaTuner Statistics Server's profile as it was: left with the game's limit in it, the next run would take that
// limit for the player's own (rtss_limit.c; it gives up after a second if the failing thread holds its lock)
rtss_limit_restore();
rt_rumble_stop_bounded(); // a controller on HD rumble: nothing renews or stops its vibration after the end
fflush(stdout);
fflush(stderr);
// Not _Exit: in the C runtime it is ExitProcess, which stops the other threads wherever they are and then runs every

View File

@@ -66,3 +66,5 @@ void rt_fatal_notes(void);
// for the first caller, which reports and ends the process; 0 for that thread again (a failure inside its own report),
// which ends at once with its code; any other thread waits, logging nothing, until the process ends (30 s at most).
int rt_ending_enter(void);
// HD rumble stopped before a failed run ends, on a thread of its own that the caller waits for 300 ms at most.
void rt_rumble_stop_bounded(void);

View File

@@ -717,7 +717,12 @@ void input_rumble_hd_stop(void) {
// The locks are tried for a while and not waited for: a thread of the game may hold one for good by now.
for (int i = 0; i < 50 && !(got = plat_mutex_trylock(&lock)); i++) plat_wait_ns(1000000);
if (!got) return;
plat_mutex_lock(&hd_lock);
got = 0;
for (int i = 0; i < 50 && !(got = plat_mutex_trylock(&hd_lock)); i++) plat_wait_ns(1000000);
if (!got) { // a fatal error or a crash on the thread that writes the reports (dispatch.c rt_rumble_stop_bounded)
plat_mutex_unlock(&lock);
return;
}
hd_ended = 1;
int buzzing = rumble_hd_buzzing(&hd);
rumble_hd_forget(&hd);

View File

@@ -373,6 +373,7 @@ static void crash_handler(const PlatCrash *x) {
kern_free_cores_note();
fs_save_crash_wait(); // a save the game already reported as done finishes first (bounded; fs_host.c)
rtss_limit_restore(); // after the report: RivaTuner Statistics Server's profile as it was
rt_rumble_stop_bounded(); // a controller on HD rumble stops (dispatch.c: 300 ms at most, SDL may be the crash)
fflush(stderr);
prof_sampler_write(); // NX2NAVI_PROFILE: what was sampled until the crash (it gives up after 2 s if the sampler hangs)
plat_exit(71);