Skip to content

fix: 优化C# IPC客户端连接逻辑,避免管道破裂异常#238

Merged
WSXYT merged 3 commits into
masterfrom
seer/fix/csharp-ipc-connect-retry
May 15, 2026
Merged

fix: 优化C# IPC客户端连接逻辑,避免管道破裂异常#238
WSXYT merged 3 commits into
masterfrom
seer/fix/csharp-ipc-connect-retry

Conversation

@seer-by-sentry
Copy link
Copy Markdown
Contributor

此PR解决了当ClassIsland(C# IPC服务端)未运行或未准备好时,Python IPC客户端尝试连接导致AggregateException: Pipe is broken的问题。

变更内容:

  • _run_client中引入了一个异步辅助函数connect_with_retry(),用于封装IpcClient.Connect()调用。
  • connect_with_retry()会在连接失败时捕获异常,记录警告日志,并以5秒的间隔重试,直到连接成功或程序停止运行。
  • 将初始连接和重连逻辑都替换为调用connect_with_retry(),确保客户端能够优雅地等待服务端可用。
  • 将捕获到的AggregateException的日志级别从ERROR降级为WARNING,以避免在ClassIsland未运行时产生不必要的错误报告,因为这是一种预期情况。

Fixes SECRANDOM-7T

@WSXYT WSXYT marked this pull request as ready for review May 15, 2026 15:11
Copilot AI review requested due to automatic review settings May 15, 2026 15:11
Comment on lines +471 to +474
except asyncio.CancelledError:
pass
except Exception as e:
logger.exception(f"C# IPC 客户端循环出错: {e}")
logger.warning(f"C# IPC 客户端循环出错: {e}")

This comment was marked as outdated.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

该 PR 通过为 Python 侧 C# IPC 客户端增加“连接失败自动重试”的机制,降低当 ClassIsland(IPC 服务端)未启动/未就绪时触发 Pipe is broken 等异常的概率,从而让客户端在服务端可用前更平滑地等待与重连。

Changes:

  • _run_client() 内新增 connect_with_retry(),对 IpcClient.Connect() + Wait() 封装并在失败时按固定间隔重试。
  • 将初始连接与断线后的重连流程统一改为调用 connect_with_retry()
  • 将客户端主循环的异常日志由 exception 调整为 warning

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +416 to +427
while self.is_running:
try:
task = self.ipc_client.Connect()
await self.loop.run_in_executor(
None, lambda task=task: task.Wait()
)
return True
except Exception as e:
logger.warning(
f"C# IPC 连接失败(ClassIsland 可能未运行),{retry_delay:.0f}s 后重试: {e}"
)
await asyncio.sleep(retry_delay)

task = self.ipc_client.Connect()
await self.loop.run_in_executor(None, lambda: task.Wait())
if not await connect_with_retry():
Comment on lines +473 to +474
except Exception as e:
logger.exception(f"C# IPC 客户端循环出错: {e}")
logger.warning(f"C# IPC 客户端循环出错: {e}")
@WSXYT
Copy link
Copy Markdown
Collaborator

WSXYT commented May 15, 2026

@copilot apply changes based on the comments in this thread

Copilot AI and others added 2 commits May 15, 2026 15:22
Copy link
Copy Markdown
Contributor

Copilot AI commented May 15, 2026

@copilot apply changes based on the comments in this thread

已按该评审线程完成修复并推送,提交为 28cea7a:已改为显式传播 asyncio.CancelledError、补充 client 级 finally 清理 ipc_client/is_connected 状态,并恢复顶层未知异常的 logger.exception 堆栈日志。

Copilot AI requested a review from WSXYT May 15, 2026 15:26
Copilot finished work on behalf of WSXYT May 15, 2026 15:26
@WSXYT WSXYT merged commit 3ed1aa1 into master May 15, 2026
1 check passed
@WSXYT WSXYT deleted the seer/fix/csharp-ipc-connect-retry branch May 15, 2026 15:29
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.

3 participants