Skip to content

fix(agent): 修复退出时套接字残留 - #289

Open
1204244136 wants to merge 2 commits into
MistEO:mainfrom
1204244136:fix/agent-socket-cleanup
Open

fix(agent): 修复退出时套接字残留#289
1204244136 wants to merge 2 commits into
MistEO:mainfrom
1204244136:fix/agent-socket-cleanup

Conversation

@1204244136

@1204244136 1204244136 commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

在应用全局退出和主窗口销毁时统一清理 AgentClient 与子进程。

监督尚未写回实例的 Agent,避免连接、停止和退出并发时遗留套接字或孤儿进程。

Summary by Sourcery

确保在应用程序关闭和主窗口销毁期间,一致地清理代理及其子进程,防止遗留套接字或孤立进程。

Bug Fixes(错误修复):

  • 通过跟踪待处理代理并在执行清理时取消它们,防止在关闭过程中启动的代理遗留 IPC 套接字或孤立进程。
  • 通过强制更短的断开连接超时时间并强制终止无响应的子进程,避免因代理的长 RPC 超时时间而阻塞关闭流程。
  • 使用生命周期锁和关闭标志来保护代理的启动、停止和清理过程,消除任务启动与全局退出之间的竞态条件,从而避免漏掉代理。

Enhancements(增强改进):

  • 引入统一的代理清理例程,在所有实例中断开客户端连接、终止子进程并移除待处理代理套接字。
  • 优化代理停止处理逻辑,在具有有界等待时间的阻塞工作线程中执行子进程退出,并在多个进程之间共享一个全局超时。
  • 扩展清理机制以处理非窗口退出路径(托盘菜单、进程插件退出、提权重启),确保在所有关闭场景中都能释放代理资源。
Original summary in English

Summary by Sourcery

Ensure agents and their child processes are consistently cleaned up during application shutdown and main window destruction to prevent leftover sockets or orphaned processes.

Bug Fixes:

  • Prevent agents started during shutdown from leaving behind IPC sockets or orphan processes by tracking pending agents and cancelling them when cleanup runs.
  • Avoid blocking shutdown on agents with long RPC timeouts by enforcing shorter disconnect timeouts and force-terminating unresponsive child processes.
  • Guard agent start, stop, and cleanup with a lifecycle lock and shutdown flag to eliminate races between task startup and global exit that could miss agents.

Enhancements:

  • Introduce a unified agent cleanup routine that disconnects clients, terminates child processes, and removes pending agent sockets across all instances.
  • Refine agent stop handling to run in a blocking worker with bounded wait times for child exit, sharing a global timeout across processes.
  • Extend cleanup to handle non-window exit paths (tray menu, process plugin exit, elevated restart) so agent resources are freed in all shutdown scenarios.

在应用全局退出和主窗口销毁时统一清理 AgentClient 与子进程。

监督尚未写回实例的 Agent,避免连接、停止和退出并发时遗留套接字或孤儿进程。

@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.

Hey - 我在这里给出了一些总体反馈:

  • agent_shutdown_requestedPendingAgentState.cancelled 上使用 Ordering::SeqCst 可能比实际需要的更强;请考虑是否使用 Ordering::AcqRel / Ordering::Relaxed 即可,在保持必要的 happens-before 关系的同时降低同步开销。
  • cleanup_all_agentscleanup_pending_agents 中,你在不显式设置超时时间的情况下直接释放了 AgentClient,而在其他地方是有设置超时时间的;请确认这些代码路径不会在长时间运行的 RPC 上发生阻塞,如果有需要的话,应让它们的超时行为与 InstanceRuntime::dropstop_agent_blocking 保持一致。
  • remove_pending_agent_socket 中硬编码的 Windows 套接字目录 C:/Temp 可能在某些系统上不存在或不可写;建议使用更标准的临时目录位置(例如 %TEMP%),或者复用已有的临时目录工具,以避免删除失败。
给 AI Agents 的提示
Please address the comments from this code review:

## Overall Comments
- The use of `Ordering::SeqCst` on `agent_shutdown_requested` and `PendingAgentState.cancelled` may be stronger than necessary; consider whether `Ordering::AcqRel` / `Ordering::Relaxed` would be sufficient to reduce synchronization cost while still preserving the required happens-before relationships.
- In `cleanup_all_agents` and `cleanup_pending_agents` you drop `AgentClient`s without explicitly setting timeouts like elsewhere; confirm that these code paths won't end up blocking on long-running RPCs and, if needed, align their timeout behavior with `InstanceRuntime::drop` and `stop_agent_blocking`.
- The hardcoded Windows socket directory `C:/Temp` in `remove_pending_agent_socket` may not exist or be user-writeable on all systems; consider using a more standard temp location (e.g., `%TEMP%`) or reusing an existing temp directory utility to avoid deletion failures.

Sourcery 对开源项目是免费的——如果你觉得我们的评审有帮助,欢迎分享 ✨
帮我变得更有用!请在每条评论上点击 👍 或 👎,我会根据你的反馈改进后续的评审。
Original comment in English

Hey - I've left some high level feedback:

  • The use of Ordering::SeqCst on agent_shutdown_requested and PendingAgentState.cancelled may be stronger than necessary; consider whether Ordering::AcqRel / Ordering::Relaxed would be sufficient to reduce synchronization cost while still preserving the required happens-before relationships.
  • In cleanup_all_agents and cleanup_pending_agents you drop AgentClients without explicitly setting timeouts like elsewhere; confirm that these code paths won't end up blocking on long-running RPCs and, if needed, align their timeout behavior with InstanceRuntime::drop and stop_agent_blocking.
  • The hardcoded Windows socket directory C:/Temp in remove_pending_agent_socket may not exist or be user-writeable on all systems; consider using a more standard temp location (e.g., %TEMP%) or reusing an existing temp directory utility to avoid deletion failures.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The use of `Ordering::SeqCst` on `agent_shutdown_requested` and `PendingAgentState.cancelled` may be stronger than necessary; consider whether `Ordering::AcqRel` / `Ordering::Relaxed` would be sufficient to reduce synchronization cost while still preserving the required happens-before relationships.
- In `cleanup_all_agents` and `cleanup_pending_agents` you drop `AgentClient`s without explicitly setting timeouts like elsewhere; confirm that these code paths won't end up blocking on long-running RPCs and, if needed, align their timeout behavior with `InstanceRuntime::drop` and `stop_agent_blocking`.
- The hardcoded Windows socket directory `C:/Temp` in `remove_pending_agent_socket` may not exist or be user-writeable on all systems; consider using a more standard temp location (e.g., `%TEMP%`) or reusing an existing temp directory utility to avoid deletion failures.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

将仅承载取消信号的原子变量改为宽松内存序,由生命周期锁和子进程槽锁建立必要的先行发生关系。

清理待连接 Agent 的套接字时同时覆盖系统临时目录和 Windows IPC 兼容目录。
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