Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@

### 修复

- 调度队列 修复任务完成后没有执行设定的关机、重启等电源操作,倒计时结束后只有软件退出的问题
- 修复绝区零一条龙由 MAS 启动时报「运行环境同步失败」,且自动模式失败后不切换启动器的问题
- 修复配了网络代理的用户初始化下载仍直连、卡在「程序文件」一步的问题 by [@qiyinxi](https://github.com/qiyinxi)
- HSR专项 修复系统区域非中文时三月七助手每个模块被判失败、整轮重复运行的问题
Expand Down
7 changes: 4 additions & 3 deletions app/services/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,18 +140,19 @@ async def set_power(
if mode in {"Shutdown", "Reboot", "Logoff"}:
await self.kill_emulator_processes()
logger.info(f"执行电源操作: {mode}")
await self._request_frontend_close()
# 系统电源动作必须先于前端关闭执行:请求前端退出会让 Electron 退出并连带结束后端进程,
# 若先等前端断开,关机/重启等命令将永远没有机会执行(issue #611)。
await power.execute(mode)

async def _request_frontend_close(self) -> None:
"""请求前端退出,并等待主会话断开后才允许执行系统动作。"""
"""请求前端退出,并等待主会话断开(仅用于后端自行退出的 KillSelf 场景)。"""

sent = await Publisher.send(
id=protocol.ID_MAIN, type=protocol.FRONTEND_CLOSE_REQUESTED
)
if not sent:
# 当前没有前端会话,本身已满足“前端关闭”前置条件。
logger.info("当前无前端主连接,继续执行系统电源操作")
logger.info("当前无前端主连接,继续退出主程序")
return

disconnected = await MainConnection.wait_until_disconnected(
Expand Down
1 change: 1 addition & 0 deletions res/version.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"日志清理 MFW 项目的 MaaFramework 原生日志备份改为按保留天数一并清理,不再无限堆积"
],
"修复": [
"调度队列 修复任务完成后没有执行设定的关机、重启等电源操作,倒计时结束后只有软件退出的问题",
"修复绝区零一条龙由 MAS 启动时报「运行环境同步失败」,且自动模式失败后不切换启动器的问题",
"修复配了网络代理的用户初始化下载仍直连、卡在「程序文件」一步的问题 by [@qiyinxi](https://github.com/qiyinxi)",
"HSR专项 修复系统区域非中文时三月七助手每个模块被判失败、整轮重复运行的问题",
Expand Down
39 changes: 39 additions & 0 deletions tests/services/test_set_power_execution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import unittest
from unittest.mock import AsyncMock, patch

from app.services.system import System


class SetPowerExecutionTest(unittest.IsolatedAsyncioTestCase):
async def test_system_power_action_executes_without_frontend_close(self):
# 前端退出会连带结束后端进程,系统电源动作不得依赖前端关闭(issue #611)
with (
patch.object(
System, "_request_frontend_close", new_callable=AsyncMock
) as request_frontend_close,
patch(
"app.services.system.power.execute", new_callable=AsyncMock
) as execute,
):
await System.set_power("ShutdownForce")
Comment on lines +14 to +18

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (testing): 回归测试在未模拟 kill_emulator_processes 的情况下调用 System.set_power("ShutdownForce"),因此会扫描主机进程表,并终止名称包含模拟器相关关键词的所有进程。在存在匹配的模拟器进程的机器上运行该测试,可能会终止该进程,使测试具有破坏性或不稳定性。

触发条件: 当测试在开发者机器或 CI 机器上运行,且其中存在名称包含 Nemu、emulator 或 MuMu 的进程时。

建议修复: 在调用 set_power 之前,在测试中使用 AsyncMock 对 System.kill_emulator_processes 进行 patch。

Suggested change
patch(
"app.services.system.power.execute", new_callable=AsyncMock
) as execute,
):
await System.set_power("ShutdownForce")
patch.object(
System, "kill_emulator_processes", new_callable=AsyncMock
),
patch(
"app.services.system.power.execute", new_callable=AsyncMock
) as execute,
):
await System.set_power("ShutdownForce")
Original comment in English

issue (testing): The regression test calls System.set_power("ShutdownForce") without mocking kill_emulator_processes, so it scans the host process table and kills every process whose name contains emulator-related keywords. Running the test on a machine with a matching emulator process can terminate that process and make the test destructive or flaky.

Triggers: When the test runs on a developer or CI machine with a process whose name contains Nemu, emulator, or MuMu.

Suggested fix: Patch System.kill_emulator_processes with an AsyncMock in the test before invoking set_power.

Suggested change
patch(
"app.services.system.power.execute", new_callable=AsyncMock
) as execute,
):
await System.set_power("ShutdownForce")
patch.object(
System, "kill_emulator_processes", new_callable=AsyncMock
),
patch(
"app.services.system.power.execute", new_callable=AsyncMock
) as execute,
):
await System.set_power("ShutdownForce")


execute.assert_awaited_once_with("ShutdownForce")
request_frontend_close.assert_not_awaited()

async def test_kill_self_still_requests_frontend_close(self):
# KillSelf 是后端自行退出,保留先请求前端退出的既有行为
server = type("Server", (), {"should_exit": False})()
with (
patch.object(
System, "_request_frontend_close", new_callable=AsyncMock
) as request_frontend_close,
patch("app.core.config.Config.server", server),
):
await System.set_power("KillSelf")

request_frontend_close.assert_awaited_once()
self.assertTrue(server.should_exit)


if __name__ == "__main__":
unittest.main()