waitForProcessExit 等待进程退出
(ctx context.Context, pid int, timeoutSeconds int, tc *tools.ToolContext)
| 267 | |
| 268 | // waitForProcessExit 等待进程退出 |
| 269 | func (t *KillShellTool) waitForProcessExit(ctx context.Context, pid int, timeoutSeconds int, tc *tools.ToolContext) int { |
| 270 | timeout := time.Duration(timeoutSeconds) * time.Second |
| 271 | startTime := time.Now() |
| 272 | |
| 273 | for time.Since(startTime) < timeout { |
| 274 | if !t.isProcessRunning(ctx, pid, tc) { |
| 275 | // 进程已退出,获取退出码 |
| 276 | waitCmd := fmt.Sprintf("wait %d 2>/dev/null; echo $?", pid) |
| 277 | if result, err := tc.Sandbox.Exec(ctx, waitCmd, nil); err == nil { |
| 278 | var exitCode int |
| 279 | _, _ = fmt.Sscanf(result.Stdout, "%d", &exitCode) |
| 280 | return exitCode |
| 281 | } |
| 282 | return 0 // 假设成功退出 |
| 283 | } |
| 284 | |
| 285 | // 短暂等待后重试 |
| 286 | time.Sleep(100 * time.Millisecond) |
| 287 | } |
| 288 | |
| 289 | // 超时,进程仍在运行 |
| 290 | return -1 |
| 291 | } |
| 292 | |
| 293 | // getCleanupInfo 获取清理信息 |
| 294 | func (t *KillShellTool) getCleanupInfo(task *TaskInfo) map[string]any { |
no test coverage detected