From b61cd178522a288f12977fbfe8acfd1ced88ac4b Mon Sep 17 00:00:00 2001 From: Codex Agent Date: Sat, 17 Oct 2026 02:26:10 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9B=94=20=E6=B8=B8=E6=88=8F=E5=90=AF?= =?UTF-8?q?=E5=8A=A8=E6=94=B9=E6=88=90=E4=BC=9A=E9=87=8D=E8=AF=95=20?= =?UTF-8?q?=E2=80=94=E2=80=94=2002:03=20=E9=82=A3=E8=BD=AE=20"exe=20?= =?UTF-8?q?=E8=B5=B7=E6=9D=A5=E5=B0=B1=E9=80=80=E4=BA=86",=20=E7=99=BD?= =?UTF-8?q?=E4=B8=A2=E4=B8=80=E8=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 现象: 02:03 计划任务跑了 241s, 报"游戏启动超时"。证据: * Steam 的 content_log 里 02:0x **毫无记录**(上一轮 22:03:43 还有 "App Running", 13 分钟后我手动启动也是 5 秒就起来) * 应用事件日志 02:00~02:12 没有任何错误 * 游戏进程始终没出现 说明 Popen 返回了, 但 exe 起来就退, 连 Steam 都没碰上。 原来的写法是"点一次火, 干等 240 秒"。碰上一次静默失败就白丢一整轮(这轮的体力白攒)。 改成试 3 次、每次等 timeout/3 秒, 中间自然间隔: * 大多数瞬时故障靠重试就能救回来 * 日志里先记一行 Steam 进程数, 便于下次判断是不是 Steam 侧的问题 验证: 5 个用例(需要假时钟 —— sleep 打桩成空函数后墙钟几乎不走, 内层循环会狂转, 不控时钟就测不出"一次等满 40 秒"这件事): * 首次检查已在运行 -> 不点火 * 第一次尝试内成功 -> 只点火 1 次 * 第一次静默失败、第二次成功 -> 能救回来, 点火 >1 次 * 三次全失败 -> 放弃, 正好点火 3 次 * 总等待 = 3 x 40 = 120 秒(不再把 240 秒全花在第一次上) 全套 340/340 通过。 --- scripts/convert_modules.py | 44 +++++++++++++++----- tests/test_convert_modules.py | 78 +++++++++++++++++++++++++++++++++++ 2 files changed, 112 insertions(+), 10 deletions(-) diff --git a/scripts/convert_modules.py b/scripts/convert_modules.py index d3ebfe6..697a332 100644 --- a/scripts/convert_modules.py +++ b/scripts/convert_modules.py @@ -364,22 +364,46 @@ def ensure_window_size(width, height, find=None, client=None, resize=None): return after -def launch_game(timeout_s=240): +def launch_game(timeout_s=240, tries=3): + """启动游戏, 分几次尝试。 + + ⛔ 2026-10-17 02:03 实测: 那一轮 exe 起来就退了 —— Steam 的 content_log 里 + 02:0x 毫无记录(上一轮 22:03:43 有 "App Running"), 应用事件日志也没有报错, + 游戏进程始终没出现。而 13 分钟后我手动启动, 5 秒就起来了。 + 时间线上, 01:49 刚发生过一次会话断开重连, Steam 的 webhelper 直到 02:16 才恢复活动。 + + 原来的写法是"点一次火, 干等 240 秒", 碰上一次静默失败就白丢一整轮(这轮体力白攒)。 + 改成多次尝试 —— 中间隔一会儿再来一次, 大多数瞬时故障都能靠这个救回来。 + """ if game_running(): log('游戏已在运行') return True - log('启动游戏: %s' % GAME_EXE) - subprocess.Popen([GAME_EXE], close_fds=True) - t0 = time.time() - while time.time() - t0 < timeout_s: - time.sleep(5) - if game_running(): - log('游戏进程已出现 (%.0fs)' % (time.time() - t0)) - return True - log('游戏启动超时') + log('Steam 进程数: %d' % len([p for p in _popen_psutil('steam')])) + each = max(40, timeout_s // max(1, tries)) + for i in range(tries): + log('启动游戏(第 %d/%d 次, 每次等 %ds): %s' % (i + 1, tries, each, GAME_EXE)) + subprocess.Popen([GAME_EXE], close_fds=True) + t0 = time.time() + while time.time() - t0 < each: + time.sleep(5) + if game_running(): + log('游戏进程已出现 (%.0fs)' % (time.time() - t0)) + return True + log(' 这一次没等到游戏进程') + log('游戏启动超时(试了 %d 次)' % tries) return False +def _popen_psutil(name): + """按进程名查进程(只用于日志, 失败返回空)。""" + try: + out = subprocess.run(['tasklist', '/FI', 'IMAGENAME eq %s.exe' % name], + capture_output=True, text=True, errors='ignore').stdout + return [l for l in out.splitlines() if l.lower().startswith(name.lower())] + except OSError: + return [] + + def close_game(): subprocess.run(['taskkill', '/IM', '%s.exe' % GAME_PROC, '/F'], capture_output=True, text=True, errors='ignore') diff --git a/tests/test_convert_modules.py b/tests/test_convert_modules.py index 191312a..dce7f58 100644 --- a/tests/test_convert_modules.py +++ b/tests/test_convert_modules.py @@ -630,6 +630,84 @@ class CliRoutingTest(QuietLogMixin, unittest.TestCase): self.assertEqual(code, 0) +class LaunchGameRetryTest(QuietLogMixin, unittest.TestCase): + """⛔ 2026-10-17 02:03 实测: exe 起来就退了。 + + Steam 的 content_log 里 02:0x 毫无记录(上一轮 22:03:43 还有 "App Running"), + 应用事件日志也没报错, 游戏进程始终没出现; 而 13 分钟后手动启动 5 秒就起来了。 + 原来"点一次火干等 240 秒"的写法碰上一次静默失败就白丢一整轮。 + + 这里的时钟是假的 —— sleep 打桩成空函数后墙钟几乎不走, 内层循环会狂转, + 不控时钟就测不出"一次尝试等满 40 秒"这件事。 + """ + + class FakeTime: + def __init__(self): + self.t = 1000.0 + + def time(self): + return self.t + + def sleep(self, s): + self.t += s + + def _run(self, appear_after_call, tries=3, each=40): + """appear_after_call: 第几次 game_running 调用开始返回 True(None=永不)。""" + clock = self.FakeTime() + calls = {'n': 0} + pops = [] + + def running(): + calls['n'] += 1 + return appear_after_call is not None and calls['n'] >= appear_after_call + + with mock.patch.object(cm, 'game_running', running), \ + mock.patch.object(cm, '_popen_psutil', lambda n: []), \ + mock.patch.object(cm.subprocess, 'Popen', + lambda *a, **k: pops.append(a)), \ + mock.patch.object(cm.time, 'time', clock.time), \ + mock.patch.object(cm.time, 'sleep', clock.sleep): + ok = cm.launch_game(timeout_s=tries * each, tries=tries) + return ok, len(pops), clock.t + + def test_first_attempt_success_is_enough(self): + # 第 1 次调用是进入函数时的"已经在运行吗"检查(返回 False), + # 之后循环里的调用才代表"点火后进程出现了" —— 所以用 3。 + ok, pops, _ = self._run(appear_after_call=3) + self.assertTrue(ok) + self.assertEqual(pops, 1, '第一次就成功, 不该重复点火') + + def test_silent_failure_is_retried_and_can_recover(self): + """第一次点火后进程没出现 -> 必须再试, 第二次才出现。""" + ok, pops, _ = self._run(appear_after_call=None if False else 10) + self.assertTrue(ok, '重试之后应该成功') + self.assertGreater(pops, 1, '进程没出现时应该重新点火') + + def test_already_running_never_launches(self): + # 第 1 次检查就为真 -> 直接返回, 不点火 + ok, pops, _ = self._run(appear_after_call=1) + self.assertTrue(ok) + self.assertEqual(pops, 0, '已经在运行就不该点火') + + def test_gives_up_after_all_tries(self): + ok, pops, _ = self._run(appear_after_call=None) + self.assertFalse(ok) + self.assertEqual(pops, 3, '试满 3 次就放弃') + + def test_each_attempt_waits_its_share(self): + """总等待要摊到每次尝试上, 不能把 240 秒全花在第一次。""" + ok, pops, elapsed = self._run(appear_after_call=None, tries=3, each=40) + self.assertAlmostEqual(elapsed - 1000.0, 120.0, delta=1.0, + msg='3 次 x 40 秒 = 120 秒总等待') + + def test_already_running_does_not_relaunch(self): + with mock.patch.object(cm, 'game_running', lambda: True), \ + mock.patch.object(cm, '_popen_psutil', lambda n: []), \ + mock.patch.object(cm.subprocess, 'Popen', + lambda *a, **k: self.fail('已在运行还去点火')): + self.assertTrue(cm.launch_game()) + + class OnModulesTabTest(QuietLogMixin, unittest.TestCase): """确认停在免费合成页签 —— 判错的代价是整轮拒绝合成。