feat(exit_game): kill BlueArchive.exe if still running after exit

The graceful Escape/Enter exit can abort when the game is stuck (three
aborts in q4h.log on 2026-10-28 alone), leaving the client open. run()
now always finishes with a process check: after a ~10s grace period for
Proton teardown, a still-running BlueArchive.exe is killed with SIGTERM,
escalating to SIGKILL if needed. driver.kill_game() gains force=True.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Nik Afiq 2026-10-28 20:52:35 +09:00
parent ac57914d4e
commit 756abe99a8
3 changed files with 55 additions and 8 deletions

View File

@ -161,11 +161,13 @@ def wait(seconds):
time.sleep(seconds) time.sleep(seconds)
def kill_game(): def kill_game(force=False):
# check=False -- pkill exits 1 with no output when nothing matches, # check=False -- pkill exits 1 with no output when nothing matches,
# which is a normal outcome here (e.g. the process already died on its # which is a normal outcome here (e.g. the process already died on its
# own), not an error worth raising on. # own), not an error worth raising on. force=True sends SIGKILL instead
run_command(["pkill", "-f", config.GAME_PROCESS_NAME], check=False) # of pkill's default SIGTERM, for a hung process that ignores SIGTERM.
signal_args = ["-KILL"] if force else []
run_command(["pkill", *signal_args, "-f", config.GAME_PROCESS_NAME], check=False)
def launch_game(): def launch_game():

File diff suppressed because one or more lines are too long

View File

@ -17,9 +17,18 @@ own reward dismissal). Success is verified against driver.window_exists()
actually going False, not just against the keypress having been sent, actually going False, not just against the keypress having been sent,
matching every other verified-keypress helper in this project. matching every other verified-keypress helper in this project.
No force-kill fallback if the graceful path doesn't verify -- matches this The graceful path itself still never guesses: it aborts cleanly on any
project's "abort cleanly on unknown state rather than guess" convention unverified state (gem_shop/bounty's convention) rather than pressing keys
(gem_shop/bounty) rather than reaching for driver.kill_game(). blindly. But whichever way it ends -- closed, aborted, or raised -- run()
finishes with a process check: if config.GAME_PROCESS_NAME is still
running after a short grace period (Proton teardown after a clean close
isn't instant), it's killed via driver.kill_game(), SIGTERM first, then
SIGKILL if that doesn't take. Added 2026-10-28 per explicit user request
("sometimes the game stuck"): q4h.log showed three exit_game aborts that
same day alone (01:23, 10:09, 17:13), each leaving the game open on
whatever screen it had wedged on. Safe to kill unconditionally here, unlike
mid-task, because exit_game is always the last task in a preset and its
whole purpose is for the game to end up closed.
Confirmed live (2026-07-18) via a standalone `exit_game` run: the Confirmed live (2026-07-18) via a standalone `exit_game` run: the
is_modal_open dim-probe correctly detected the exit-confirmation dialog is_modal_open dim-probe correctly detected the exit-confirmation dialog
@ -32,6 +41,11 @@ from ba_auto import navigation
CONFIRM_RETRIES = 3 CONFIRM_RETRIES = 3
CLOSE_WAIT_ITERATIONS = 15 CLOSE_WAIT_ITERATIONS = 15
CLOSE_WAIT_INTERVAL = 1 CLOSE_WAIT_INTERVAL = 1
# Grace period for the process to exit on its own after a clean close,
# then how long to wait after each kill signal before escalating/giving up.
PROCESS_EXIT_WAIT_ITERATIONS = 10
KILL_WAIT_ITERATIONS = 5
PROCESS_WAIT_INTERVAL = 1
def _alert_if_ap_left_unspent(driver, config): def _alert_if_ap_left_unspent(driver, config):
@ -76,7 +90,31 @@ def _confirm_exit(driver, config):
return not driver.window_exists() return not driver.window_exists()
def run(driver, config): def _wait_for_process_exit(driver, iterations):
for _ in range(iterations):
if not driver.is_game_running():
return True
driver.wait(PROCESS_WAIT_INTERVAL)
return not driver.is_game_running()
def _kill_if_still_running(driver, config):
if _wait_for_process_exit(driver, PROCESS_EXIT_WAIT_ITERATIONS):
return
print(f"[exit_game] {config.GAME_PROCESS_NAME} still running after the exit attempt -- killing it")
driver.kill_game()
if _wait_for_process_exit(driver, KILL_WAIT_ITERATIONS):
print(f"[exit_game] {config.GAME_PROCESS_NAME} killed.")
return
print(f"[exit_game] {config.GAME_PROCESS_NAME} ignored SIGTERM -- sending SIGKILL")
driver.kill_game(force=True)
if _wait_for_process_exit(driver, KILL_WAIT_ITERATIONS):
print(f"[exit_game] {config.GAME_PROCESS_NAME} killed.")
return
print(f"[exit_game] warning: {config.GAME_PROCESS_NAME} still running even after SIGKILL")
def _graceful_exit(driver, config):
driver.focus_game() driver.focus_game()
if not navigation.return_to_home(driver): if not navigation.return_to_home(driver):
print("[exit_game] could not confirm the true home screen -- aborting without pressing Escape") print("[exit_game] could not confirm the true home screen -- aborting without pressing Escape")
@ -93,3 +131,10 @@ def run(driver, config):
return return
print("[exit_game] game closed.") print("[exit_game] game closed.")
def run(driver, config):
try:
_graceful_exit(driver, config)
finally:
_kill_if_still_running(driver, config)