Skip to content

fix: 停止时终止 pretask 子进程,避免其在后台继续运行 - #396

Open
zmdyy0318 wants to merge 2 commits into
MaaXYZ:mainfrom
zmdyy0318:fix/pretask-kill-on-stop
Open

zmdyy0318 wants to merge 2 commits into
MaaXYZ:mainfrom
zmdyy0318:fix/pretask-kill-on-stop

Conversation

@zmdyy0318

@zmdyy0318 zmdyy0318 commented Oct 23, 2026 •

Copy link
Copy Markdown
Collaborator

pre_task可能会运行比较久,当用户点击停止时,需要把pre_task的进程关掉。

opus 5.5

问题

run_pretask 收到停止请求时,只是把子进程交给后台线程 wait,然后直接返回取消。界面上已经停止,但 pretask 进程仍在后台运行。

以 MaaEnd 的 LaunchGamePC 为例:它会一直等启动器窗口和游戏窗口,等到后还会去点启动器。用户点了停止之后,它仍可能继续操作。

改动

停止时先 child.kill(),再交给后台线程 wait 回收。

  • 只改 pretask:pretask 由项目声明、输出由 MXU 接管,属于 MXU 管理的子进程。
  • 前置程序(run_action)保持原样:那里结束的可能是用户自己的程序(例如游戏本体),原注释已说明停止不强制终止。

🤖 Generated with Claude Code

Sourcery 摘要

错误修复:

  • 收到停止请求时终止 pretask 子进程,防止它们继续运行或在后台进行交互。
Original summary in English

Sourcery 总结

Bug 修复:

  • 收到停止请求时终止 pretask 子进程,防止它们继续运行或在后台进行交互。
Original summary in English

Summary by Sourcery

Bug Fixes:

  • Terminate pretask child processes when a stop request is received, preventing them from continuing to run or interact in the background.

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

您好,我发现了 3 个问题

面向 AI 智能体的提示
请处理本次代码审查中的意见:

## 各项意见

### 意见 1
<location path="src-tauri/src/commands/system.rs" line_range="760" />
<code_context>
             );
+            // pretask 由项目声明、受 MXU 管理,与前置程序不同,停止时直接终止,避免其在后台继续操作
+            if let Err(e) = child.kill() {
+                warn!("Failed to kill pretask {}: {}", program, e);
+            }
             std::thread::spawn(move || {
</code_context>
<issue_to_address>
**非 Windows 构建失败**

将项目编译为非 Windows 目标时,`warn!` 未解析,因为 `log::warn` 仅在 `#[cfg(windows)]` 下导入,因此构建会失败。

请使用完整路径限定该宏,或确保非 Windows 目标也能导入 `log::warn`。

另见 `src-tauri/src/commands/system.rs:761`。
</issue_to_address>

### 意见 2
<location path="src-tauri/src/commands/system.rs" line_range="759-760" />
<code_context>
                 instance_id
             );
+            // pretask 由项目声明、受 MXU 管理,与前置程序不同,停止时直接终止,避免其在后台继续操作
+            if let Err(e) = child.kill() {
+                warn!("Failed to kill pretask {}: {}", program, e);
+            }
             std::thread::spawn(move || {
</code_context>
<issue_to_address>
**取消后 pretask 仍在运行**

当子进程仍在运行时,如果操作系统拒绝终止请求,`run_pretask` 会记录 `child.kill()` 错误并返回 `MXU_PRE_ACTION_CANCELLED`,而后台线程只会等待,因此尽管报告了取消,pretask 仍会继续运行。

只有确认子进程已停止后,才应报告取消;请妥善处理终止失败的情况。
</issue_to_address>

### 意见 3
<location path="src-tauri/src/commands/system.rs" line_range="759" />
<code_context>
                 instance_id
             );
+            // pretask 由项目声明、受 MXU 管理,与前置程序不同,停止时直接终止,避免其在后台继续操作
+            if let Err(e) = child.kill() {
+                warn!("Failed to kill pretask {}: {}", program, e);
+            }
</code_context>
<issue_to_address>
**辅助进程仍在运行**

如果 pretask 启动了生命周期长于自身的后代进程,`child.kill()` 只会终止由 `Command` 启动的进程,其辅助进程仍会存活,并在取消后继续运行。

请同时终止 pretask 的后代进程和直接子进程。
</issue_to_address>

Sourcery 对开源项目免费——如果您喜欢我们的审查,请考虑分享 ✨
Original comment in English

Hey - I've found 3 issues

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="src-tauri/src/commands/system.rs" line_range="760" />
<code_context>
             );
+            // pretask 由项目声明、受 MXU 管理,与前置程序不同,停止时直接终止,避免其在后台继续操作
+            if let Err(e) = child.kill() {
+                warn!("Failed to kill pretask {}: {}", program, e);
+            }
             std::thread::spawn(move || {
</code_context>
<issue_to_address>
**Non-Windows builds fail**

When the project is compiled for a non-Windows target, `warn!` is unresolved because `log::warn` is imported only under `#[cfg(windows)]`, so the build fails.

Qualify the macro or make the `log::warn` import available on non-Windows targets.

Also at `src-tauri/src/commands/system.rs:761`.
</issue_to_address>

### Comment 2
<location path="src-tauri/src/commands/system.rs" line_range="759-760" />
<code_context>
                 instance_id
             );
+            // pretask 由项目声明、受 MXU 管理,与前置程序不同,停止时直接终止,避免其在后台继续操作
+            if let Err(e) = child.kill() {
+                warn!("Failed to kill pretask {}: {}", program, e);
+            }
             std::thread::spawn(move || {
</code_context>
<issue_to_address>
**Cancellation leaves pretask running**

When the OS rejects the kill request while the child is still running, `run_pretask` logs the `child.kill()` error, returns `MXU_PRE_ACTION_CANCELLED`, and the background thread only waits, so the pretask keeps operating despite reported cancellation.

Do not report cancellation unless the child is confirmed stopped; handle a failed kill accordingly.
</issue_to_address>

### Comment 3
<location path="src-tauri/src/commands/system.rs" line_range="759" />
<code_context>
                 instance_id
             );
+            // pretask 由项目声明、受 MXU 管理,与前置程序不同,停止时直接终止,避免其在后台继续操作
+            if let Err(e) = child.kill() {
+                warn!("Failed to kill pretask {}: {}", program, e);
+            }
</code_context>
<issue_to_address>
**Helper processes keep running**

When a pretask starts descendant processes that outlive it, `child.kill()` terminates only the process spawned by `Command`, leaving its helpers alive to continue operating after cancellation.

Terminate the pretask's descendant processes as well as the direct child.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment thread src-tauri/src/commands/system.rs
Comment thread src-tauri/src/commands/system.rs
Comment thread src-tauri/src/commands/system.rs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant